Brand new to Serilog, I apologize in advance if my question has been answered before.
All Serilog configuration examples I've found imply that somewhere very early in the execution chain some code similar to the below one should get executed in order to make Serilog write the log.
In a regular console app, yeah, that works, I just add that code in Main(). However, I am writing a DLL, so there is no Main() method. I can't tell which method will get called first, so I can't add the code to a particular method.
So I'm pretty much stuck, unless I am missing something obvious. Could anyone help me with a hint?
Thank you
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Verbose()
.WriteTo.File("log.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
Logging pipeline setup is generally a concern of the host application that is using your library and not something you setup within the library itself.
If your class library is part of your solution together with the host app and you decided to use Serilog, your library would normally rely on Log.Logger
having being setup by the host process that is consuming your library.
public class MyClassWithinMyLibrary
{
private readonly ILogger _log = Log.ForContext<MyClassWithinMyLibrary>();
public void MyMethod()
{
_log.Information("Doing stuff inside the library");
}
}
If you are building a library that is going to be consumed by others (e.g. via NuGet) and you don't really know if they want to use Serilog, or something else, then you'd normally take a dependency on Microsoft.Extensions.Logging
and require the user to provide an ILogger<T>
instance in the constructor of your classes, or use a custom interface that you provide within your library to make the bridge between their logging system and your library's logging system.
In a more niche scenario, where you really want to use Serilog internally for logging within the library (perhaps because a non-.NET app consumes your library), then you can use a module initializer, which is a method that is called one time when your assembly is used, and gives you the opportunity to execute initialization code:
internal static class ModuleInitializer
{
[ModuleInitializer]
internal static void Run()
{
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Verbose()
.WriteTo.File("log.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
}
}
N.B. Module initializers require you to compile your code with the C# 9 compiler. If you're using an older version, you can still use a module initializer but you'll need a weaving tool such as ModuleInit.Fody that can include the module initializer in the assembly for you.