【问题标题】:Exclude specific endpoint serilog logging using aspnet core使用 aspnet 核心排除特定端点 serilog 日志记录
【发布时间】:2018-08-09 01:11:55
【问题描述】:

我们有一个 aspnetcore 应用程序和一个运行状况检查,它每 x 秒向一个端点发出请求。我们的 API 使用 serilog,记录级别信息,记录对 API 的每个请求。

我的问题是:我们如何过滤以排除对特定端点的日志请求?

例如,这是我们今天的日志记录代码:

        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
                .Enrich.FromLogContext()                    
                .Filter.ByExcluding(c => c.MessageTemplate.Text.Contains("Request"))
                .WriteTo.Console()
                .WriteTo.RollingFile(hostingContext.Configuration["log-path"]))                    
            .Build();

今天我们只能过滤所有请求。我们如何过滤特定的端点请求?

【问题讨论】:

    标签: asp.net-core .net-core serilog


    【解决方案1】:

    我找到了方法。

    我没有按MessageTemplate 排除,而是按属性值排除。

    过滤器是这样的:

    Filter.ByExcluding(c => c.Properties.Any(p => p.Value.ToString().Contains("swagger")))
    

    最后代码应该是这样的:

    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
            .Enrich.FromLogContext()                  
            .Filter.ByExcluding(c => c.Properties.Any(p => p.Value.ToString().Contains("swagger")))
            .WriteTo.Console()
            .WriteTo.RollingFile(hostingContext.Configuration["log-path"]))                    
        .Build();
    

    希望这对帮助过我的人有所帮助!

    【讨论】:

    • 嗨,你能举一个 Log.Info 调用的例子,因为这个调用会被忽略吗?我不太清楚
    猜你喜欢
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多