无法使用 Serilog 将最低日志记录级别设置为警告 HttpClients
Cannot set minimum logging level to warning for HttpClients using Serilog
我正在尝试使用 HttpClient 将 Serilog 的最低调用级别设置为在使用 Serilog 2.10.0 的 .Net 5 Blazor 应用程序中发出警告。它似乎适用于 EFCore 和其他组件,但不适用于 HTTPClient - 每次进行 HttpClient 调用时,我都会获得信息级别非常详细的日志记录。
我的 Program.cs 文件中的代码如下所示。
public static void Main(string[] args)
{
// see https://nblumhardt.com/2019/10/serilog-in-aspnetcore-3/
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.MinimumLevel.Override("System.Net.Http.HttpClient", LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.File(new CompactJsonFormatter(), "Logs/log-.clef", rollingInterval: RollingInterval.Day)
.WriteTo.Console()
.CreateLogger();
如有任何关于如何解决此问题的建议,我们将不胜感激。
这是一个错误,已在 4.0.0 版本中修复,请参阅:
https://github.com/serilog/serilog-aspnetcore/issues/221
另一种方法是使用 ILogEventFilter:
public class MyLoggingFilter : ILogEventFilter
{
private static readonly HashSet<string> ignoredMessages = new HashSet<string>(StringComparer.Ordinal)
{
"Start processing HTTP request {HttpMethod} {Uri}",
"End processing HTTP request after {ElapsedMilliseconds}ms - {StatusCode}"
};
// Allow the event to be logged if the message template isn't one we ignore
public bool IsEnabled(LogEvent logEvent) => !ignoredMessages.Contains(logEvent.MessageTemplate.Text);
}
和:
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.Filter.With<MyLoggingFilter>() // <-- Plug in your filter type
.Enrich.FromLogContext()
.WriteTo.File(new CompactJsonFormatter(), "Logs/log-.clef", rollingInterval: RollingInterval.Day)
.WriteTo.Console()
.CreateLogger();
我正在尝试使用 HttpClient 将 Serilog 的最低调用级别设置为在使用 Serilog 2.10.0 的 .Net 5 Blazor 应用程序中发出警告。它似乎适用于 EFCore 和其他组件,但不适用于 HTTPClient - 每次进行 HttpClient 调用时,我都会获得信息级别非常详细的日志记录。
我的 Program.cs 文件中的代码如下所示。
public static void Main(string[] args)
{
// see https://nblumhardt.com/2019/10/serilog-in-aspnetcore-3/
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.MinimumLevel.Override("System.Net.Http.HttpClient", LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.File(new CompactJsonFormatter(), "Logs/log-.clef", rollingInterval: RollingInterval.Day)
.WriteTo.Console()
.CreateLogger();
如有任何关于如何解决此问题的建议,我们将不胜感激。
这是一个错误,已在 4.0.0 版本中修复,请参阅:
https://github.com/serilog/serilog-aspnetcore/issues/221
另一种方法是使用 ILogEventFilter:
public class MyLoggingFilter : ILogEventFilter
{
private static readonly HashSet<string> ignoredMessages = new HashSet<string>(StringComparer.Ordinal)
{
"Start processing HTTP request {HttpMethod} {Uri}",
"End processing HTTP request after {ElapsedMilliseconds}ms - {StatusCode}"
};
// Allow the event to be logged if the message template isn't one we ignore
public bool IsEnabled(LogEvent logEvent) => !ignoredMessages.Contains(logEvent.MessageTemplate.Text);
}
和:
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.Filter.With<MyLoggingFilter>() // <-- Plug in your filter type
.Enrich.FromLogContext()
.WriteTo.File(new CompactJsonFormatter(), "Logs/log-.clef", rollingInterval: RollingInterval.Day)
.WriteTo.Console()
.CreateLogger();