How to configure Serilog (in the json file) to log info to file, but verbose to console?

huangapple go评论72阅读模式
英文:

How to configure Serilog (in the json file) to log info to file, but verbose to console?

问题

以下是翻译好的部分:

这是我的配置:

"Serilog": {
  "MinimumLevel": "Debug",
  "WriteTo": [
    {
      "Name": "Console",
      "restrictedToMinimumLevel":  "Information"
    },
    {
      "Name": "File",
      "Args": {
        "path": "../logs/log-.txt",
        "rollingInterval": "Day"
      }
    }
  ]
}

这是C#代码:

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
    .Enrich.FromLogContext()
    .WriteTo.Console()
    .CreateBootstrapLogger();

try
{
    Log.Information("Starting up ...");

    var builder = WebApplication.CreateBuilder(args);

    builder.Host.UseSerilog((context, services, configuration) => configuration
        .ReadFrom.Configuration(context.Configuration)
        .ReadFrom.Services(services)
        .Enrich.FromLogContext());
    ...
}
...

我的目标是能够在不重新编译代码的情况下调整配置文件,以更改日志级别。

上述配置会将 Debug 日志同时发送到控制台和文件,尽管我试图仅将 Info 日志发送到控制台。但不起作用 - 两个输出都显示 Debug。

我使用的版本是 Serilog.AspNetCore 6.1.0

我错过了什么?

英文:

Here is my configuration:

  "Serilog": {
    "MinimumLevel": "Debug",
    "WriteTo": [
      {
        "Name": "Console",
        "restrictedToMinimumLevel":  "Information"
      },
      {
        "Name": "File",
        "Args": {
          "path": "../logs/log-.txt",
          "rollingInterval": "Day"
        }
      }
    ]
  },

Here is the C# code:

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
    .Enrich.FromLogContext()
    .WriteTo.Console()
    .CreateBootstrapLogger();

try
{
    Log.Information("Starting up ...");

    var builder = WebApplication.CreateBuilder(args);

    builder.Host.UseSerilog((context, services, configuration) => configuration
        .ReadFrom.Configuration(context.Configuration)
        .ReadFrom.Services(services)
        .Enrich.FromLogContext());
    ...
}
...

My goal is to be able to tweak the configuration file without recompiling the code, if I want to change the log level.

The above configuration sends Debug logs to both the console and the file, though I am trying to send only Info to the console. Does not work - both sinks show Debug.

The version I use is Serilog.AspNetCore 6.1.0

What am I missing?

答案1

得分: 1

restrictedToMinimumLevel 应该放在 Args 中,根据这个文档

"Serilog": {
    "MinimumLevel": "Debug",
    "WriteTo": [
        {
            "Name": "Console",
            "Args": {
                "restrictedToMinimumLevel": "Information"
            }
        },
        {
            "Name": "File",
            "Args": {
                "path": "../logs/log-.txt",
                "rollingInterval": "Day"
            }
        }
    ]
}

尝试了一个最小示例:

Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(builder.Configuration)    
    .CreateLogger();

Log.Debug("Debug");
Log.Information("Info");

它按预期工作:

How to configure Serilog (in the json file) to log info to file, but verbose to console?

英文:

restrictedToMinimumLevel should be inside Args accroding to this doc:

"Serilog": {
    "MinimumLevel": "Debug",
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "restrictedToMinimumLevel":  "Information"
        }
      },
      {
        "Name": "File",
        "Args": {
          "path": "../logs/log-.txt",
          "rollingInterval": "Day"
        }
      }
    ]
  },

Tried with a minimal example:

Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(builder.Configuration)    
    .CreateLogger();

Log.Debug("Debug");
Log.Information("Info");

it works as expected:

How to configure Serilog (in the json file) to log info to file, but verbose to console?

huangapple
  • 本文由 发表于 2023年6月5日 04:17:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402245.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定