One registrar for multi-project solution

I integrate Serilog into the existing tiered layer and the Multi-Assembly Solution that I created. The previous logging method I used just passed lines to event layers. I would like to get away from this.

I read that I can use the app.config file to load the logger configuration into each class in my library, but how to do this in a project with multiple assemblies.

I have a top-level / launch project and two class library projects. I would like to use the same magazine with two receivers for the entire program.

These are the articles that I have found so far.

https://github.com/serilog/serilog/wiki/AppSettings

http://nblumhardt.com/2014/04/xml-configuration-for-serilog/

Can someone explain how to do this?

+6
source share
1 answer

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.

+5
source

All Articles