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?
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.
Yes, ILogger s in Serilog are always safe to use concurrently from multiple threads.
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.
Yes, ILogger
s in Serilog are always safe to use concurrently from multiple threads.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With