I have a solution with an ASP.NET Core (2.1.5) Web API project which references a couple of .NET Standard 2.0 class libraries projects.
I want to implement some logging to my application and I have already done this for the Web API project using NLog (in fact, it can be any provider implementing ILogger). To do this, I defined in Program.cs a static field which is initialized during the runtime and configured it with using WebHostBuilderExtensions.ConfigureLogging:
using Microsoft.Extensions.Logging;
public class Program
{
private static readonly Logger logger = NLogBuilder.ConfigureNLog("NLog.config").GetCurrentClassLogger();
// other content ...
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
var webHost = WebHost.CreateDefaultBuilder(args)
.UseStartup(typeof(Startup).GetTypeInfo().Assembly.FullName)
.ConfigureLogging(builder =>
{
builder.ClearProviders();
builder.SetMinimumLevel(LogLevel.Information);
})
.UseNLog();
// other content ...
}
This allows me to use DI of ILogging in every class inside my ASP.NET Core application whenever I need it:
using Microsoft.Extensions.Logging;
public class Startup
{
public IConfiguration Configuration { get; }
private readonly ILogger<Startup> _logger;
public Startup(IConfiguration configuration, ILogger<Startup> logger)
{
Configuration = configuration;
_logger = logger;
}
}
But now I am totally confused with setting up a logging for my class libraries. One of the options I can imagine is to use the DI mechanism to allow my libraries to call the same ILogger interface like this:
using Microsoft.Extensions.Logging;
public class MyLibraryClass
{
private ILogger<MyLibraryClass> _logger;
public MyLibraryClass(ILogger<MyLibraryClass> logger)
{
_logger = logger;
}
public void SomeLibraryMethod()
{
_logger.LogInformation("Some library method logging here...);
}
}
But that looks to be quite awkward as I need to inject this dependency for all my libraries classes where I need logging (so to say, to all classes).
I also think of some static class for each library project where this dependency is injected just once and so that the static class can act as a wrapper around ILogger. Or even to move NLog from the WebAPI project to one of the libraries (or even make a separate library) and use it call this library from all other projects. But as advised here, it is better to have a separate instance of ILogger for each class to filter different loggers. Or maybe it is enough to create static loggers for a certain project?
My another concern is that such static wrappers around ILogger would require from me to define my own interface to work with log messages. But ILogger is itselt a well-designed interface and such attitude seems to be some kind of double work.
I have looked through a lot of step-by-step guides but most of them describe how to configure logging just for one ASP.NET Core web application (or most probably, I used wrong search keywords). Anyway, I am stuck at this moment and don't know the correct way to make my libraries use the same logging provider and file as for my main WebAPI application. Maybe, there are any well-known practices that I have missed?