gpt4 book ai didi

logging - 始终使用 serilog 记录上下文数据

转载 作者:行者123 更新时间:2023-12-05 00:16:15 26 4
gpt4 key购买 nike

我正在使用 Serilog 进行日志记录。对于每个日志,我想记录上下文信息,如用户名和其他一些上下文信息。所以我用静态方法创建了包装器,如下所示

public static class MyLogger
{
public static void Error(Exception ex, string messageTemplate, params object[] propertyvalues)
{
var properties = new List<object>(propertyvalues);
if (HttpContext.Current != null && HttpContext.Current.User != null && HttpContext.Current.User.Identity != null)
{
var contextInfo = new
{
UserName = HttpContext.Current.User.Identity.Name
};
messageTemplate += "{@ContextInfo}";
properties.Add(contextInfo);
}

//serilog log
Log.Error(ex, messageTemplate, properties.ToArray());
}
}

然后将错误记录为
      MyLogger.Error(exception,"{@SomeMetadata}",metadata);

这是有效的,但是有没有更好的方法来包含带有 serilog 的上下文信息

更新 1

所以我根据建议创建了一个丰富的
public class HttpContextEnricher: ILogEventEnricher
{
LogEventProperty _cachedProperty;

public const string EnvironmentUserNamePropertyName = "UserName";

public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
_cachedProperty = _cachedProperty ?? propertyFactory.CreateProperty(EnvironmentUserNamePropertyName, GetUserName());
logEvent.AddPropertyIfAbsent(_cachedProperty);
}

private string GetUserName()
{
if (HttpContext.Current != null && HttpContext.Current.User != null && HttpContext.Current.User.Identity != null)
{
return HttpContext.Current.User.Identity.Name;
}

return string.Empty;
}
}

但是我如何调用它?其中 With我应该使用的方法?
  Log.Logger = new LoggerConfiguration()
.Enrich.With???????
.ReadFrom.AppSettings()
.CreateLogger();

更新 2
我创建了扩展方法,然后在日志配置期间使用它
public static class HttpContextLoggerConfigurationExtensions
{
public static LoggerConfiguration WithUserName(
this LoggerEnrichmentConfiguration enrichmentConfiguration)
{
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
return enrichmentConfiguration.With<HttpContextEnricher>();
}
}

然后在 Application_Start 事件中的 global.asax 中配置记录器
 Log.Logger = new LoggerConfiguration()
.Enrich.WithUserName()
.ReadFrom.AppSettings()
.CreateLogger();

我注意到每次我记录一些东西时,丰富器都会被调用并返回用户名,但它没有在日志中记录用户名。我在日志中看到我的消息,但没有看到 UserName 属性。我正在使用 Windows EventLog Sink

我将信息记录为
       Log.Information("Some message");

和错误为
       Log.Error(exception, "ErrorID={ErrorID}",someid);

我错过了什么?

最佳答案

使用 Serilog 有几种不同的方法可以做到这一点。您可以使用 Enricher您在应用程序开始时配置的(配置日志记录时),它会在您记录消息时自动调用,您可以将所需的其他属性添加到日志上下文。

另一种方法是在每个请求开始时 Hook 由您的 Web 应用程序框架调用的事件,然后将属性添加到日志上下文。

另一种方法是在记录器实例由您的 DI 容器解析的那一刻添加上下文属性(如果您正在使用一个)。

  • 浓缩 sample

  • https://github.com/serilog-web/owin/blob/master/src/SerilogWeb.Owin/Owin/RequestContextMiddleware.cs

    https://github.com/serilog-web/classic/blob/master/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestClientHostIPEnricher.cs

  • 上下文推送属性示例

  • https://web.archive.org/web/20171207095449/http://mylifeforthecode.com/enriching-serilog-output-with-httpcontext-information-in-asp-net-core/

  • Autofac Serilog 集成

  • https://github.com/nblumhardt/autofac-serilog-integration

    关于logging - 始终使用 serilog 记录上下文数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42378787/

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