2011-07-01 61 views
7

我只想寫堆棧跟蹤,當我有異常文件,目前我不喜歡這樣寫堆棧跟蹤只記錄時拋出異常

layout="${longdate}|${level}|${message} ${exception:format=tostring} | ${stacktrace}" 

所以我一直把它在我的日誌文件。

編輯:

我用這個佈局我所有的記錄,所以,當我沒有任何例外情況我也得到堆棧trace.But我需要它,只有當我有一些例外

時,我有例外,我有如下輸出,這正是我所需要

2011-07-01 22:59:02.3782|Debug|fffffffffffffffffffffffffffff System.Exception: Exception of type 'System.Exception' was thrown. | AppDomain.ExecuteAssembly => AppDomain._nExecuteAssembly => Program.Main 

但無一例外:

2011-07-01 22:57:26.7117|Trace|fffffffffffffffffffffffffffff | AppDomain.ExecuteAssembly => AppDomain._nExecuteAssembly => Program.Main 

但我想只有

2011-07-01 22:57:26.7117|Trace|fffffffffffffffffffffffffffff 

需要想法如何做到這一點?

+0

你會希望有堆棧跟蹤以其他方式? –

+0

你能澄清這個問題嗎?給我們一些背景? –

+0

@ Adrian,新增了一個例子 –

回答

2

是,在NLOG你可以使用不同級別的警告,錯誤信息等,您還可以登錄與ErrorException例外, WarnException,InfoException。 IE

logger.Error("This is an error message"); 

如果要顯示異常,請使用以下內容。

logger.ErrorException("This is an error with an Exception", e); 

更新:

<target name="Errors" xsi:type="File" fileName="${logDirectory}/ErrorLog.txt" layout="${longdate} ${message} ${exception:format=tostring}"/> 

刪除{}棧跟蹤

更新:

Exception e = new Exception(); 
e.StackTrace // <= The exception holds the stack trace 

你會得到異常的堆棧跟蹤。

+0

@Jetho,但我不想在沒有異常時得到堆棧跟蹤 –

+0

如果你使用Error,而不是ErrorException,你將不會得到異常堆棧跟蹤。 – Jethro

+0

還是看看:2011-07-01 23:29:29.4367 |調試| fffffffffffffffffffffffffffffff | AppDomain.ExecuteAssembly => AppDomain._nExecuteAssembly => Program.Main –

13

您可以使用以下佈局:

${longdate}|${level}|${message} ${onexception:${exception:format=tostring} | ${stacktrace}} 
相關問題