Serilog:如何进一步增强appsettings.json中的过滤器表达式? [英] Serilog: How do I further enhance a filter expression in appsettings.json?

查看:1072
本文介绍了Serilog:如何进一步增强appsettings.json中的过滤器表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个使用Serilog的记录器,它们保存到不同的文件中。我想通过appsettings.json而不是代码来创建记录器。

I have two loggers with Serilog and they save to different files. I want to create the loggers through the appsettings.json as opposed to code.

当我通过 Startup.cs 配置,我得到了更具体的日志,我更喜欢用这种方式,因为它更清晰,并且可以让其他人检查日志并了解正在发生的事情而无需太多的技术知识。但是,我想将此应用到appsettings,以便可以将代码迁移到我可以使用的任何其他项目。
仅供参考:我不想覆盖日志级别。

When I create my loggers through the Startup.cs configuration, I get a much more specific log and I prefer to have it that way as it's much clearer and will allow others to check the logs and understand what's going on without needing much technical knowledge. However, I want to apply this to the appsettings so that I can migrate the code to any other project that I may use. FYI: I don't want to override the log level.

例如,使用代码,我像这样创建记录器:

For example, with code I create my loggers like so:

Log.Logger = new LoggerConfiguration()
    .WriteTo.ColoredConsole()
    .WriteTo.Logger(lc => lc
    .Filter.ByExcluding(Matching.FromSource("Microsoft"))
    .WriteTo.File("Serilogs/Program_log_.txt", rollingInterval: RollingInterval.Day)
    .MinimumLevel.Information()
    .MinimumLevel.Override("Microsoft", LogEventLevel.Information))

    .WriteTo.Logger(lc => lc
    .Filter.ByIncludingOnly(Matching.FromSource("Microsoft"))
    .WriteTo.File("Serilogs/ServerTrace_log_.txt", rollingInterval: RollingInterval.Day)
    .MinimumLevel.Information()
    .MinimumLevel.Override("Microsoft", LogEventLevel.Information))

    .CreateLogger();

这将生成两个我喜欢的格式的独立输出:
文件1 :可读的自定义日志:

This generates the two separate outputs that I like the formatting of: File 1: Readable, custom logs:

2019-01-18 11:18:09.873 +00:00 [INF] //Some custom log information
2019-01-18 11:18:09.875 +00:00 [INF] //Some more custom log information

文件2 :堆栈跟踪,服务器信息:

File 2: Stack trace, server information:

2019-01-18 11:17:56.378 +00:00 [INF] User profile is available. Using '//A path' as key repository and Windows DPAPI to encrypt keys at rest.
2019-01-18 11:17:57.840 +00:00 [INF] Request starting HTTP/1.1 GET https://localhost:5001/swagger/index.html  
2019-01-18 11:17:58.074 +00:00 [INF] Request finished in 235.5796ms 200 text/html
2019-01-18 11:17:58.326 +00:00 [INF] Request starting HTTP/1.1 GET https://localhost:5001/swagger/v1/swagger.json  
2019-01-18 11:17:58.452 +00:00 [INF] Request finished in 126.2127ms 200 application/json;charset=utf-8 

当我使用JSON应用程序设置时,它看起来像这样:

When I do it by using the JSON appsettings it looks like so:

 "Serilog": {
    "Using": [
      "Serilog.Sinks.File",
      "Serilog.Filters.Expressions",
      "Serilog.Settings.Configuration"
    ],
    "WriteTo": [
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "Filter": [
              {
                "Name": "ByIncluding",
                "Args": {
                  "expression": "SourceContext = 'Microsoft'"
                }
              }
            ],
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "./Serilogs/server_logs_.log",
                  "rollingInterval": "Day"
                }
              }
            ]
          }
        }
      },
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "Filter": [
              {
                "Name": "ByExcluding",
                "Args": {
                  "expression": "SourceContext = 'Microsoft.AspNetCore.Hosting.Internal.WebHost'"
                }
              }
            ],
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "./Serilogs/program_logs_.log",
                  "rollingInterval": "Day"
                }
              }
            ]
          }
        }
      }
    ]
  }

这将生成两个文件。
文件1 :可读的自定义日志:

And this generates the two files. File 1: Readable, custom logs:

