小编Fab*_*Bit的帖子

如何使用 Serilog 的 ReadFrom.KeyValuePairs 方法?

在我的 appsettings.json 我写过:

{
    "Logging": {
        "PathLogsFile": "./Logs",
         "IncludeScopes": false,
         "LogLevel": {
             "Default": "Debug",
             "System": "Information",
             "Microsoft": "Information"
         }
    }
}
Run Code Online (Sandbox Code Playgroud)

并在我的startup.cs文件中的构造函数中

var pathLogsFile = Configuration["Logging:PathLogsFile"];            
var logLevelApp = Configuration.GetSection("Logging:LogLevel:Default");

Log.Logger = new LoggerConfiguration()        
    .ReadFrom.KeyValuePairs(new List<KeyValuePair<string, string>>()
        {
            new KeyValuePair<string, string>(logLevelApp.Key, logLevelApp.Value)                        
        }.ToDictionary(x => x.Key, x => x.Value)) 
    .Enrich.FromLogContext()
    .WriteTo.Logger(lc => lc
        .Filter.ByIncludingOnly(evt => evt.Level == Serilog.Events.LogEventLevel.Error)
        .WriteTo.RollingFile(Path.Combine(pathLogsFile, "error-{Date}.log")))
    .WriteTo.Logger(lc => lc
        .Filter.ByIncludingOnly(evt => evt.Level >= Serilog.Events.LogEventLevel.Debug)
        .WriteTo.RollingFile(Path.Combine(pathLogsFile, "log-{Date}.log")))
    .CreateLogger();
Run Code Online (Sandbox Code Playgroud)

但是从 appsettings 读取的最低级别不起作用。有任何想法吗?我该如何解决?

c# serilog asp.net-core

2
推荐指数
1
解决办法
1717
查看次数

标签 统计

asp.net-core ×1

c# ×1

serilog ×1