2

我有一个System.Web.Services.WebService包含几个WebMethods. 如果其中任何一个WebMethods引发异常,我想记录传递给该 WebMethod 的值。我想以一种足够通用的方式处理这个问题,无论参数的数量或类型如何,我都可以使用相同的方法。我以为我会记录原始 JSON,但我无法确定如何访问它。我搜索了整个 Context.Request 对象(包括InputStream属性),但没有找到它。

这是我想做的事情:

[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
}

我正在使用带有 .net Framework 3.5 的 C#

4

2 回答 2

4

以下是访问原始 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
}
于 2010-08-22T04:40:30.623 回答
0

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

请参阅示例中的 TraceExtension。它处理所有 Web 方法并将结果记录到文件中。应该很容易适应您的需求。

于 2010-08-20T22:07:38.463 回答