首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将日志分离到多个日志文件中

将日志分离到多个日志文件中
EN

Stack Overflow用户
提问于 2019-04-24 17:10:04
回答 1查看 558关注 0票数 2

我尝试将Serilog配置为将普通日志和实体框架日志分开。下面是我的serilog配置:

"Serilog": {
"Using": [
  "Serilog.Settings.Configuration",
  "Serilog.Sinks.File"
],
"MinimumLevel": {
  "Default": "Debug",
  "Override": {
    "Microsoft": "Debug",
    "System": "Warning"
  }
},
"WriteTo": [
  {
    "Name": "RollingFile",
    "Args": {
      "configureLogger": {
        "WriteTo": [
          {
            "Name": "LogFile",
            "Args": {
              "textFormatter": "JsonFormatter",
              "fileSizeLimitBytes": 2147483648,
              "retainedFileCountLimit": 5
            }
          }
        ]
      },
      "pathFormat": "log-{Date}.log",
      "logDirectory": ".",
      "Filter": [
        {
          "Name": "ByExcluding",
          "Args": {
            "expression": "StartsWith(SourceContext, 'Microsoft.EntityFrameworkCore.')"
          }
        }
      ]
    }
  },
  {
    "Name": "RollingFile2",
    "Args": {
      "configureLogger": {
        "WriteTo": [
          {
            "Name": "LogFile2",
            "Args": {
              "textFormatter": "JsonFormatter",
              "fileSizeLimitBytes": 2147483648,
              "retainedFileCountLimit": 5
            }
          }
        ]
      },
      "pathFormat": "log-DB-{Date}.log",
      "logDirectory": ".",
      "Filter": [
        {
          "Name": "ByIncluding",
          "Args": {
            "expression": "StartsWith(SourceContext, 'Microsoft.EntityFrameworkCore.')"
          }
        }
      ]
    }
  },
  {
    "Name": "Console",
    "Args": {
      "outputTemplate": "{Timestamp:HH:mm:ss} [{Level}] ({CorrelationToken}) {Message}{NewLine}{Exception}"
    }
  }
],
"Enrich": [
  "FromLogContext",
  "WithMachineName",
  "WithProcessId",
  "WithThreadId",
  "WithHttpRequestId"
]

}

我没有得到第二个日志文件。我基于这篇文章来实现我的配置Filter Serilog logs to different sinks depending on context source?

我在serilog配置中有什么不理解的地方?我通过代码找到了很多配置示例,但通过appsettings.json找到的很少。我更喜欢使用appsetting.json

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2019-05-17 03:27:24

你必须设置子记录器,而不是滚动文件。WriteTo数组中有两个条目,但这两个条目都是"Name": "RollingFile"。将它们更改为"Name": "Logger",然后将筛选器配置移动到这些子记录器的相应configureLogger部分。如下所示:

    "WriteTo": [
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "log-{Date}.log",
                  "retainedFileCountLimit": 5,
                  "fileSizeLimitBytes": 2147483648,
                }
              }
            ],
            "Filter": [
              {
                "Name": "ByExcluding",
                "Args": {
                  "expression": "StartsWith(SourceContext, 'Microsoft.EntityFrameworkCore.')"
                }
              }
            ]
          }
        }
      },
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "log-db-{Date}.log",
                  "retainedFileCountLimit": 5,
                  "fileSizeLimitBytes": 2147483648,
                }
              }
            ],
            "Filter": [
              {
                "Name": "ByIncluding",
                "Args": {
                  "expression": "StartsWith(SourceContext, 'Microsoft.EntityFrameworkCore.')"
                }
              }
            ]
          }
        }
      }
    ],

在我的测试中,仍然有一些Microsoft.AspNetCoreMicrosoft.Extensions名称空间通过第二个筛选器-所以不确定ByIncluding是否像ByExcluding一样阻塞。我不确定有什么其他类型的过滤器可用于此。

更新

我在其他地方看到,这显示为另一种类型的过滤器,您可以尝试一下:"Name": "ByIncludingOnly"

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55826381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档