Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One logger for a multi-project solution

Tags:

serilog

I am integrating Serilog into an existing multi-class layer and Multi-Assembly Solution that I've created. The previous logging technique I used was simply passing a string up the layers with events. I'd like to get away from this.

I have read that I can use the app.config file to load the logger config into each class in my library but how can I do this in a multi-assembly project.

I have a top-level / startup project and two class library projects. I would like to use the same logger with two sinks for the whole program.

These are the articles I have found so far

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

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

Could someone explain how to accomplish this?

like image 937
TheColonel26 Avatar asked Feb 27 '16 17:02

TheColonel26


People also ask

How do I use nlog in multiple projects?

Just reference NLOG directly in each of the projects and use it. But you only need to configure it in your main application. The configuration will then be shared automatically. Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. Provide details and share your research! But avoid …

Is it worth it to add a logging library to a project?

In some cases this can be worth it (e.g. an open source project where users will likely have different logging libraries), however in most projects this is a bad idea and just adds complexity for little real-world gain. Show activity on this post. Normally, it would go to one of your core projects (having no dependency on other projects).

Is Loggin and common dependant on logging?

So Logging.NLog is dependant on Logging, Logging on Common...etc. When we pack Logging.NLog I would like nuget to discover the Loggin and Common dependecies. At the moment, I created a package with Common, then in Logging I installed the package Common with


1 Answers

Serilog lets you use a logger configuration per program; though it's possible to adjust logging class-by-class this is usually done after the fact with filtering and so-on.

The recommended approach is:

Set up Serilog as the first thing in Program.Main() or wherever your app's entry point is:

Log.Logger = new LoggerConfiguration()
  .WriteTo.Sink1()
  .WriteTo.Sink2()
  .CreateLogger();

Notice this sets up the static Log class. Elsewhere in your app you can now write:

Log.Information("This is a message");

and the message will be passed to both sinks. You can avoid the static Log and instead pass ILogger around, but unless you have strong preferences here, the static option is less hassle.

XML/appSettings configuration doesn't change any of this. It just moves some details to the config file, so 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 hassle (fewer moving parts) if you can avoid the XML.

like image 74
Nicholas Blumhardt Avatar answered Jun 20 '23 09:06

Nicholas Blumhardt