[英]Is it safe to use a static `Serilog.ILogger`
I am using Serilog . 我正在使用Serilog 。 I want to write log entries with a
SourceContext
of the containing class. 我想用包含类的
SourceContext
写日志条目。
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?
根据经验,上述似乎有效,但任何人都可以提供明确的指导吗?
是的,Serilog中的ILogger
始终可以安全地从多个线程同时使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.