2016-08-18 105 views
2

我有一些我在工作中編寫的控制檯應用程序。我想讓NLog進入他們但我有麻煩。 當我檢查'logger'對象時,我看到它的'Factory'屬性,配置的目標爲0,loggingrules = 0,一切都爲空或未設置。 因此,它不會做任何事情..不要刪除內部日誌文件...我已經嘗試nLog.config NLog.config和nlog.config ...無濟於事。試圖第四版和NLOG過的4 ...登錄.NET 4.5控制檯應用程序:不加載配置

爲什麼會不會拿起配置?

我:

  • NLog.config根與構建行動「內容」和「一直拷貝」
  • 證實NLog.config被複制到bin screenshot1
  • 設置

這裏的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" 
     throwConfigExceptions="true" 
     throwExceptions="true" 
     internalLogLevel="Trace" 
     internalLogFile="c:\temp\NlogInternal.log" 
     internalLogToConsole="true" 
     internalLogToConsoleError="true" 
     internalLogToTrace="true"> 
    <targets> 
    <target xsi:type="Console" name="debugConsole" layout="${message} "/> 
    <target xsi:type="File" name="debugFile" createDirs="true" fileName="c:\temp\testlog.log" layout="${longdate} ${uppercase:${level}} ${message}"/> 
    </targets> 
    <rules> 
    <logger name="*" minlevel="Trace" writeTo="debugConsole"/> 
    <logger name="*" minlevel="Trace" writeTo="debugFile"/>  
    </rules> 
</nlog> 

和最後(這不會錯誤,但沒有輸出,因爲配置爲空):

private static Logger logger = LogManager.GetCurrentClassLogger(); 
logger.Info("ConsoleApp test log..."); 
+0

我沒有解決方案,但您的配置文件看起來不錯。我替換了我的一個配置文件(名爲「Nlog.config」)的內容,它的工作原理與您所期望的完全相同,將大量信息轉儲到控制檯並按照指示寫入信息日誌.log文件。我也在該項目中使用完全相同的logger.Info方法,並且我有相同的構建操作集。 – Micah

回答

2

您的app.config是否有NLog configSection? 事情是這樣的:

<configuration> 
    <configSections> 
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/> 
    </configSections> 
    <nlog> 
    </nlog> 
</configuration> 
+0

不,因爲我正在使用外部NLog.config文件。當你使用外部你不需要按照https://github.com/nlog/NLog/wiki/Configuration-file#configuration-file-locations –

+0

@Beau來觸摸app.config,執行nloger.config IS是應用程序的根目錄還是應用程序的bin? (像這樣:project/bin/bin) –

+0

在我的解釋中:「應用程序根目錄設置爲'複製'(所以它也複製到bin中) –

0

如果連internalLogger不工作,你可以通過設置 的InternalLogger.LogWriter

例如調試問題

// enable internal logging to a custom TextWriter 
    InternalLogger.LogWriter = new StringWriter(); //e.g. TextWriter writer = File.CreateText("C:\\perl.txt") 
相關問題