EHAB - How to log information that is not in the exception?

Topics: Building and extending application blocks, Exception Handling Application Block, Logging Application Block
Jan 21, 2009 at 9:41 PM
Hi guys,

  I have the following requirement on my exception management:

- I should log the exception in the event viewer(easy, using a Text Formatter Handler)
- Along with the exception, I should also log the unique identifier of the business item which generated the error.

  This second item is where I fall short. I undertand that the formatter can only see the exception properties when logging. How can I include business related information in the internal logs, in order to improve traceability?

  I think I might need a custom handler, but I still don't get how to include those extra items. Do I need to create a new exception class, and handle it?

Cheers, Wagner.
Jan 22, 2009 at 4:05 AM
Hi,

just create your custom exception class with the information that you want to be logged. Like this:

 

public class ExceptionWithOtherInformation : System.Exception
{
public ExceptionWithOtherInformation() : base() { }
public ExceptionWithOtherInformation(int id) : base() {
this.ExceptionId = id;
}
public int ExceptionId { get; set; }
}


Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

 

Jan 22, 2009 at 4:26 AM
Edited Jan 22, 2009 at 4:27 AM

Hi Valiant,

I was tying not to use another Exception, but seems like I will need to create this other exception and multiple policies in order to acomplish what I want. I can see that I will have to do something like this:

1) Create a "Wrapper policy" using a wrapper handler.
2) Create a "Notify Policy" using a log handler
2) Execute 

    ExceptionPolicy.HandleException(ex, "WrapperPolicy", out newException)

3) apply the extra values to newException

4) Execute

    if (ExceptionPolicy.HandleException(ex, "NotifyPolicy"))

    {
        throw
    }

All of that in the catch block. Seems a lot for something that should make the life of handling exceptions easier... :-)

Does this make sense, or you guys can see an easier way?

Jan 22, 2009 at 7:34 AM
Hi,

Yes it does make sense make sense to me. I also tried it. and it works. Here is my sample code:

try

{
throw new System.Exception();
}
catch (System.Exception ex)
{
try
{
bool rethrow = ExceptionPolicy.HandleException(ex, "Wrapper");
if (rethrow)
throw;
}
catch (ExceptionWithOtherInformation exceptionWithOtherInfo)
{
exceptionWithOtherInfo.ExceptionId = 123545;
bool rethrowFinalException = ExceptionPolicy.HandleException(exceptionWithOtherInfo, "NotifyAndLog");
if (rethrowFinalException)
throw;
}
}
}


Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com