2016-11-14 99 views
0

我在我的Asp.net核心應用程序使用NLOG到文件,但NLOG不能在文件中寫,我有文件都準備好了變更許可,但仍面臨proble。第一次創建文件,但無法寫入該文件中的日誌。NLOG不寫在Asp.net核心

這是我下面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="Warn" 
     internalLogFile="C:\git\damienbod\AspNetCoreNlog\Logs\internal-nlog.txt"> 

    <targets> 
     <target xsi:type="File" name="allfile" fileName="D:\nlog-all.txt" 
        layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" 
        keepFileOpen="false"/> 
     <target xsi:type="EventLog" 
      name="String" 
      layout="Layout" 
      machineName="String" 
      source="Layout" 
      category="Layout" 
      eventId="Layout" 
      log="String" 
      maxMessageLength="Integer" /> 
    </targets> 

    <rules> 
     <!--All logs, including from Microsoft--> 
     <logger name="*" minlevel="*Info*" maxLevel="*Deubg*" writeTo="allfile" /> 
    </rules> 
    </nlog> 

這裏是我的控制器代碼

NLog.Logger _Logger= LogManager.GetCurrentClassLogger(); 
      _Logger.Info("Hello i m executing"); 
+0

我根據您的建議進行了修改@ FORCE JB,minlevel =「調試」但仍面臨同樣的問題 –

+0

internalLogFile中是否有任何錯誤消息?似乎沒有其他不正確的設置,我有一個可行的NLog.config [github](https://github.com/KarateJB/Angular2.Mvc/blob/master/Angular2.Mvc/src/Angular2.Mvc.Website/NLog。配置),希望它有幫助! –

回答

0

我認爲這是因爲「信息」級實際上是具有較高的優先級比「調試」。像這樣更改配置可能會起作用。

<logger name="*" minlevel="Debug" maxLevel="Info" writeTo="File" /> 

參考:NLOG Configuration

0

我覺得你的配置文件應該這樣。

<?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="Warn" 
     internalLogFile="C:\git\damienbod\AspNetCoreNlog\Logs\internal-nlog.txt"> 

    <!-- define various log targets --> 
    <targets> 
    <!-- write logs to file --> 
    <target xsi:type="File" name="allfile" fileName="D:\nlog-all.txt" 
       layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" keepFileOpen="false"/> 

    </targets> 

    <rules> 
    <logger name="*" minlevel="Debug" maxlevel="Info" writeTo="allfile" /> 
    </rules> 
</nlog> 

我已刪除不需要的部分,也mixlevel和maxlevel通過@The FORCE JB的建議必須指定。

除了這個我也可以看到,你有DEBUG字類型。

0

您需要.NET核心的NLog.Extensions.Logging包(NuGet link)和startup.cs使NLOG:

public void Configure(IApplicationBuilder app, 
         IHostingEnvironment env, ILoggerFactory loggerFactory) 
{ 
    //add NLog to ASP.NET Core 
    loggerFactory.AddNLog(); 

    //configure nlog.config in your project root 
    env.ConfigureNLog("nlog.config"); 

    ... 

欲瞭解更多信息,請參閱GitHub page

+0

我也這樣做了,仍然面臨問題 –

+0

感謝您的支持 –

0

只需點擊此鏈接: https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-(csproj---vs2017)

您必須啓用複製到bin文件夾nlog.config Image copy nlog.config

+0

只有鏈接答案容易被審覈,請從提供的鏈接添加一些解釋。 –

+0

Nlog.config需要在編譯時拷貝到bin文件夾。因此,去nlog.config屬性,並選擇「複製到輸出目錄」部分的選項「總是複製:) :)希望這有助於和比以前的答案更清晰 –

+0

謝謝,但我已經解決了這一點。 –