首页 新闻 会员 周边 捐助

Serilog中怎么配置不同的日志分类写入到不同的文件中?

0
悬赏园豆:5 [待解决问题]

我想把微软的日志和自己应用的日志分别写入到不同的文件中去

火焰人生的主页 火焰人生 | 初学一级 | 园豆:195
提问于:2020-04-13 14:14
< >
分享
所有回答(2)
0

参考 Serilog : Log to different files 中的回答:

Log.Logger = new LoggerConfiguration()
    .WriteTo.Logger(lc => lc
        .Filter.ByExcluding(Matching.FromSource("MyApp.Performance"))
        .WriteTo.File("first.json", new JsonFormatter()))
    .WriteTo.Logger(lc => lc
        .Filter.ByIncludingOnly(Matching.FromSource("MyApp.Performance"))
        .WriteTo.File("second.json", new JsonFormatter()))
    .CreateLogger();
dudu | 园豆:29759 (高人七级) | 2020-04-13 15:12

我想在appsettings.json进行配置,不知道这个过滤条件能不能配置进去?
"Serilog": {
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "log.txt",
"rollingInterval": "Day"
}
},
{
"Name": "Console",
"Args": {}
}
]
},

支持(0) 反对(0) 火焰人生 | 园豆:195 (初学一级) | 2020-04-13 15:18

@火焰人生: 参考 Serilog : how do you specify a filter expression in config file 中的回答:

Install-Package Serilog.Filters.Expressions
"Filter": [
  {
    "Name": "ByExcluding",
    "Args": {
      "expression": "SourceContext = 'Microsoft.AspNetCore.Hosting.Internal.WebHost'"
    }
  }
]
支持(0) 反对(0) dudu | 园豆:29759 (高人七级) | 2020-04-13 15:30

@dudu: 下面这样配置,不起作用

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "Serilog": {
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "system-.txt",
          "rollingInterval": "Day"
        },
        "Filter": [
          {
            "Name": "ByIncludingOnly",
            "Args": {
              "expression": "SourceContext == 'Microsoft.AspNetCore.Hosting.Internal.WebHost'"
            }
          }
        ]
      },
      {
        "Name": "File",
        "Args": {
          "path": "web-.txt",
          "rollingInterval": "Day"
        },
        "Filter": [
          {
            "Name": "ByExcluding",
            "Args": {
              "expression": "SourceContext == 'Microsoft.AspNetCore.Hosting.Internal.WebHost'"
            }
          }
        ]
      },
      {
        "Name": "Console",
        "Args": {}
      }
    ]
  },
  "AllowedHosts": "*"
}

支持(0) 反对(0) 火焰人生 | 园豆:195 (初学一级) | 2020-04-13 15:48

@火焰人生: 在 "Serilog" 下添加 "Using": [ "Serilog.Sinks.File" ] 试试

支持(0) 反对(0) dudu | 园豆:29759 (高人七级) | 2020-04-13 16:04

@dudu: 还是没起作用

支持(0) 反对(0) 火焰人生 | 园豆:195 (初学一级) | 2020-04-13 16:08
0

可以试一下 按月拆文件夹,按日和级别拆文件
appsetting.json 可能不太好配,用是没问题的
pathPrefixKey变量为目录前缀,比如 d:/logs/xxx_api
输出
D:\logs\xxx_api\2020-03\debug-20200331.txt
D:\logs\xxx_api\2020-03\warning-20200331.txt
D:\logs\xxx_api\2020-03\information-20200331.txt
D:\logs\xxx_api\2020-04\debug-20200428.txt
D:\logs\xxx_api\2020-04\warning-20200428.txt
D:\logs\xxx_api\2020-04\information-20200428.txt

便于按月按日分析问题,也可以修改保存规则,代码如下:

.WriteTo.Map(
le => new Tuple<DateTime, LogEventLevel>(
new DateTime(le.Timestamp.Year, le.Timestamp.Month, le.Timestamp.Day), le.Level),
(key, log) => log.Async(a =>
a.File(new CompactJsonFormatter(),
Path.Combine(pathPrefixKey, $"{key.Item1:yyyy-MM}/{key.Item2.ToString().ToLower()}-.txt"),
rollingInterval: RollingInterval.Day, retainedFileCountLimit: null)),
sinkMapCountLimit: 1);

黄靖洆 | 园豆:179 (初学一级) | 2020-04-28 15:02

能说一下这个都是引得哪个包吗?

支持(0) 反对(0) CrazyRisk | 园豆:202 (菜鸟二级) | 2021-05-19 15:01
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册