2019-01-18 11:22:52.903 +00:00 [INF] User profile is available. Using '// A path' as key repository and Windows DPAPI to encrypt keys at rest.
2019-01-18 11:22:59.229 +00:00 [INF] Route matched with {action = "PollForStatusUpdate", controller = "StatusMessage"}. Executing action {apiname}.{controller}.{method}.PollForStatusUpdate ({apiname})
2019-01-18 11:22:59.505 +00:00 [INF] Executing action method {apiname}.{controller}.{method}.PollForStatusUpdate ({apiname}) with arguments (["{apiname}.{controller}.{method}.MessageResult"]) - Validation state: "Valid"
2019-01-18 11:22:59.632 +00:00 [INF] //Some custom log information
2019-01-18 11:22:59.639 +00:00 [INF] //Some more custom log information
{apiname}.{controller}.{method}.PollForStatusUpdate ({apiname}), returned result Microsoft.AspNetCore.Mvc.OkObjectResult in 516.8129ms.
2019-01-18 11:23:00.040 +00:00 [INF] Executing ObjectResult, writing value of type '{apiname}.{controller}.{method}.MessageResult'.
2019-01-18 11:23:00.048 +00:00 [INF] Executed action {apiname}.{controller}.{method}.PollForStatusUpdate ({apiname}) in 813.08940000000007ms

文件2 :堆栈跟踪,服务器信息:

File 2: Stack trace, server information:

2019-01-18 11:22:52.903 +00:00 [INF] User profile is available. Using 'C:\Users\msharp\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
2019-01-18 11:22:54.263 +00:00 [INF] Request starting HTTP/1.1 GET https://localhost:5001/swagger/index.html  
2019-01-18 11:22:54.473 +00:00 [INF] Request finished in 211.831ms 200 text/html
2019-01-18 11:22:54.737 +00:00 [INF] Request starting HTTP/1.1 GET https://localhost:5001/swagger/v1/swagger.json  
2019-01-18 11:22:54.838 +00:00 [INF] Request finished in 100.8892ms 200 application/json;charset=utf-8
2019-01-18 11:22:59.205 +00:00 [INF] Request starting HTTP/1.1 POST https://localhost:5001/api/StatusMessage/PollForStatusUpdate application/json-patch+json 180
2019-01-18 11:22:59.229 +00:00 [INF] Route matched with {action = "PollForStatusUpdate", controller = "StatusMessage"}. Executing action PureValuationsApi.Controllers.StatusMessageController.PollForStatusUpdate (PureValuationsApi)
2019-01-18 11:22:59.505 +00:00 [INF] Executing action method {apiname}.{controller}.{method}.PollForStatusUpdate ({apiname}) with arguments (["{apiname}.{controller}.{method}.MessageResult"]) - Validation state: "Valid"
2019-01-18 11:22:59.632 +00:00 [INF] //Some custom lof information
2019-01-18 11:22:59.639 +00:00 [INF] //Some more custom log information
{apiname}.{controller}.{method}.PollForStatusUpdate ({apiname}), returned result Microsoft.AspNetCore.Mvc.OkObjectResult in 516.8129ms.
2019-01-18 11:23:00.040 +00:00 [INF] Executing ObjectResult, writing value of type '{apiname}.{controller}.{method}.MessageResult'.
2019-01-18 11:23:00.048 +00:00 [INF] Executed action {apiname}.{controller}.{method}.PollForStatusUpdate ({apiname}) in 813.08940000000007ms
2019-01-18 11:23:00.052 +00:00 [INF] Request finished in 847.5964ms 200 application/json; charset=utf-8

因此,通过应用相同的技术,结果是与文件1 现在包含更多操作信息,而文件2 包含堆栈 plus 不同,自定义日志信息,尽管我希望它们完全分开。

So, by applying what feels the same technique, the outcome of the results are different as File 1 now contains more action information and File 2 contains the stack plus the custom log information although I want them completely separate.

推荐答案

我认为您的过滤器不太正确。

I think your filters are not quite right.

过滤器表达式应为:

"expression": "SourceContext = 'Microsoft' or StartsWith(SourceContext, 'Microsoft.')"

(我想您可以只需执行 StartsWith(SourceContext,'Microsoft')不带最后一个点,但对于 MicrosoftOrIsItReally.MyNamespace

(I guess you could just do StartsWith(SourceContext, 'Microsoft') without the last dot , but then that may not behave properly for namespaces like MicrosoftOrIsItReally.MyNamespace)

(内部 Matching.ForSource 执行 SourceContext.StartsWith( ..)这种过滤,如在源文件中

(internally Matching.ForSource does a SourceContext.StartsWith(..) kind of filtering , as seen in the source)

要进行确认,您可能需要编辑 outputTemplate 以显示 SourceContext 属性,并查看日志事件的来源。 (默认值为 {Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj} {NewLine} {Exception} )。您可以将其更改为 {Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}]< {SourceContext}> {Message:lj} {NewLine } {Exception} 包括 SourceContext 属性。

To confirm it, you may want to edit the outputTemplate of your File sink to display the SourceContext property and see the origin of the log events. (the default is "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}") . You could change it to "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}]<{SourceContext}> {Message:lj}{NewLine}{Exception}" to include the SourceContext property.

顺便说一句,您有一个多余的使用 指令。 Serilog.Settings.Configuration 不是必需的

By the way, you have an extra, unneeded "Using" directive. The "Serilog.Settings.Configuration" is not necessary

这篇关于Serilog:如何进一步增强appsettings.json中的过滤器表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