16

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?

James World
  • 29,019
  • 9
  • 86
  • 120

1 Answers1

19

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

Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101