使用静态`Serilog.ILogger`是否安全(Is it safe to use a static `Serilog.ILogger`)

编程入门 行业动态 更新时间:2024-10-27 09:37:00
使用静态`Serilog.ILogger`是否安全(Is it safe to use a static `Serilog.ILogger`)

我正在使用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.

更多推荐

本文发布于:2023-07-26 03:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1270749.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态   Serilog   ILogger   safe   static

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!