尝试设置 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 关注
- 175 浏览
添加回答
举报
0/150
提交
取消