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 に答える