嘗試設(shè)置 Serilog 2.8.0(在 .NET 4.6.2 中)的最低日志級(jí)別。日志記錄工作正常,但無(wú)法覆蓋功能。這是我的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開(kāi)始的所有日志,但只從warning中獲取。
1 回答

瀟湘沐
TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊
根據(jù)您的配置,您將覆蓋MinimumLevel 僅從SourceContext名稱為 發(fā)送的日志消息。LogTest
"Override": {
? "LogTest": "Information"
}
對(duì)您所做的簡(jiǎn)單調(diào)用Log.Information("Information")不會(huì)設(shè)置源上下文,因此覆蓋不適用...您必須首先創(chuàng)建上下文。
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 關(guān)注
- 225 瀏覽
添加回答
舉報(bào)
0/150
提交
取消