嘗試設置 Serilog 2.8.0(在 .NET 4.6.2 中)的最低日志級別。日志記錄工作正常,但無法覆蓋功能。這是我的Program.cs:using System;using LogTest1.Classes;using Microsoft.Extensions.Configuration;using Serilog;using SomeLib;namespace LogTest{ internal class Program { private static void Main(string[] args) { var configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.json", false, true) .Build(); Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(configuration) .CreateLogger(); Log.Verbose("Verbose"); Log.Debug("Debug"); Log.Information("Information"); Log.Warning("Warning"); Log.Error("Error"); Log.Fatal("Fatal"); Console.ReadKey(); } }}以及appsettings.json文件:{ "Serilog": { "Using": [ "Serilog.Sinks.Console" ], "MinimumLevel": { "Default": "Warning", "Override": { "LogTest": "Information" } }, "WriteTo": [ { "Name": "Console" } ] }}期望看到從Information開始的所有日志,但只從warning中獲取。
1 回答

瀟湘沐
TA貢獻1816條經驗 獲得超6個贊
根據您的配置,您將覆蓋MinimumLevel 僅從SourceContext名稱為 發送的日志消息。LogTest
"Override": {
? "LogTest": "Information"
}
對您所做的簡單調用Log.Information("Information")不會設置源上下文,因此覆蓋不適用...您必須首先創建上下文。
var contextLog = Log.ForContext("SourceContext", "LogTest");
contextLog.Information("This shows up because of the override!");
contextLog.Information("... And this too!");
Log.Information("This does **not** show, because SourceContext != 'LogTest'");
- 1 回答
- 0 關注
- 229 瀏覽
添加回答
舉報
0/150
提交
取消