WCF Exception Shielding and Logging Custom Fault Exception

Topics: Exception Handling Application Block, Logging Application Block
Sep 7, 2010 at 6:04 AM

Hi,

My WCF service uses ExceptionShielding from Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF. The custom fault exception is caught at client side as

            try 
            { 
              //Service call 
            } 
            catch (FaultException<ServiceReference1.ServiceFault> ex) 
            {                  ServiceReference1.ServiceFault fault = ex.Detail; 
                bool rethrow = ExceptionPolicy.HandleException(ex, "ExceptionPolicy"); 
            } 

Now the server exception is in the form of fault exception (ex.Detail). What is the right pattern to use enterprise exception logging application block to log the custom fault exception.

  • Jyothish George

 

Sep 7, 2010 at 7:07 AM

Hi Jyothish George,

I'm not sure if I got your question right, please let me know if my understanding is correct. Basically what you would want to know is how to log an exception, is that right? If yes then here is what I think can help. As long as it is an Exception type then you're good of logging it, all you need to have is to configure your exception policy to use a logging handler. You can check the documentation as reference http://msdn.microsoft.com/en-us/library/ff664711(v=PandP.50).aspx. Hope this helps.

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Sep 7, 2010 at 7:24 AM

Hi Gino,

 Thanks for your response. Let me explaing my scenario in detail. The service exceptions are mapped to a custom fault exception using ExceptionShielding. At the client side the custom fault exception are caught and which is available in ex.Detail as the above snippets. The custom fault class holds the Id, Message and StackTrace from the service. Now I want the Message and StackTrace to be logged as part of caught exception at the client side. I don’t log exception at the service side; instead I would like to log the exception at the client side. The text formatter of logging module cannot format my custom fault (obviously). What do you suggest the best way to log my custom fault exception at the client side?

 

-Jyothish George

Sep 7, 2010 at 9:44 AM

Hello Jyotihsh George,

Thanks for the explanation. I think one simple approach you can use is to add your custom fault properties in the exception Data property before the actual handling (using the exception block). Your code may look something like the following below.

            try
            {
                Service.IService1 service = new Service.Service1Client();
                service.GetData(1);
            }
            catch (FaultException<Service.CompositeType> ex)
            {
                ex.Data.Add("ID", ex.Detail.ID);
                ex.Data.Add("Message", ex.Detail.Message);
                ex.Data.Add("Custom StackTrace", ex.Detail.CustomStackTrace);
                ExceptionPolicy.HandleException(ex, "Policy");
            }

Then make sure in your Logging App Block section specifically in the Text Formatter that the Extended Properties: {dictionary({key} - {value}{newline})} token is included. With this, you should now be able to log your custom fault properties as part of the log entry. Hope this helps, let us know if you may have questions.

 

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Sep 7, 2010 at 9:53 AM

Hi Gino Terrado,

 

Okay, is this the only pattern by which we can log custom fault exceptions on client side? It requires re-factoring all our production code. Can we think about something in configuration so that I can plug-in without code change?

 

Regards,

Jyothish George

Sep 7, 2010 at 10:54 AM

Well, as far as I know there is no approach that will not require you to at least have some minor code change. Another approach I can think of is by creating a custom exception handler class that you can plug-in in your configuration though this requires a new class to be created (you can refer to the documentation for more details on creating a custom exception handler http://msdn.microsoft.com/en-us/library/ff664570(v=PandP.50).aspx).

For me this is more scalable and beneficial if you do not want to update your existing code and you can actually directly executes the actual logging from the custom handler. I think your custom exception handler may look something like this;

[ConfigurationElementType(typeof(CustomHandlerData))]
    public class MyCustomExceptionHandler : IExceptionHandler
    {
        public MyCustomExceptionHandler(NameValueCollection ignore) : base() { }


        public Exception HandleException(Exception exception, Guid handlingInstanceId)
        {
            var ex = (FaultException<Service.CompositeType>)exception;            

            LogWriter writer = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
            LogEntry log = new LogEntry();
            log.Categories.Add("General");
            log.Severity = System.Diagnostics.TraceEventType.Error;
            log.ExtendedProperties.Add("ID", ex.Detail.ID);
            log.ExtendedProperties.Add("Message", ex.Detail.Message);
            log.ExtendedProperties.Add("Custom StackTrace", ex.Detail.CustomStackTrace);
            writer.Write(log);

            return exception;
        }
 

    }

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Sep 8, 2010 at 10:30 AM

Hi Gino Terrado,

I am going with your first solution with a bit of code change, thanks for your support

Regards,

Jyothish George