Kri*_*Sik 3 c# logging overriding serilog log-level
尝试为 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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
和appsettings.json文件:
{
"Serilog": {
"Using": [ "Serilog.Sinks.Console" ],
"MinimumLevel": {
"Default": "Warning",
"Override": {
"LogTest": "Information"
}
},
"WriteTo": [
{ "Name": "Console" }
]
}
}
Run Code Online (Sandbox Code Playgroud)
期望看到所有日志,从Information开始,但只从Warning得到。
Cai*_*ete 11
根据您的配置,您正在覆盖从名为 的a 发送的MinimumLevel 唯一日志消息....SourceContextLogTest
"Override": {
"LogTest": "Information"
}
Run Code Online (Sandbox Code Playgroud)
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'");
Run Code Online (Sandbox Code Playgroud)
您可以SourceContext在文档中阅读更多信息:https :
//github.com/serilog/serilog/wiki/Writing-Log-Events#source-contexts
| 归档时间: |
|
| 查看次数: |
7940 次 |
| 最近记录: |