views:

39

answers:

2

I have a System.Web.Services.WebService containing several WebMethods. If any of these WebMethods raises an exception, I want to log the values passed to that WebMethod. I want to handle this in a way that is generic enough that I can use the same method regardless of the number or type of parameters. I thought I would log the raw JSON but I am unable to determine how to access it. I have searched throughout the Context.Request object (including the InputStream property) without finding it.

Here is what I would like to do:

[WebMethod(EnableSession = true)]
public IEnumerable MyWebMethod(int a, string b)
{
    try
    {
      //do something
    }
    catch (Exception e)
    {
      LogException(e, this.Context);
      throw;
    }
}

//All WebMethods should be able to call LogExceoption regardless of param type/count
protected void LogException(Exception ex, HttpContext context)
{
  string parameters = context.Request. //?? i don't know how to get to the JSON
  //write exception detail to log
}

I am using C# with .net Framework 3.5

A: 

See: http://msdn.microsoft.com/en-us/library/system.web.services.protocols.soapextension(VS.71).aspx

See TraceExtension, in the example. It handles all web methods and logs the results to a file. Should be straightforward to adapt that to your needs.

Kirk Woll
+1  A: 

Here is how to access the raw JSON:

protected void LogException(Exception ex, HttpContext context)
{
    context.Request.InputStream.Position = 0;
    string rawJson = null;
    using (StreamReader reader = new StreamReader(context.Request.InputStream))
    {
        rawJson = reader.ReadToEnd();
    }
    //write exception detail to log
}
Joel Harris
Crazy that it was that simple.
Jason Webb