locked
WCF & Logging Application Block RRS feed

  • Question

  • Hello,

    How can I log an exception to a log file or event log from WCF? I have implemented ProvideFault() under IErrorHandler, it throws a custom fault/exception to the client. I want to implement HandleError() to log the exception details. How can I do this using Logging Application Block? I just want to log the exception message, source, and may be the stack trace.

    Thanks.
    Tuesday, April 14, 2009 11:17 PM

Answers

All replies

  • Hello,

    Within Ent Lib Exception Handling Application Block there is WCF Exception Shielding feature. Check this one. It must handle your scenario very well.
    Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/
    Wednesday, April 15, 2009 7:17 AM
  • Exception Shielding is sheilding the exception details from the client, right? I believe that's what I am doing when I implement ProvideFault() and return a custom exception?
    Wednesday, April 15, 2009 4:25 PM
  • No, exception shielding allows you automatically convert internal exceptions to SOAP faults defined in service contract. You can easily add logging handlers there.

    Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/
    Wednesday, April 15, 2009 7:19 PM

  • I think that's what I am doing. In my ProvideFault method, I convert all the exceptions into 'CustomFault' and return this custom SOAP fault to the client.

    There is a HandleError method in the same interface which I want to implement to log the exceptions. Can you point me to some online articles or samples where they add logging handlers.
    Wednesday, April 15, 2009 7:57 PM
  • If you're asking about how to log your exceptions with other wcf traces, see this posting: http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/33c106b4-8f86-4e0f-ab2f-b3f1b289b926/

    See http://msdn.microsoft.com/en-us/library/cc309261.aspx for an overview of integrating the Logging Application Block with WCF.

    Thursday, April 16, 2009 2:04 AM
  • Is there any way to log just the exception details? I tried the code from msdn article, it logs a huge xml string.
    Thursday, April 16, 2009 2:54 AM
  • -> Is there any way to log just the exception details? I tried the code from msdn article, it logs a huge xml string.

    I've written a sample a while back which use IErrorHandler to implement exception logging feature, please refer to the following link for source:

    http://oxju0g.blu.livefilestore.com/y1pd8npWx0SQVJNVTMUniHtjUPwGdFTrftSASOR3s1SbXdMdC9ZDLZug0PaCeMn33KT9fJXOxXvT_D6MiwgqpYblw/ErrorHandlingWithParameterChecking.zip?download

    Hope this helps
    Another Paradigm Shift
    http://shevaspace.blogspot.com
    • Edited by Marco Zhou Monday, April 20, 2009 4:44 AM fix the link
    • Marked as answer by Marco Zhou Monday, April 27, 2009 5:31 AM
    Friday, April 17, 2009 6:09 AM
  • Can you please post the link again. If I click on the above link, a blank page opens up, but the file download never begins. Thanks.
    Friday, April 17, 2009 5:09 PM
  • Link has been fixed.

    Thanks
    Marco
    Another Paradigm Shift
    http://shevaspace.blogspot.com
    Monday, April 20, 2009 4:44 AM
  • Sorry for the delayed response.

    Going through your code, I don't see the exception logging code. This is what is there in HandleError()

            public bool HandleError(Exception error)
            {
                return error is OperationFaultException;
            }
    Wednesday, April 22, 2009 9:52 PM
  • You can easily add the logging code inside the IErrorHandler implementation, you could even incorporate the logging application block here as Mark mentioned above.

    Thanks
    Another Paradigm Shift
    http://shevaspace.blogspot.com
    Thursday, April 23, 2009 2:46 AM
  • I tried the tracing example for WCF. It works, but I am interested in just the exception details. Plus I would like the option to control the logging type, to event log, log file, DB, email etc. I am not sure how can I configure something like that with tracing.

    I tried the logging application block with a sample web app. It works fine. I copied the same stuff over to my WCF app, it doesn't work. The logging code is inside HandleError(), but it doesn't log anything when an exception occurs. Should the configuration be changed differently for WCF apps?

    Thursday, April 23, 2009 4:06 AM
  • -> I copied the same stuff over to my WCF app, it doesn't work. The logging code is inside HandleError(), but it doesn't log anything when an exception occurs. Should the configuration be changed differently for WCF apps?

    Could you please send the project which has logging application block integrated back to me, I should be able to get it working for you after looking into the code?

    Thanks

    Another Paradigm Shift
    http://shevaspace.blogspot.com
    Friday, April 24, 2009 5:02 AM

  • Never mind. LogEntry category in HandleError and the category source name under loggingconfiguration in web.config were different. I got the samples from two different places, so they were not the same and I didn't know they need to match. Plus I did not explicity set the LogEntry severity, as web.config was configured to catch just Errors.

    Thanks for the help.
    • Edited by skumart Friday, April 24, 2009 8:51 PM
    Friday, April 24, 2009 6:41 PM