gpt4 book ai didi

c# - Microsoft.AspNetCore.Hosting.Internal.WebHost 和 log4net.LogicalThreadContext.Properties ["requestid"]

转载 作者:行者123 更新时间:2023-11-30 16:41:04 28 4
gpt4 key购买 nike

我使用 netcore2.0 并在我的中间件类中为每个请求设置 log4net.LogicalThreadContext 属性“requestid”以进行日志记录。

log4net.LogicalThreadContext.Properties["requestid"] = Guid.NewGuid().ToString("N");

但是“Microsoft.AspNetCore.Hosting.Internal.WebHost”类在记录行时不显示此属性。

我不知道如何为“Microsoft.AspNetCore.Hosting.Internal.WebHost”设置 log4net.LogicalThreadContext.Propery["requestid"]。

我希望把所有的请求日志从头到尾都绑起来。有没有人有任何可能有帮助的见解?

问候!

更新:这是代码

using Microsoft.Extensions.Logging;
...

public static ILoggerProvider Log4NetProvider;

public static void Main(string[] args)
{

#if DEBUG
Log4NetProvider = new Log4NetProvider("log4net.Debug.config", ((o,
exception) => exception.Message));
#else
Log4NetProvider = new Log4NetProvider("log4net.Release.config", ((o,
exception) => exception.Message));
#endif
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddProvider(Log4NetProvider);
})
.ConfigureServices(services =>
{
services.AddAutofac();
})
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();

在 Startup.cs 中Configure(IApplicationBuilder app, IHostingEnvironment env) 我已经设置为第一件事。

app.UseMiddleware<Log4NetRequestIdMiddleware>();

public class Log4NetRequestIdMiddleware
{
private readonly RequestDelegate _next;

public Log4NetRequestIdMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task Invoke(HttpContext context)
{
log4net.LogicalThreadContext.Properties["requestid"] = Guid.NewGuid().ToString("N");
await _next(context);
}
}

Log4net 配置文件>

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="C:\log\applog.log" />
<rollingStyle value="Size"/>
<maxSizeRollBackups value="500"/>
<maximumFileSize value="5MB"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%utcdate|%-5level|%logger|%property{requestid}| %message %exception%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingFile" />
</root>
</log4net>

来自 Fileappender.log 的附加示例(粗体标记 RequestId)

2018-04-10 14:02:28,664|INFO|Microsoft.AspNetCore.Hosting.Internal.WebHost|(null)| Request starting HTTP/1.1 GET http://localhost...
2018-04-10 14:02:28,956|INFO| Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker|cfb2709e3b4f40559c365ecbb08a7746| Executing action method Controllers.HealtCheckStatusController.GetHealthCheckStatus 2018-04-10 14:02:29,486|INFO| Microsoft.AspNetCore.Mvc.Internal.ObjectResultExecutor|cfb2709e3b4f40559c365ecbb08a7746| Executing ObjectResult, writing value Microsoft.AspNetCore.Mvc.ControllerContext.
2018-04-10 14:02:29,510|INFO| Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker|cfb2709e3b4f40559c365ecbb08a7746| Executed action Controllers.HealtCheckStatusController.GetHealthCheckStatus in 564.464ms
2018-04-10 14:02:29,520|INFO|Microsoft.AspNetCore.Hosting.Internal.WebHost|(null)| Request finished in 858.9575ms 200 application/json; charset=utf-8

最佳答案

为什么 WebHost 记录器不显示属性

WebHost 记录器记录两条消息 - 请求开始消息和请求完成消息。

请求开始消息没有看到 requestid 属性,因为 WebHost 在第一个中间件包含到 Startup 类的管道之前运行并记录消息其中设置了 requestid 属性。

请求完成消息没有看到 requestid 属性,因为(我猜)WebHost 记录器在 log4net.LogicalThreadContext 之外。这就像 Log4NetRequestIdMiddleware 记录器不会看到 requestid 属性,如果它在 Controller 操作中设置的话。

可能的解决方案

自从 WebHost 记录器将消息记录在 HostingApplicationDiagnostics 深处的某处来自 Microsoft.AspNetCore.Hosting.Internal 命名空间的类没有任何明确的扩展点来注入(inject)将设置 requestid 属性的代码。所以,我认为您可以像 WebHost logger 一样在您的中间件中记录信息:

public async Task Invoke(HttpContext context)
{
var logger = httpContext.RequestServices.GetRequiredService<ILogger<Log4NetRequestIdMiddleware>>();
log4net.LogicalThreadContext.Properties["requestid"] = Guid.NewGuid().ToString("N");

var request = context.Request;
string logMsg = string.Format(
CultureInfo.InvariantCulture,
"Request starting {0} {1} {2}://{3}{4}{5}{6} {7} {8}",
request.Protocol,
request.Method,
request.Scheme,
request.Host.Value,
request.PathBase.Value,
request.Path.Value,
request.QueryString.Value,
request.ContentType,
request.ContentLength);

logger.LogDebug(logMsg);

await _next(context);
}

关于c# - Microsoft.AspNetCore.Hosting.Internal.WebHost 和 log4net.LogicalThreadContext.Properties ["requestid"],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49920511/

28 4 0
文章推荐: c# - Akka.Remote 配置失败
文章推荐: javascript - 从外部js文件接收数据