mardi 25 septembre 2018

Logging HttpListener Response OutputStream during debug

I try to get every request and response of my HttpListener logged.

For the HttpListenerContext.Request.InputStream, this was as simple as doing this:

var finfoIn = context.Request.GetType().GetField("m_RequestStream", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
context.Request.InputStream.CopyTo(memstreamIn);
memstreamIn.Seek(0, System.IO.SeekOrigin.Begin);
var reader = new System.IO.StreamReader(memstreamIn, Encoding.Unicode);
this.Log(reader.ReadToEnd()); //Actual log method that prints to console
memstreamIn.Seek(0, System.IO.SeekOrigin.Begin);
finfoIn.SetValue(context.Request, memstreamIn);

However, due to the HttpListenerContext.Response.OutputStream having the field type (internal) System.Net.HttpResponseStream, this above trick is not possible.

Anybody got some solution to allow logging for the OutputStream?





Aucun commentaire:

Enregistrer un commentaire