Serilog allows you to use the registrar configuration for each program; although you can set up sorting by class, this is usually done after the fact with filtering, etc.
Recommended Approach:
Set Serilog as the first thing in Program.Main() or wherever the entry point to the application is:
Log.Logger = new LoggerConfiguration() .WriteTo.Sink1() .WriteTo.Sink2() .CreateLogger();
Note that this sets up the static Log class. Elsewhere in the application, you can write:
Log.Information("This is a message");
and the message will be transmitted to both receivers. You can avoid static Log and skip ILogger , but if you don't have strong preferences, the static option is less difficult.
Configuration
XML / appSettings does not change anything. It just moves some details to the configuration file, therefore changing the first block of code to:
Log.Logger = new LoggerConfiguration() .ReadFrom.AppSettings() .CreateLogger();
And adding:
<add key="serilog:write-to:Sink1" /> <add key="serilog:write-to:Sink2" />
to App.config will have the same effect. Configuration in code is often less of a problem (fewer moving parts) if you can avoid XML.
source share