0

I have created a class library for custom logger and register my logger to windows or web application using extension method on IHostBuilder. I want to create "Log" folder (if not exists) in directory were Program.cs file exists. With console application I can create directory using relative path Directory.CreateDirectory(@"......\Logs") and it creates directory in desired location. However when my logger is registered with WebApp it creates directory in "F:\Logs\CsvILogger.csv".

public static IHostBuilder RegisterMyLoggerWithDefaultSettings(this IHostBuilder builder)
{
   //How can I get absolute path of program.cs file here with web application?

}

How can I get path to folder where "program.cs" file exists when my logger is registered to WebApplication?

1 Answer 1

0

I got the idea to find the solution from this post: remove specific logging provider from the services

Note: For getting Webroot path I used reflection to extract property value because I don't want to add reference to Asp.Net core library in my class library where this extesion methord resides. I have only added package reference of Microsoft.Extensions.Hosting in my class library.

public static IHostBuilder FindApplicationPath(this IHostBuilder builder)
    {
        builder.ConfigureLogging((context, loggingBuilder) =>
            {
                var serviceDescriptor = loggingBuilder.Services.Where(x => x.ImplementationInstance != null && x.ServiceType.FullName == "Microsoft.AspNetCore.Hosting.IWebHostEnvironment").FirstOrDefault();
                string appPath;
                if (serviceDescriptor != null)
                {
                    //application is web application
                    appPath = serviceDescriptor.ImplementationInstance
                                     .GetType()
                                     .GetProperty("WebRootPath")
                                     .GetValue(serviceDescriptor.ImplementationInstance, null)
                                     .ToString();
                    //code to use path
                }
                else
                {
                    serviceDescriptor = loggingBuilder.Services
                                        .Where(x => x.ImplementationInstance != null && 
                                                                   x.ServiceType == typeof(Microsoft.Extensions.Hosting.IHostEnvironment))
                                        .FirstOrDefault();
                    var hostingEnvironment = (Microsoft.Extensions.Hosting.Internal.HostingEnvironment)serviceDescriptor.ImplementationInstance;
                    appPath = hostingEnvironment.ContentRootPath;
                     
                      //code to use path
                }

            }
        );
        return builder;
    }

Output:

Web-App:

Z:\Folder1\Folder2\MyWebApp\wwwroot

Console-App:

Z:\MyConsoleApp\bin\Debug\net6.0

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.