防止 Serilog 默认写入 HTTP 事件
Prevent Serilog from writing HTTP events by default
我 injecting typed HTTP clients 在 .NET Core 应用程序中。我也在用 Serilog 登录。我没有特意配置 HTTP activity 的任何日志记录。它神奇地来了。我该如何关闭它?
[12:00:52 INF] Start processing HTTP request POST https://foo/bar
[12:00:52 INF] Sending HTTP request POST https://foo/bar
[12:00:53 INF] Received HTTP response after 707.8906ms - OK
我的 HTTP 客户端配置:
services.AddHttpClient("FOO")
.ConfigureHttpClient(client =>
{
client.BaseAddress = new Uri("https://foo/");
})
.ConfigurePrimaryHttpMessageHandler(sp => new HttpClientHandler()
{
Credentials = new NetworkCredential("user", "pass"),
ServerCertificateCustomValidationCallback = (senderC, cert, chain, sslPolicyErrors) => true
})
.AddTypedClient<Thing1>()
.AddTypedClient<Thing2>();
我 运行 参与其中,并且能够使用 Serilog 的 Filter
配置进行寻址。不确定记录在何处 (this appears to be related), but one avenue is to implement the ILogEventFilter
interface (source)。包括允许事件的逻辑,并插入配置。
示例过滤器 class:
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);
}
已添加到配置(在本例中为 Program.cs):
config
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
.Enrich.FromLogContext()
.Filter.With<MyLoggingFilter>() // <-- Plug in your filter type
.WriteTo.Console(theme: AnsiConsoleTheme.Code)
.WriteTo.Debug()
// etc
或者,如果您想阻止与 HttpClient 相关的任何日志记录,请使用所需的日志级别覆盖类别(即类型命名空间):
config.MinimumLevel.Override("System.Net.Http.HttpClient", LogEventLevel.Warning)
另一种选择是将 System.Net.Http.HttpClient
的覆盖添加到 appsettings.json
:
我 injecting typed HTTP clients 在 .NET Core 应用程序中。我也在用 Serilog 登录。我没有特意配置 HTTP activity 的任何日志记录。它神奇地来了。我该如何关闭它?
[12:00:52 INF] Start processing HTTP request POST https://foo/bar
[12:00:52 INF] Sending HTTP request POST https://foo/bar
[12:00:53 INF] Received HTTP response after 707.8906ms - OK
我的 HTTP 客户端配置:
services.AddHttpClient("FOO")
.ConfigureHttpClient(client =>
{
client.BaseAddress = new Uri("https://foo/");
})
.ConfigurePrimaryHttpMessageHandler(sp => new HttpClientHandler()
{
Credentials = new NetworkCredential("user", "pass"),
ServerCertificateCustomValidationCallback = (senderC, cert, chain, sslPolicyErrors) => true
})
.AddTypedClient<Thing1>()
.AddTypedClient<Thing2>();
我 运行 参与其中,并且能够使用 Serilog 的 Filter
配置进行寻址。不确定记录在何处 (this appears to be related), but one avenue is to implement the ILogEventFilter
interface (source)。包括允许事件的逻辑,并插入配置。
示例过滤器 class:
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);
}
已添加到配置(在本例中为 Program.cs):
config
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
.Enrich.FromLogContext()
.Filter.With<MyLoggingFilter>() // <-- Plug in your filter type
.WriteTo.Console(theme: AnsiConsoleTheme.Code)
.WriteTo.Debug()
// etc
或者,如果您想阻止与 HttpClient 相关的任何日志记录,请使用所需的日志级别覆盖类别(即类型命名空间):
config.MinimumLevel.Override("System.Net.Http.HttpClient", LogEventLevel.Warning)
另一种选择是将 System.Net.Http.HttpClient
的覆盖添加到 appsettings.json
: