ASMX服务的全局HttpModule中没有InnerException

时间:2016-07-28 10:35:59

标签: c# web-services exception-handling asmx ihttpmodule

我为https://stackoverflow.com/a/551532/6607492

中建议的asmx-Services成功实施了IHttpModule

不幸的是序列化异常(在我们的例子中是JSON格式)不包含有关InnerException的任何信息。

有没有办法看到InnerException?

我的代码与我上面提到的答案非常相似:

public class GlobalErrorLogModule : IHttpModule
{
    private ILogger log = Logger.GetLogger();
    private ErrorHandlerFilter errorHandlerFilter = null;

    public void Init(HttpApplication context)
    {
        context.PostRequestHandlerExecute += Context_PostRequestHandlerExecute;
        context.EndRequest += Context_EndRequest;
    }

    private void Context_PostRequestHandlerExecute(object sender, EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;
        if (app.Response.StatusCode == 500)
        {
            errorHandlerFilter = new ErrorHandlerFilter(app.Response.Filter);
            app.Response.Filter = errorHandlerFilter;
        }
    }

    private void Context_EndRequest(object sender, EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;
        if (app.Response.StatusCode == 500)
        {
            string exceptionContent = "";
            if (errorHandlerFilter != null)
            {
                exceptionContent = Encoding.UTF8.GetString(errorHandlerFilter.WrittenBytes.ToArray());
                errorHandlerFilter = null;
            }

            //The exceptionContent is in JSON-Format and does not contain information abouth the InnerException
            log.Error(exceptionContent);
        }
    }

    public void Dispose()
    {
    }
}

public class ErrorHandlerFilter : Stream
{
    private readonly Stream responseFilter;

    public List<byte> WrittenBytes { get; private set; }

    public ErrorHandlerFilter(Stream responseFilter)
    {
        this.responseFilter = responseFilter;
        WrittenBytes = new List<byte>();
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        for (int i = offset; i < offset + count; i++)
        {
            WrittenBytes.Add(buffer[i]);
        }
        responseFilter.Write(buffer, offset, count);
    }

    public override void Flush()
    {
        responseFilter.Flush();
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
        return responseFilter.Seek(offset, origin);
    }

    public override void SetLength(long value)
    {
        responseFilter.SetLength(value);
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        return responseFilter.Read(buffer, offset, count);
    }

    public override bool CanRead
    {
        get { return responseFilter.CanRead; }
    }

    public override bool CanSeek
    {
        get { return responseFilter.CanSeek; }
    }

    public override bool CanWrite
    {
        get { return responseFilter.CanWrite; }
    }

    public override long Length
    {
        get { return responseFilter.Length; }
    }

    public override long Position
    {
        get { return responseFilter.Position; }
        set { responseFilter.Position = value; }
    }
}

0 个答案:

没有答案