【问题标题】:Any alternatives to aspnet-request:serverVariable when using NLog with .Net Core?将 NLog 与 .Net Core 一起使用时,aspnet-request:serverVariable 的任何替代方案?
【发布时间】:2019-11-17 17:28:40
【问题描述】:

NLog GitHub 所述,.Net Core 不支持${aspnet-request:serverVariable=String} 布局渲染器。
该文档没有提供 serverVariable 下许多可用变量的替代方法。

我的问题是,还有其他选择吗?喜欢访问远程地址、服务器名称、端口等?还是我只需要编写一堆自定义布局渲染器documented here 并手动依赖注入所有东西?

【问题讨论】:

    标签: asp.net-core logging nlog


    【解决方案1】:

    对于 ASP.NET Core,有许多新的布局呈现。原因是 ASP.NET Core 的 API 非常不同,无法像 ASP.NET 中那样读取服务器变量(所以非核心)

    目前有 13 个用于 ASP.NET Core 的布局渲染,用于渲染请求的一部分。

    • ${aspnet-request} - ASP.NET 请求变量。
    • ${aspnet-request-contenttype} - ASP.NET Content-Type 标头(例如 application/json)
    • ${aspnet-request-cookie} - ASP.NET 请求 cookie 内容。
    • ${aspnet-request-form} - ASP.NET 请求表单内容。
    • ${aspnet-request-headers} - ASP.NET 标头键/值对。
    • ${aspnet-request-host} - ASP.NET 请求主机
    • ${aspnet-request-ip} - 客户端 IP。
    • ${aspnet-request-method} - ASP.NET 请求方法(GET、POST 等)。
    • ${aspnet-request-posted-body} - ASP.NET 发布的正文/有效负载
    • ${aspnet-request-querystring} - ASP.NET 请求查询字符串。
    • ${aspnet-request-referrer} - ASP.NET 请求引荐来源网址。
    • ${aspnet-request-url} - ASP.NET 请求 URL。
    • ${aspnet-request-useragent} - ASP.NET 请求用户代理。

    另见https://nlog-project.org/config/?tab=layout-renderers&search=package:nlog.web.aspnetcore

    如果您需要其他东西,您确实可以创建自定义渲染器。如果您需要 http 请求,您可以使用:

    AspNetLayoutRendererBase.Register("aspnet-request-myrenderer", (logevent, httpcontext, config) => ... );
    

    您需要为此参考 NLog.Web.AspNetCore 包。

    【讨论】:

    • 标记你的答案,因为在最后阶段使用 .Net 5,仍然没有其他选择,所以可能永远也不会。因此,如果您遗漏任何内容,您必须创建自定义渲染器并自己检索应用中的信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-16
    • 1970-01-01
    • 1970-01-01
    • 2021-03-10
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    相关资源
    最近更新 更多