using EHAB and LAB to log to a custom table

Topics: Exception Handling Application Block, Logging Application Block
Apr 20, 2009 at 6:49 PM
I managed to get the EAB to use the LAB to log an exception to the database using the code that came with the samples. I now need to change it so that it will use our custom table and stored procedure which are totally different than the sample.

After reading the threads, I think that I need to override the HandleException method, but I'm running into issues.

Here is the new class I created:

[ConfigurationElementType(typeof(CustomHandlerData))]
public class CustomTraceListener : IExceptionHandler

{
    public CustomTraceListener(NameValueCollection collection)
    {
    }

    public Exception HandleException(Exception exception, Guid handlingInstanceId)
    {
        return new ApplicationException(exception.Message, exception);
    }
}

and I handle the exceptions as follows:

        catch(Exception ex)
        {
            bool r = ExceptionPolicy.HandleException(ex, "ADPDS Exception Policy");
            if(r)
                throw;
        }

The problem is that the code "return new ApplicationException(exception.Message, exception);" is not being reached. Am I doing something wrong?

Also, since we are using a totally differtent stored procedure, is this where I need to write my code to call our custom stored procedure?

Thanks
Alex
Apr 20, 2009 at 8:06 PM
I may be barking up the wrong tree here. Looks like a custom trace listener is the way, but the following fails to compile: .Write(string) : no suitable method found to override:

[ConfigurationElementType(typeof(CustomTraceListenerData))]
public class CustomDatabaseTraceListener : CustomTraceListener
{
    public override void Write(string message)
    {
        // Place code here
    }
}
Apr 21, 2009 at 7:00 AM
Hi,

Do you have your references setup properly? I tried your code and seems to be compiling. Did you use the Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.CustomTraceListener abstract class or you defined your own abstract class with a same name? That's the only scenario that I've repro your error.


Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
Apr 21, 2009 at 3:18 PM
I managed to get ti compiled by using the full namespace. Since I was a bit confused, I think I was jumping around too much, so perhaps I should start over again.

I'd like to use both the LAB and the EHAB to log errors in my custom table.

1. I assume I would need to use ExceptionPolicy.HandleException() rather than Logger.Write(), correct? IN other words, I create the following class:

[

ConfigurationElementType(typeof(CustomHandlerData))]
public class CustomExceptionHandler : IExceptionHandler
{
    public CustomExceptionHandler(NameValueCollection collection)
    {
    }
    public Exception HandleException(Exception exception, Guid handlingInstanceId)
    {
        return new ApplicationException(exception.Message, exception);
    }
}

 

But bool r = ExceptionPolicy.HandleException(ex, "ABC"); does not reach the code.

2. I created a custom trace listener. I have the Write and WriteLine overloaded methods. Since our exception handling is not "complicated", we'll probably use the Logger.Write more than ExceptionPolicy.HandleException, but again, once I created the following class:

[

ConfigurationElementType(typeof(CustomTraceListenerData))]
public class CustomDatabaseTraceListener : Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.CustomTraceListener
{
    public CustomDatabaseTraceListener()
    {}
    public override void Write(string message)
    {
        // Place code here
    }
    public override void WriteLine(string message)
    {
        // Place code here
    }
}
Executing Logger.Write(ex.Message);  does not reach the code.
 
Although we don't have a huge requirement to handle exceptions, logging them would be done by the LAB, but I don't want to limit the functionality because we may want to use the EHAB to log the exceptions to the database in the near future.

Thanks
Alex

Apr 22, 2009 at 2:48 AM

Ok, but if you would not be adding any special implementation in the HandleException of your custom exception handler, you can have the same behavior configured in the Exception Handling Block by creating an Exception Policy with an PostHandlingAction of ThrowNewException and adding a Replace Handler.  

Regarding your custom trace listener, you're not encountering any errors now right?    "Executing Logger.Write(ex.Message);  does not reach the code." - which code?  If it's not executing either Write or WriteLine method,  try overriding the TraceData as well and from there, call one of the Write methods.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 23, 2009 at 4:38 PM
Sorry, I had to put some fires out, but now I'm back to this issue. I still understand how or even what I need to do. To start off, how would I do the following:

Use EHAB to catch an exception and log information into a custom table using a custom stored procedure through LAB. A a result, I would need to do both Logger.Write() and ExceptionPolicy.HandleException(). Because my custom table is different than the example, I also need to pass in additional information to the two methods to be written in the table.

I would appriciate it a lot if you could just point me to the right direction.

I thouhgt I needed to do a custom trace listener, but going over the sample, I don't think thi sis what i need. If it is, I'm not sure I understand it enough to change it to use my custom sp and table.

Thank you for your help.
Apr 24, 2009 at 2:48 AM
When logging to a custom database, you definitely have to create a custom trace listener.  The other way to do this is of course modify the source code specifically the FormattedDatabaseTraceListener and the associated stored procedures but most would prefer just extending entlib rather than modify the source code. 

If you need to add aditional information, you can add it to the .Data collection property of the Exception in your custom exception handler.  You can then access that property and put it in the LogEntry's .Extended properties or if you want, you can even create your custom LogEntry.  Override then the .Write(object o)  in your CustomTraceListener.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com