devblog.avesse.net

programming and the web.

Using a SOAP Extension and Log4Net to trace an ASMX web service

Recently I had to implement some error logging on an old-ish ASMX SOAP web service.  Specifically, I wanted to get the stack trace for errors and the request input that caused them.  I’m told this is quite easy with WCF, but takes a bit of hackery with ASMX.

Initially I wanted to use Elmah because it’s super easy to set up and I use it in all my projects.  Unfortunately, ASP.NET  web services never fire Application_Error, which is where Elmah hooks in.  And I wasn’t going to wrap every method in a try/catch to log errors manually.

To intercept requests and responses as well as errors, you need to write a Soap Extension.  The Soap Extension then writes to the logging tool of your choice.  For this I used Log4Net, mostly because most of the log viewers are built for Log4Net and easy to configure for it (I quite like YALV, but there are many others).  Of course you can also log with Elmah, which I did for errors.

I used the template for a SoapExtension from MSDN and just plugged in the logging code in the appropriate places.  You can attach the SoapExtension by either using an attribute on the web service method you want to log, or you can register it in the web.config to log all requests.  I chose the latter, and configured Log4Net to use the XML Layout, since YALV (which I quite like) displays it nicely.

Get the Log4NetSoapExtension class and configuration here.

Just copy the Log4NetSoapExtension class to your project and add the config settings, and you’ve got a log of every SOAP request and response, as well as stack traces for errors.

6 Comments

  1. Tim says:

    Thank you for sharing this code. Could you tell me how you configured web.config to log all requests?

  2. Tim says:

    Oops. How did I miss it? It’s at the bottom of the page with the class code

  3. Tim says:

    I had to remove message.MethodInfo.Name from the header string concatenation statement in the WriteInput method because message.MethodInfo throws an exception at that stage. Other than that works as advertised!

  4. FirstIrving says:

    I have noticed you don’t monetize your page, don’t waste your traffic,
    you can earn additional cash every month because you’ve got high quality content.

    If you want to know how to make extra $$$, search for:
    Boorfe’s tips best adsense alternative

  5. Fivaulionhjstytnemseylhjrlrcolxueyfvkctwcvkpdoycqqua gdt6jtyhgjdtyrfj g Beple says:

    Gfxsrdhjspemvixnhjfxbrqknwxhjrxnpofvxqppzxkdbhjvlikshjf gdt6jtyhgjdtyrfj z Beple

Leave a Reply

Switch to our mobile site