I am using Serilog for logging in asp.net core website and there is 'class library' used in the project.
Simply, you add the Microsoft.Extensions.Logging
NuGet package to your class library, and then inject ILogger<T>
into your classes:
public class MyClass
{
private readonly ILogger _logger;
public MyClass(ILogger<MyClass> logger)
{
_logger = logger;
}
public void DoSomething()
{
_logger.LogInformation("I'm doing something here.");
}
}
ILogger
is a facade, an API for logging that doesn't actually do any logging itself. The actual logging happens via providers that you plug into the facade when you have a real application using it. This is where something like Serilog would come in. In your actual application, you'd configure logging to use Serilog, and then any time anything calls a method on ILogger
, that gets proxied over to your actual real logging provider (Serilog), and then Serilog does the actual logging.