简体   繁体   English

Serilog LogContext vs Owrich中的Enrich

[英]Serilog LogContext vs Enrich in Owin

I'm trying to figure out what approach is the best: 我想弄清楚哪种方法最好:

I have an Owin pipeline where I would like to enrich all logs with various information based on the request (URL, IP address, etc.) 我有一个Owin管道,我想根据请求(URL,IP地址等)丰富所有日志和各种信息

As I see it I have two possibilities with Serilog ForContext() in the Owin pipeline: 在我看来,我在Owin管道中有两种可能的Serilog ForContext()

public override async Task Invoke(IOwinContext context)
{
    using (LogContext.PushProperties(
        RequestUri(context),
        RemoteIp(context)))
        {
            await Next.Invoke(context);
        }
    }

Or set up enrichers when configuring Serilog: 或者在配置Serilog时设置richrs:

 var configuration = new LoggerConfiguration()
     .Enrich.WithRequestUrl()
     .Enrich.WithRequestClientIp()
     ...

Is there a right or wrong way to do this? 有没有正确或错误的方法来做到这一点? Personally I like the LogContext as I provides me with an easy way to access the Owin Context, but on the other hand all my requests will trigger all the enrichers to be evaluated. 我个人喜欢LogContext,因为我为我提供了一种访问Owin Context的简单方法,但另一方面,我的所有请求都将触发所有要评估的richves。 But then again, it is only done once during a request, instead of for each logging statement? 但话说回来,它只在请求期间执行一次,而不是每个日志记录声明?

Is this solely opinion-based and should therefore be closed, or is there any advantages to the one approach compared to the other? 这是否仅以意见为基础,因此应该关闭,或者与另一种方法相比,这种方法是否有任何优势?

I don't believe Owin provides the ambient ( static ) contextual information you'd need to make enrichment work. 我不相信Owin提供了进行浓缩工作所需的环境( static )上下文信息。 For that reason I'd go for the LogContext version first, personally. 出于这个原因,我首先会亲自去看LogContext版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM