1

我正在使用滚动将所有日志写入一个文件。但我想用Information,WarningExceptions滚动文件将它们分开。

我目前的配置是这样的

     "Serilog": {
    "WriteTo": [
      {
        "Name": "RollingFile",
        "Args": {
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}",
          "pathFormat": "logs\\log-{Hour}.log",
          "rollOnFileSizeLimit ": true,
          "retainedFileCountLimit ": null,
          "rollingInterval": "Hour",
          "fileSizeLimitBytes": 5000000
        }
      }
    ]
  },

班级

public ILogger GetLogger()
    {
        var configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
            .Build();
        _logger =
            new LoggerConfiguration()
                .ReadFrom.Configuration(configuration)
                .CreateLogger();

        return _logger ;
    }
4

2 回答 2

3

在 Serilog 中,您可以通过子记录器或通过Serilog.Sinks.Map进行这种分离。

您将无法通过配置整个管道,appsetting.json但您可以使用您选择的自定义键轻松配置每个文件的路径appsetting.json,并在通过代码配置日志管道时使用它。


使用子记录器的示例,每个子记录器都应用了过滤器:

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .WriteTo.Logger(c =>
        c.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Debug)
            .WriteTo.File("Debug.log"))
    .WriteTo.Logger(c =>
        c.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Error)
            .WriteTo.File("Error.log"))
    .CreateLogger();

Log.Debug("This goes to Debug.log only");
Log.Error("This goes to Error.log only");

Log.CloseAndFlush();

使用Serilog.Sinks.Map将每个映射LogEventLevel到不同文件的示例。

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .WriteTo.Map(evt => evt.Level, (level, wt) => wt.File($"{level}.log"))
    .CreateLogger();

Log.Debug("This goes to Debug.log only");
Log.Error("This goes to Error.log only");

Log.CloseAndFlush();
于 2020-05-28T16:15:44.100 回答
0

我没有测试,但通过推断文档:您可以尝试在 appconf 文件中创建 2 个部分

  "ErrorLog": {
    "Using": [ "Serilog.Sinks.File" ],
    "MinimumLevel": "Debug",
    "WriteTo": [
      {
        "Name": "RollingFile",
        "Args": {
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}",
          "pathFormat": "logs\\ Errorlog-{Hour}.log",       
          "rollingInterval": "Hour",
          "restrictedToMinimumLevel": "Error"
        }
      }
    ]
  },
  "InfoLog": {
    "Using": [ "Serilog.Sinks.File" ],
    "MinimumLevel": "Debug",
    "WriteTo": [
      {
        "Name": "RollingFile",
        "Args": {
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}",
          "pathFormat": "logs\\ Infolog-{Hour}.log",
          "rollingInterval": "Hour",
          "restrictedToMinimumLevel": "Information"
        }
      }
    ]
  }

然后创建 2 个记录器:

    var configuration = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json")
        .Build();


        var errorSection = configuration.GetSection("ErrorLog");
        var infoSection = configuration.GetSection("InfoLog");


        _errorlog = new LoggerConfiguration()
            .ReadFrom
            .ConfigurationSection(errorSection)
            .CreateLogger();

        _infolog = new LoggerConfiguration()
            .ReadFrom
            .ConfigurationSection(infoSection)
            .CreateLogger();
于 2020-05-25T06:13:48.613 回答