0

Possible Duplicate:
How to get the Stack trace when logging exceptions with NLog?

What is the best practice to throw exception to get a clear picture from the log?

For logging, I am using NLog.

Below is the simple code:

catch (Exception ex)
{
    logger.Fatal(ex.Message);
    throw new Exception(ex.Message);
}

It does not give me a good logging message. I need info like, the function, the error code line.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Alvin
  • 8,219
  • 25
  • 96
  • 177

2 Answers2

5

For loggin purpose you are going analyse much more than just message field of exception object.

This link will give you info of what you can get from generic Exception object

Also, there is a difference between throw ex and throw. And moreover when you throw newly created exception.

  • In your case ( throw new Exception(ex.Message) ) you are throwing generic exception that doest say anything about nature of exception and has new stacktrace, build from this line of code.
  • throw ex - rethrows original exception, but cut stacktrace to current catch clause

  • throw - rethrows original exception with original stacktrace allowing you to log etc exception and let it go further

So depending on what you are going to achieve one of this three cases would meet your needs.

vittore
  • 17,449
  • 6
  • 44
  • 82
0

You can read more about best practice of exception throwing from http://msdn.microsoft.com/en-us/library/system.exception_properties.aspx