Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use a static `Serilog.ILogger`

Tags:

c#

serilog

I am using Serilog. I want to write log entries with a SourceContext of the containing class.

Is it safe (including thread safe) to do this:

using Serilog;

...

class Foo
{
    private static readonly ILogger Log =
     Serilog.Log.ForContext<Foo>();

    /* Invoke Log.Debug(...) etc. in other methods */
}

The documentation (linked) has a small example that adds the context within a method, rather than creating one to share for the type as above. I'd rather not have to put the same boiler plate code into every method where I am logging. Empirically the above seems to work, but can anyone offer definitive guidance?

like image 610
James World Avatar asked Mar 21 '17 13:03

James World


People also ask

Is Serilog safe to use?

The npm package serilog was scanned for known vulnerabilities and missing license, and no issues were found. Thus the package was deemed as safe to use.

Is ILogger thread safe?

Yes, ILogger s in Serilog are always safe to use concurrently from multiple threads.

Is Serilog logger Singleton?

Log. Logger is thread-safe, thus registering as a singleton as you are doing above is correct if you want all classes to share the same instance (without SourceContext s) - nothing wrong with that.


1 Answers

Yes, ILoggers in Serilog are always safe to use concurrently from multiple threads.

like image 84
Nicholas Blumhardt Avatar answered Oct 16 '22 21:10

Nicholas Blumhardt