È anche possibile implementare inserendo il codice Global.asax.cs
protected void Application_BeginRequest(object sender, EventArgs e)
{
// Create byte array to hold request bytes
byte[] inputStream = new byte[HttpContext.Current.Request.ContentLength];
// Read entire request inputstream
HttpContext.Current.Request.InputStream.Read(inputStream, 0, inputStream.Length);
//Set stream back to beginning
HttpContext.Current.Request.InputStream.Position = 0;
//Get XML request
string requestString = ASCIIEncoding.ASCII.GetString(inputStream);
}
Ho un metodo di utilità nel mio servizio web che uso per catturare la richiesta quando succede qualcosa che non mi aspettavo come un'eccezione non gestita.
/// <summary>
/// Captures raw XML request and writes to FailedSubmission folder.
/// </summary>
internal static void CaptureRequest()
{
const string procName = "CaptureRequest";
try
{
log.WarnFormat("{0} - Writing XML request to FailedSubmission folder", procName);
byte[] inputStream = new byte[HttpContext.Current.Request.ContentLength];
//Get current stream position so we can set it back to that after logging
Int64 currentStreamPosition = HttpContext.Current.Request.InputStream.Position;
HttpContext.Current.Request.InputStream.Position = 0;
HttpContext.Current.Request.InputStream.Read(inputStream, 0, HttpContext.Current.Request.ContentLength);
//Set back stream position to original position
HttpContext.Current.Request.InputStream.Position = currentStreamPosition;
string xml = ASCIIEncoding.ASCII.GetString(inputStream);
string fileName = Guid.NewGuid().ToString() + ".xml";
log.WarnFormat("{0} - Request being written to filename: {1}", procName, fileName);
File.WriteAllText(Configuration.FailedSubmissionsFolder + fileName, xml);
}
catch
{
}
}
Poi nel web.config ho memorizzare diversi valori appSetting che definiscono il livello che voglio usare per catturare la richiesta.
<!-- true/false - If true will write to an XML file the raw request when any Unhandled exception occurrs -->
<add key="CaptureRequestOnUnhandledException" value="true"/>
<!-- true/false - If true will write to an XML file the raw request when any type of error is returned to the client-->
<add key="CaptureRequestOnAllFailures" value="false"/>
<!-- true/false - If true will write to an XML file the raw request for every request to the web service -->
<add key="CaptureAllRequests" value="false"/>
Quindi nel mio Application_BeginRequest lo ho modificato in questo modo. Nota che Configuration è una classe statica che creo per leggere le proprietà da web.config e altre aree.
protected void Application_BeginRequest(object sender, EventArgs e)
{
if(Configuration.CaptureAllRequests)
{
Utility.CaptureRequest();
}
}
fonte
2011-04-01 21:07:42
Questo non funzionerà molto bene con la codifica UTF-8, non credo. Inoltre, sei sicuro che il flusso sia ricercabile? Sempre? –
Per il flusso di input, sì. Questo è quello che sto attualmente usando per eseguire il debug delle richieste in arrivo e non ho ancora riscontrato alcun problema. Il flusso di risposta tuttavia non era ricercabile. Ho dovuto avvolgere il flusso di risposta (originale/copia) prima che potessi registrarlo. – nivlam
E il flusso di uscita ?? come posso catturarlo utilizzando un'implementazione IHttpModule? – gillyb