How to include additional information in logging exception using exception handling application block

Topics: Exception Handling Application Block, Logging Application Block
Jun 10, 2009 at 4:37 AM

Hi,

I am new to enterprise library, but I find it really interesting. But now I stuck in one trouble that I can get a good solution. I want to include additional information whenever I log my exception. How can I do this? What I want to accomplish is like this: when an exception occurs, my application will log the exception to a file text, and the information that logged is the standard information in build-in template plus additional information that I can defined in my application code (i.e my application userlogon that rise the exception). Please anyone provide me an example to do this, cause I have googling around and still can find a good walkthrough or example.

Thanks a lot.

Regards.

Jun 10, 2009 at 6:46 AM

Hi,

One thing I know to add additional details to the exception is to explicitly add those information to the "Data" property of the exception. The KeyValuePairs that you've added to the exception will automatically be added as the extended properties of the Log Entry, so when the exception is logged additional information will also get logged and you'll find it in the Extended Properties in the lower part of the log.

Like this:

            try

            {

                throw new Exception();

            }

            catch (Exception ex)

            {

                ex.Data.Add("Additional Details", "Details");

                bool rethrow = ExceptionPolicy.HandleException(ex, "EP2");

                if(rethrow)

                    throw;

            }

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

Jun 10, 2009 at 6:58 AM

Hi, have tried the solution you posted, it didn't work. The object Ex didn't contain any Data property. By the way, I forgot to mention that I am using VS 2003 and Enterprise Library 1.0 (June 2005 release). How do it in VS 2003 and Enterprise Library 1.0? Please help me. Thanks a lot.

Regards

Jun 10, 2009 at 7:31 AM

Hi,

I have tried the solution you posted, it didn't work :(

The Exception object didn't contain any Data property. By the way, I forgot to mention that I am using VS 2003 and Enterprise Library 1.0 (June 2005 release). How to do it in VS 2003 and Enterprise Library 1.0? Please help me. Please reply me ASAP.

Thanks a lot.

Regards

From: AvanadeSupport [mailto:notifications@codeplex.com]
Sent: 10 Juni 2009 13:47
To: Willy Juisan
Subject: Re: How to include additional information in logging exception using exception handling application b... [entlib:59016]

From: AvanadeSupport

Hi,

One thing I know to add additional details to the exception is to explicitly add those information to the "Data" property of the exception. The KeyValuePairs that you've added to the exception will automatically be added as the extended properties of the Log Entry, so when the exception is logged additional information will also get logged and you'll find it in the Extended Properties in the lower part of the log.

Like this:

try

{

throw new Exception();

}

catch (Exception ex)

{

ex.Data.Add("Additional Details", "Details");

bool rethrow = ExceptionPolicy.HandleException(ex, "EP2");

if(rethrow)

throw;

}

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

Read the full discussion online.

To add a post to this discussion, reply to this email (entlib@discussions.codeplex.com)

To start a new discussion for this project, email entlib@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Jun 10, 2009 at 8:21 AM

Hi,

I have figured out how to do it in .NET 1.1 and Enterprise Library 1.0, but still don't know if this is the best pratice or not. What we need to do is just pass the additional data to SetContextItem method in Logger Class from Enterprise Library Logging Application Block dll. I post the code for someone that maybe needed or encounter the same problem with me. Here is the code (in VB, sorry don't have the C# version):

        Try
           Throw New Exception
        Catch Ex As Exception
            Microsoft.Practices.EnterpriseLibrary.Logging.Logger.SetContextItem("LogonUser", "Willy")
            Dim rethrow As Boolean = Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(Ex, "General Exception Policy")

            If (rethrow) Then
                Throw
            End If
        End Try

That's it, nothing more than this. Pretty simple isn't it? Cheers :D

Regards,

Willy Juisan