2010年3月15日 星期一

Log函式庫 - NLog

下載 NLog-VERSION-setup.exe並安裝。
開啟一個專案並選擇"Add New Item..."選項去加入NLog設定檔。
加入一個空白的NLog設定檔。在NLog設定檔的屬性中,將屬性""Copy to Output Directory"的值改為"Copy always"。
在專案中加入一個名為"Program.cs"的檔案,內容如下:

using System; 
using System.Collections.Generic; 
using System.Text; 
using NLog; 

namespace NLogExample 
{ 
    class Program 
    { 
        private static Logger logger = LogManager.GetCurrentClassLogger(); 
        
        static void Main(string[] args) 
        { 
            logger.Debug("Hello World!"); 
        } 
    } 
}

NLog.config
<br /><nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" <br />      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <br />  <targets> <br />    <target name="console" xsi:type="ColoredConsole" <br />            layout="${date:format=HH\:mm\:ss}|${level}|${stacktrace}|${message}"/> <br />    <target name="file" xsi:type="File" fileName="${basedir}/file.txt" <br />            layout="${stacktrace} ${message}"/> <br />  </targets> <br />  <rules> <br />    <logger name="*" minlevel="Trace" writeTo="console,file"/> <br />  </rules> <br /></nlog> <br />

參考Log等級
Trace - Very detailed log messages, potentially of a high frequency and volume
Debug -Less detailed and/or less frequent debugging messages
Info - Informational messages
Warn - Warnings which don't appear to the user of the application
Error - Error messages
Fatal - Fatal error messages. After a fatal error the application usually terminates.
Reference
Open source logging tools in C#
NLog-Advanced .NET Logging

參考網址

沒有留言: