简体   繁体   English

使用静态`Serilog.ILogger`是否安全

[英]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.

相关问题 如何从 Serilog.ILogger 创建 MELA.ILogger 以在 .NET 标准库中使用 - How to create a MELA.ILogger from a Serilog.ILogger for use in a .NET Standard library 在 Azure Function 中使用 Serilog.ILogger - Using Serilog.ILogger in Azure Function 无法解析“Serilog.ILogger”类型的服务:“AspNetCore.Serilog.RequestLoggingMiddleware” - Unable to resolve service for type 'Serilog.ILogger' : "AspNetCore.Serilog.RequestLoggingMiddleware" 将Serilog ILogger添加到静态类 - Adding Serilog ILogger to a static class 使用 Serilog 的模式(通过 ILogger 与使用静态 Serilog.Log) - Pattern to use Serilog (pass ILogger vs using static Serilog.Log) ASP.NET Core Web API - 尝试激活“AuthService”时无法解析“Serilog.ILogger”类型的服务 - ASP.NET Core Web API - Unable to resolve service for type 'Serilog.ILogger' while attempting to activate 'AuthService' 将 Serilog 与 Microsoft.Extensions.Logging.ILogger 结合使用 - Use Serilog with Microsoft.Extensions.Logging.ILogger 使用当前的 Serilog ILogger 实例将消息格式化为字符串(但不打印) - Use current Serilog ILogger instance to format a message into a string (but not print it) Serilog 不会覆盖 ILogger 的最低级别 - Serilog Won't Override Minimum Level For ILogger ILogger 不使用 serilog 写入控制台 - ILogger does not write to console using serilog
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM