【问题标题】:How to pass IHttpContextAccessor to Serilog Enricher by Configuration如何通过配置将 IHttpContextAccessor 传递给 Serilog Enricher
【发布时间】:2021-10-14 05:25:21
【问题描述】:

我想将 httpContextAccessor.HttpContext.TraceIdentifier 添加到每个日志条目,因此创建了一个自定义丰富器,我也使用 Serilog.Settings.Configuration 进行了设置,但不确定如何通过配置将 HttpContextAccessor 传递给 Enricher

{
    "Enrich": ["FromLogContext", "LogTraceid"],
    "WriteTo": [{
        "Name": "File",
        "Args": {
            "path": "App_Data/Logs/App.log",
            "rollingInterval": "Day",
            "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] [{TraceId}] [{RequestId}] {Message}{NewLine}{Exception}"
        }
    }]
}

【问题讨论】:

    标签: asp.net-core serilog


    【解决方案1】:

    这个例子很好用

    从这个仓库得到它

    https://github.com/JohnLTaylor/Serilog.Enrichers.TraceIdentifier/blob/master/src/Serilog.Enrichers.TraceIdentifier/Enrichers/TraceIdentifierEnricher.cs

    class TraceIdentifierEnricher : ILogEventEnricher
    {
        private const string TraceIdentifierPropertyName = "TraceIdentifier";
        private readonly IHttpContextAccessor _contextAccessor;
    
        public TraceIdentifierEnricher() : this(new HttpContextAccessor())
        {
        }
    
        public TraceIdentifierEnricher(IHttpContextAccessor contextAccessor)
        {
            _contextAccessor = contextAccessor;
        }
    
        public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
        {
            var property = propertyFactory.CreateProperty(TraceIdentifierPropertyName, _contextAccessor.HttpContext?.TraceIdentifier ?? "-");
            logEvent.AddOrUpdateProperty(property);
        }
    }
    
    public static class TraceIdentifierLoggerConfigurationExtensions
    {
        public static LoggerConfiguration WithTraceIdentifier(
           this LoggerEnrichmentConfiguration enrichmentConfiguration)
        {
            if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
            return enrichmentConfiguration.With<TraceIdentifierEnricher>();
        }
    }
    

    同时确保您已在服务集合中注册IHttpContextAccessor

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddHttpContextAccessor();
        ...
    }
    

    【讨论】:

    • 运行这个,带有 IHttpContextAccessor 参数的构造函数似乎从未通过 asp.net 依赖注入使用。但是TraceIdentifierEnricher一次Program.Main 构造的,其构造函数为空(创建HttpContextAccessor 本身)。它似乎工作。这是标准做法吗?看着这个 repo (github.com/trenoncourt/serilog-enrichers-aspnetcore-httpcontext),他似乎在尝试做一些更复杂的事情。知道每种方法的优缺点吗?
    • services.AddHttpContextAccessor(); ..对我来说是个问题!非常感谢。
    【解决方案2】:

    如果您使用Serilog.AspNetCore,则自动包含TraceIdRequestId

    查看代码示例:https://github.com/serilog/serilog-aspnetcore/tree/dev/samples

    【讨论】:

    • 感谢您的回复,我在丰富器中也有一些使用HttpContext请求查询字符串的自定义逻辑,所以我想知道如何实现
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 2020-01-27
    • 2015-10-06
    • 1970-01-01
    相关资源
    最近更新 更多