开发者

Writing stack trace to log file only when exception thrown

开发者 https://www.devze.com 2023-03-17 05:52 出处:网络
I want to write the sta开发者_如何学运维ck trace only when I have exceptions, currently I do it like this

I want to write the sta开发者_如何学运维ck trace only when I have exceptions, currently I do it like this

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

So I always get it in my log file.

EDIT:

I use this layout for all my logging, so when I dont have any exceptions I also get the stack trace.But I need it only when i have some exception

when I have an exception I have following output, and it what I need

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

But without exception :

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

But I want only

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

Need ideas how to do so...


you can use the following layout:

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


Yes, in NLog you can use different levels Warn, Error Info etc. You can also log the exceptions with ErrorException, WarnException, InfoException. IE

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

If you want to show the exception then use the following.

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

Update :

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

Remove {stacktrace}

Update :

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

You will get the stacktrace from the exception.

0

精彩评论

暂无评论...
验证码 换一张
取 消