I want to write the stack 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...
To print a stack trace to log you Should declare logger and method info(e. toString()) or log(Level.INFO, e. toString()). Logging is the process of writing log messages during the execution of a program to get error and warning messages as well as info messages.
Logging the stack traces of runtime exceptions assists developers in diagnosing runtime failures. However, unnecessary logging of exception stack traces can have many negative impacts such as polluting log files.
It is recommended for you to use specific exceptions when declaring catch and throw clauses. For debugging purposes, you should ensure that you log your exceptions.
Initially, developers should look in the application logs for the stack trace, because often, the stack trace tells you the exact line or function call that caused a problem.
you can use the following layout:
${longdate}|${level}|${message} ${onexception:${exception:format=tostring} | ${stacktrace}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With