【问题标题】:How to configure nlog.config to include asp.net core 2 web api request custom header information?如何配置 nlog.config 以包含 asp.net core 2 web api 请求自定义标头信息?
【发布时间】:2018-03-11 00:45:31
【问题描述】:

我有一个简单的 asp.net 核心 web api 应用程序(使用 Visual Studio 2017 中的默认 web api 模板)使用 nlog 以 JSON 格式将消息记录到控制台。

  • ASP.NET Core 2:2.0.5
  • NLog 版本:4.5.0-rc07
  • NLog.Web.AspNetCore: 4.5.0-rc03

这里是 NLog 配置文件:

 <!-- the targets to write to -->
 <targets>
    <target xsi:type="Console" name="console_log" >
      <layout xsi:type="JsonLayout">
        <attribute name="ts" layout="${date}" />
        <attribute name="level" layout="${level:upperCase=true}"/>
        **<attribute name="all-events" layout="${all-event-properties}"/>
        <attribute name="tid" layout="${aspnet-request:header=tid}"/>**
        <attribute name="cn" layout="${callsite}"/>
        <attribute name="msg" layout="${message}" />
      </layout>
    </target>
  </targets>

  <!-- rules to map from logger name to target -->
  <rules>
    <!--All logs, including from Microsoft-->
    <logger name="*" minlevel="Debug" writeTo="console_log" />
  </rules>
</nlog>

在 Startup.cs 中,Configure 方法包含以下两行:

        env.ConfigureNLog("nlog.config");
        loggerFactory.AddNLog();

我将 JSON 格式的日志消息输出到控制台。但是,我无法接收到 tid,它是 HTTP 请求标头中的自定义标头。

如何提取 HTTP 请求标头并将其输出到控制台?

感谢您的 cmets/answers/指导。谢谢。

【问题讨论】:

  • 问题是在 nlog.config 中缺少 NLog.Web.AspNetCore 扩展。

标签: c# asp.net-core nlog


【解决方案1】:

${aspnet-request} 依赖于HttpContextAccessor。请更新您的代码以在 IWebHostBuilder 上使用 UseNLog(请参阅本示例中的 program.cs)

https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2#4-update-programcs

UseNLog会自动注册HttpContextAccessor(记得去掉ConfigureNLogAddNLog的任何用法,但只能使用UseNLogLogManager.LoadConfiguration,如上例所示)

【讨论】:

  • 我按照 URL 链接设置 NLog。谢谢你。但是,我的主要挑战是如何配置 nlog.config 条目以显示自定义标头,而不显式提取请求处理管道中的信息并在可能的情况下设置日志记录上下文。如果可能,nlog.config 中的以下行是否正确?
  • @PaulS UseNLog 将授予对 HttpContextAccessor 的访问权限,这将授予对 HttpRequest.Headers 的访问权限,可以使用 ${aspnet-request:header=tid} 进行记录。但在您的解决方案中,我不是专家。听起来您随意拼凑了一些与 Wiki 推荐的不符的东西。也许您需要检查 NLog 内部记录器是否有任何警告。
  • 感谢您对检查 NLog 内部记录器的帮助和建议。我错过了 nlog.config 中的以下内容。
【解决方案2】:

您可以像这样使用ActionFilterAttribute

public class HttpHeadersLogger : ActionFilterAttribute
{
    private readonly Logger _log = LogManager.GetCurrentClassLogger();

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var request = filterContext.HttpContext.Request;
        var builder = new StringBuilder();

        builder.Append(request.RequestType + " " + request.Url + "\n");

        var logEventInfo = new LogEventInfo(LogLevel.Info, null, null);
        var parsed = HttpUtility.ParseQueryString(request.Headers.ToString());

        foreach (string key in parsed)
        {
            // Search for 'tid' header here to only log these one
            logEventInfo.Properties.Add(key, parsed[key]);
        }

        _log.Log(logEventInfo);

        base.OnActionExecuting(filterContext);
    }
}

在这个例子中,所有的标题都被记录了,如果你只想要“tid”一个,你应该过滤掉。 这将使用 NLog 布局表达式,通过使用对象 LogEventInfo 来执行日志记录操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-21
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    • 2015-07-27
    • 2019-12-09
    相关资源
    最近更新 更多