gpt4 book ai didi

c# - 默认情况下阻止 Serilog 写入 HTTP 事件

转载 作者:行者123 更新时间:2023-12-04 01:15:28 27 4
gpt4 key购买 nike

我是 injecting typed HTTP clients在 .NET Core 应用程序中。我也在用 Serilog 记录。我没有故意配置任何 HTTP 事件的日志记录。它神奇地随之而来。我该如何关闭它?

[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 ),但一种途径是实现 ILogEventFilter接口(interface)(source)。包括允许事件的逻辑,并插入配置。
示例过滤器类:

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) 

关于c# - 默认情况下阻止 Serilog 写入 HTTP 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63511789/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com