尝试设置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" }
]
}
}
期望从信息开始查看所有日志,而只能从警告获取。
答案 0 :(得分:3)
根据您的配置,您正在覆盖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'");
您可以在文档中阅读有关SourceContext
的更多信息:
https://github.com/serilog/serilog/wiki/Writing-Log-Events#source-contexts