Log4Net Logging Exceptions
I know I can log exceptions to the database using Log4Net, but I want to break up what it stores for an exception into different columns.
Does anyone know if this is available, if anyone has done this or is it just easier to log exceptions to the database manually?
Cheers Anthony
UPDATE:
If it helps maybe use the following:
- Does anyone know if this is available?
- These are the sort of details I am after:
- ExceptionType
- ExceptionMessage
- ExceptionSource
- ExceptionTargetSite
- ExceptionStackTrace
- These are the sort of details I am after:
- If anyone has done this?
- Is it just easier to log exceptions to the database manua开发者_运维问答lly?
Maybe the change in format will help.
As requested, you can use the approach outlined here: Log4Net available database fields for adoappender - seems there are a few more i.e. method_name? to use custom properties in the log4net ThreadContext
to possibly save this extra information about the exceptions in something like the following:
public void LogDetailedException (LogLevel level, string message, Exception exception)
{
log4net.ThreadContext.Properties["exceptionType"] = exception.GetType().AssemblyQualifiedName;
// appropriate logging statement
log4net.ThreadContent.Properties.Remove("exceptionType"); // clear it, so it's not used in future calls
}
And then in your pattern:
<conversionPattern value="%property{exceptionType}" />
It's worth testing/researching to see if this is thread-safe (the name would imply that it is, but it can't hurt to check). You'll also want to make sure subsequent logs don't include this data in their messages (i.e, make sure it's cleared).
(marked was wiki as this isn't specifically my answer, just a gathering of various info and discussion in the comments on Mitch's answer).
If you don't want to use something like ELMAH, a dodgy way might be catching all exceptions by overriding Page.ProcessRequest (dodgy because it is marked for infrastructure use only)
public override void ProcessRequest(HttpContext context)
{
try
{
base.ProcessRequest(context);
}
catch (Exception e)
{
log.ErrorFormat(e.Message); // Log exception message, or
log.ErrorFormat(e.ToString()); // Log stack trace
throw;
}
}
ELMAH is a great pick. A less known library is CuttingEdge.Logging. Just as ELMAH, it allows logging the exception type, message and stack trace to a table in the database.
Use the AdoNetAppender Class, or that doesn't meet your needs, write your own custom Appender.
精彩评论