Is there a way to enrich the log with a property from configuration?

不羁岁月 提交于 2020-01-05 03:56:08

问题


I know I can add enrichers this way:

{
  "Serilog": {
    "Using": [
      "Serilog",
      "Serilog.Enrichers.Environment",
      "Serilog.Enrichers.Process",
      ...
    ],
    "Enrich": [
      "FromLogContext",
      "WithMachineName",
      "WithProcessId"
    ]
...

Can I do the same for a property (ApplicationName)?


回答1:


One way of doing this is with properties. But the configuration for enrichment properties has a caveat that it's a top level configuration and not an enrichment configuration:

{
  "Serilog": {
    "Using": [
      "Serilog",
      "Serilog.Enrichers.Environment",
      "Serilog.Enrichers.Process",
      ...
    ],
    "Properties": {
      "ApplicationName": "my application"
    },
    "Enrich": [
      "FromLogContext",
      "WithMachineName",
      "WithProcessId"
    ]
...

This will cause the configuration reader to call something like this:

loggerConfiguration.Enrich.WithProperty("ApplicationName", "my application");


来源:https://stackoverflow.com/questions/50486481/is-there-a-way-to-enrich-the-log-with-a-property-from-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!