简体   繁体   中英

ASP.NET Core NLog nlog.config loaded but ignored

I am writing a asp.net core app using NLog.Logging.Extensions to provide logging.

Log Registration:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
    loggerFactory.AddNLog();
    loggerFactory.ConfigureNLog("nlog.config");
    loggerFactory.AddConsole();
    loggerFactory.AddDebug();
    app.UseMvc();
}

I am getting log output, however it doesn't match the format of my logging Layout defined in the .config file, and it doesn't show anything below information (but again, it is configured to show trace and above in the config file).

Is anyone able to shed any light as to why this may be occurring?

nlog.config:

<?xml version="1.0" encoding="utf-8"?>
<nlog>
    <variable name="Layout" value="${longdate} ${level:upperCase=true} ${message} (${callsite:includSourcePath=true})${newline}${exception:format=ToString}"/>
    <targets>
        <target name="debugger" type="Debugger" layout="${Layout}" />
        <target name="console" type="ColoredConsole" layout="${Layout}" detectConsoleAvailable="False"/>
    </targets>
    <rules>
        <logger name="*" minlevel="Trace" writeTo="debugger,console" />
    </rules>
</nlog>

Example Log Output:

Hosting environment: Development 
Content root path: /Users/###/dev/###/Services/src/app/###/### 
Now listening on: http://localhost:8888 Application started. 
Press Ctrl+C to shut down. 
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
          Request starting HTTP/1.1 GET http://localhost:8888/State
info: ###.###.###[0]
          Building ### instance.

There are several problems here.

1. You get the log output because you've attached default .NET Core loggers:

loggerFactory.AddConsole();
loggerFactory.AddDebug();

And this is why the output doesn't match the format of your Layout. Do not add the default loggers if you are going to use only NLog. Then, keep these two lines below:

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

2. NLog config is broken. <add assembly="NLog.Web.AspNetCore"/> is missing. Moreover, it looks like the Debugger target is breaking something in NLog.

There is a fully-workable nlog.config below:

<?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">

  <!-- Load the ASP.NET Core plugin -->
  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>

  <variable name="Layout"
            value="${longdate}|${level:uppercase=true}|${logger}|${message}"/>

  <targets>
    <target name="console" 
            type="ColoredConsole"
            layout="${Layout}"
            detectConsoleAvailable="False"/>
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="console" />
  </rules>
</nlog>

Additional examples: https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-(project.json)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM