views:

368

answers:

4

Hi guys

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
  • If anyone has done this?
  • Is it just easier to log exceptions to the database manually?

Maybe the change in format will help.

A: 

Use the AdoNetAppender Class, or that doesn't meet your needs, write your own custom Appender.

Mitch Wheat
Why would this be downvoted? The AdoNetAppender lets you break things up, and if you need more granularity, then you can also follow the advice for a custom appender.
Noon Silk
AdoNetAppender class does not break out exception details. The generic answer "custom Appender" really doesn't help answer the question.
Sam
@Sam thanks, my thoughts exactly
vdh_ant
Sam: It breaks out details of the log; what sort of details are you hiding in an exception 'message' string that you want broken into separate columns? (Seems bad to me). Nevertheless, custom appender is accurate, if not overly detailed. Does everyone need to be spoonfed?
Noon Silk
@Sam: I don't see your better alternative answer anywhere?
Mitch Wheat
@vdh_ant: if you already know the answer to your question, why ask it? Or would you like someone to write the code for you?
Mitch Wheat
@Mitch: I'm sorry to say, but I guess this is just a case of agreeing to disagree. I don't know the answer hence why I asked the question. Its a little frustrating when people answer questions without reading the question (from the votes here it would appear others agree). The question states pretty much what you answered and the answer is quite generic. If you had of said No it doesn't exist, no-one has ever done this and it looks like you will need to create it yourself, then there wouldn't be a problem. But your answer doesn't really help me, maybe other people will find it more helpful.
vdh_ant
vdh: How can one person comment on whether or not the entire population of the world has done something? Isn't it implied in the answer that, as far as researched, it doesn't exist, and you must implement it? You still haven't explained what detail you want to get from the exceptions.
Noon Silk
"How can anyone comment..." That's my point exactly and why I asked the question. Hence the comment that states that "I don't see your better alternative answer anywhere?" doesn't mean an alternative answer is coming. Sorry I didn't get your implication from the answer - and I thank you for all the time an energy you put into looking for an answer (3 minutes), it tool me quite a bit longer to come to the same conclusion and decide to ask combined collective of the masses. I have proved the exception details you where after.
vdh_ant
vdh: Keep in mind that me (silky) and Mitch Wheat (the provider of this answer) are different people :) Following on from your details, it looks like you can do it with the Custom Properties and AdoNetAppender as outlined in this post: http://stackoverflow.com/questions/1324604/log4net-available-database-fields-for-adoappender-seems-there-are-a-few-more-i (Edit: You'll need to change how you log exceptions, obviously, and you'll need to test it a bit to ensure it doesn't get overwritten, but I think it should work; I've not tried it though).
Noon Silk
@silky: thanks for the useful information. I know the custom properties quite well and to use these you have to manually put the data in them in the first place. As far as I know, by default it wouldn't automatically break up the exception, information. I am happy if you want to put your above comment in an answer and I will give it an up vote as its a good idea.
vdh_ant
vdh: No, it won't manually break it out; but you'd write a custom `LogException(string Message, Exception exception)` function that would then call log4net after setting up the properties appropriately (and then your AdoNetAppender would do it's stuff to add it into relevant fields).
Noon Silk
@silky: could you please put what you have in a different answer as I would like to discuss this more and within this answer probably isn't the best place.
vdh_ant
@Mitch Wheat, are you saying that 'cause I don't know of an existing solution to the problem I forfeit the right to point out this proposal is wrong and provide my own comments?
Sam
@Sam: whatever.
Mitch Wheat
+2  A: 

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;
    }
}
Si
Thanks Si... This is really interesting, I hadn't heard of ELMAH before. I was trying to get Log4Net to do my exception logging and not just my logging. But ELMAH could be the way to go.
vdh_ant
+1 for ELMAH, very neat.
Sam
+1  A: 

As requested, you can use the approach outlined here: http://stackoverflow.com/questions/1324604/log4net-available-database-fields-for-adoappender-seems-there-are-a-few-more-i 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).

Noon Silk
Thanks for that. I really appreciate it. For my question this is the best answer, but I will probably turn to ELMAH for my error handling and not use Log4Net.
vdh_ant
+1  A: 

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.

Steven