protected void Application_BeginRequest()
{
try
{
HttpResponse response = HttpContext.Current.Response;
OutputFilterStream filter = new OutputFilterStream(response.Filter);
response.Filter = filter;
HttpContext.Current.Items.Add("filter", filter);
}
catch (Exception)
{
}
}
protected void Application_EndRequest()
{
try
{
string requrl = HttpContext.Current.Request.Path;
string para = new StreamReader(HttpContext.Current.Request.InputStream).ReadToEnd();
string resp = ((OutputFilterStream)HttpContext.Current.Items["filter"]).ReadStream();
new LogHelper("./Log/log" + DateTime.Now.ToString("yyyyMMdd") + ".log").WriteLine("{\"requrl\":\"" + requrl + "\",\"reqpara\":\"" + para + "\",\"responsebody\":\"" + resp + "\"}");
}
catch (Exception)
{
}
}
public class OutputFilterStream : Stream
{
private readonly Stream InnerStream;
private readonly MemoryStream CopyStream;
public OutputFilterStream(Stream inner)
{
this.InnerStream = inner;
this.CopyStream = new MemoryStream();
}
public string ReadStream()
{
lock (this.InnerStream)
{
if (this.CopyStream.Length <= 0L ||
!this.CopyStream.CanRead ||
!this.CopyStream.CanSeek)
{
return String.Empty;
}
long pos = this.CopyStream.Position;
this.CopyStream.Position = 0L;
try
{
return new StreamReader(this.CopyStream).ReadToEnd();
}
finally
{
try
{
this.CopyStream.Position = pos;
}
catch { }
}
}
}
public override bool CanRead
{
get { return this.InnerStream.CanRead; }
}
public override bool CanSeek
{
get { return this.InnerStream.CanSeek; }
}
public override bool CanWrite
{
get { return this.InnerStream.CanWrite; }
}
public override void Flush()
{
this.InnerStream.Flush();
}
public override long Length
{
get { return this.InnerStream.Length; }
}
public override long Position
{
get { return this.InnerStream.Position; }
set { this.CopyStream.Position = this.InnerStream.Position = value; }
}
public override int Read(byte[] buffer, int offset, int count)
{
return this.InnerStream.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
this.CopyStream.Seek(offset, origin);
return this.InnerStream.Seek(offset, origin);
}
public override void SetLength(long value)
{
this.CopyStream.SetLength(value);
this.InnerStream.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
this.CopyStream.Write(buffer, offset, count);
this.InnerStream.Write(buffer, offset, count);
}
}
新建类OutputFilterStream copy一份HttpContext.Current.Response的响应流,把copy的流放入httpContent.Item中,在Application_EndRequest中读取copy的流。