我使用Serilog作为Microsoft.Extension.Logging的提供程序,我不希望它接管所有日志记录控制。但是,Serilog没有响应该配置。我不支持默认级别,也不支持其自己的提供程序别名“ Serilog”的默认级别。
包裹
<PackageReference Include="Serilog.Extensions.Logging" Version="2.0.4" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.Debug" Version="1.0.1" />
Program.cs
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.WriteTo.Console(outputTemplate: "{Timestamp:HH:mm:ss.fff} [{Level:u3}] {SourceContext} - {Message:lj}{NewLine}{Exception}")
.WriteTo.Debug()
.CreateLogger();
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseApplicationInsights()
.UseStartup<Startup>()
.ConfigureLogging(logging =>
{
logging.AddSerilog(dispose: true);
})
.Build();
appsettings.json
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
},
"Serilog": {
"LogLevel": {
"Default": "Warning"
}
}
}
答案 0 :(得分:-4)
调用logging.AddSerilog(dispose: true);
还添加了一个级别为Trace的过滤器,该过滤器将覆盖Logging配置。手动添加Serilog提供程序,并以最低级别的Verbose配置Serilog,这样您就可以忘记此可怕的贪婪库的配置,并将其与其他任何Logging提供程序一样使用。
Program.cs
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseApplicationInsights()
.UseStartup<Startup>()
.ConfigureLogging((builderContext, logging) =>
{
var logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Console(outputTemplate: "[{Level:w4} {Timestamp:HH:mm:ss.fff} {SourceContext}]{NewLine} {Message:lj}{NewLine}{Exception}")
.WriteTo.Debug(outputTemplate: "[{Timestamp:HH:mm:ss.fff} {SourceContext} {Level:u3}] {Message:lj}{NewLine}{Exception}")
.CreateLogger();
logging.Services.AddSingleton<ILoggerProvider, SerilogLoggerProvider>(services => new SerilogLoggerProvider(logger, true));
})
.Build();
}