2017-04-16 136 views
2

我正在使用NLog.Logging.Extensions編寫一個asp.net核心應用程序來提供日誌記錄。ASP.NET Core NLog nlog.config加載但忽略

登錄報名:

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

我越來越日誌輸出,但它不匹配在config文件中定義我的記錄佈局的格式,它並沒有顯示下面的信息什麼(但是,它又被配置爲在配置文件中顯示trace和above)。

是否有人能夠解釋爲什麼這可能會發生?

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> 

示例日誌輸出:

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. 

回答

4

這裏有幾個問題。

你得到日誌輸出,因爲你已經附加默認.NET核心伐木工人:

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

這就是爲什麼日誌不符合您的佈局格式。刪除它,如果你打算只使用NLOG並保持低於只有兩行:

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

2. NLOG配置壞了。缺少<add assembly="NLog.Web.AspNetCore"/>。此外,它看起來像Debugger目標是打破NLog中的東西。有完全可行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"> 

    <!-- 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> 

其他例子:https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-(project.json)

+3

注:調試目標不在netstandard可用,見https://github.com/NLog/NLog/wiki/platform -支持 – Julian