我正在使用Serilog 。 我想用包含类的SourceContext写日志条目。
这样做是否安全(包括线程安全):
using Serilog; ... class Foo { private static readonly ILogger Log = Serilog.Log.ForContext<Foo>(); /* Invoke Log.Debug(...) etc. in other methods */ }文档(链接)有一个小例子,它在方法中添加上下文 ,而不是像上面那样为类型创建一个共享。 我宁愿不必将相同的锅炉板代码放入我记录的每个方法中。 根据经验,上述似乎有效,但任何人都可以提供明确的指导吗?
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?
最满意答案
是的,Serilog中的ILogger始终可以安全地从多个线程同时使用。
Yes, ILoggers in Serilog are always safe to use concurrently from multiple threads.
更多推荐
发布评论