NLog.Web.AspNetCore布局渲染器为空

时间:2019-04-19 10:11:06

标签: asp.net-core asp.net-core-mvc nlog

我正在开发ASP.net核心(2.2)MVC应用程序,但无法使日志记录正常工作。 AspNetCore布局渲染器返回空值。我安装了以下软件包:

    <PackageReference Include="NLog" Version="4.6.2" />
    <PackageReference Include="NLog.Extensions.Logging" Version="1.4.0" />
    <PackageReference Include="NLog.Web.AspNetCore" Version="4.8.1" />

nlog.config:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogLevel="Trace"
      internalLogFile="c:\temp\internal-nlog.txt">

  <!-- enable asp.net core layout renderers -->
  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>

  <!-- the targets to write to -->
  <targets>
    <!-- write logs to file  -->
    <target xsi:type="File" name="allfile" fileName="c:\temp\nlog-all-${shortdate}.log"
            layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />

    <!-- another file log, only own logs. Uses some ASP.NET core renderers -->
    <target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-api-${shortdate}.log"
            layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
  </targets>

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

    <!--Skip non-critical Microsoft logs and so log only own logs-->
    <logger name="Microsoft.*" maxlevel="Info" final="true" />
    <logger name="IdentityServer4.*" maxlevel="Info" final="true" />
    <!-- BlackHole without writeTo -->
    <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
  </rules>
</nlog>

Startup.cs

services.AddLogging(builder =>
            {
                builder.ClearProviders();
                builder.SetMinimumLevel(LogLevel.Trace);
                builder.AddNLog();
            });

日志:

2019-04-19 11:55:01.2521||INFO|MyApp.API.Controllers.ConfigController| |url: |action: 

1 个答案:

答案 0 :(得分:0)

简单的方法就是按照wiki指南,并致电UseNLog()

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

但是,如果您喜欢以自己的方式来做事情,并且像在ASP.NET Core ver。 1:

https://github.com/NLog/NLog/issues/2859#issuecomment-415865357