1.安裝NLog? 使其集成到VS 里??下載地址: http://nlog-project.org/
2.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"><!-- make sure to set 'Copy To Output Directory' option for this file --><!-- go to http://nlog-project.org/wiki/Configuration_file for more information --><targets><target name="file" xsi:type="File" fileName="${basedir}/log/${shortdate}.txt"layout="${longdate} ${stacktrace} ${message}"/></targets><rules><logger name="*" minlevel="Debug" writeTo="file" /></rules> </nlog>
?
3.創建
private static Logger logger = LogManager.GetCurrentClassLogger();
4.記錄錯誤信息
每條跟蹤信息都包含一個記錄等級(log level)信息,用來描述該條信息的重要性。NLog支持如下幾種記錄等級:
- Trace - 最常見的記錄信息,一般用于普通輸出
- Debug - 同樣是記錄信息,不過出現的頻率要比Trace少一些,一般用來調試程序
- Info - 信息類型的消息
- Warn - 警告信息,一般用于比較重要的場合
- Error - 錯誤信息
- Fatal - 致命異常信息。一般來講,發生致命異常之后程序將無法繼續執行。
例如:
try{int i = 9;int j = 0;int y = i / j; //試圖除以0
int[] arr = {2,5,5,8,6,9,3,7 };for (int n = 0; n < 12; i++)//索引超出了數組界限
{int m = arr[n];}}catch (Exception ex){logger.Error(ex.Message);Response.Write("Error");}
更多內容參考:http://www.cnblogs.com/dflying/archive/2006/12/05/583071.html