Note that there are some explanatory texts on larger screens.

plurals
  1. POSystem.Net (HttpWebRequest) tracing without using files or app.config?
    text
    copied!<p>I want to capture certain, but not all, HttpWebRequest traffic in my application for debugging purposes. It's a web service hosted by IIS.</p> <p>I have read <a href="http://msdn.microsoft.com/en-us/library/ty48b824(VS.80).aspx" rel="noreferrer">How to: Configure Network Tracing</a>. This works great, but I don't want to direct the trace to a file, due to possible permission problems on the file system, data sensitivity, etc. I would like to capture directly to a something in-memory that I can subsequently inspect or encrypt and email. Preferably, this would not involve any changes to the app.config file.</p> <p>I tried the following, but obviously I am missing a step to tie the TextWriterTraceListener into System.Net. How can I capture the System.Net traffic into my StringWriter?</p> <pre><code>StringWriter sw = new StringWriter(); TextWriterTraceListener myListener = new TextWriterTraceListener(sw); Trace.Listeners.Add(myListener); HttpWebRequest req = (HttpWebRequest) WebRequest.Create("http://www.microsoft.com"); HttpWebResponse resp = (HttpWebResponse) req.GetResponse(); Stream s = resp.GetResponseStream(); byte[] buf = new byte[4096]; while (s.Read(buf, 0, buf.Length) &gt; 0) ; s.Close(); myListener.Flush(); sw.Flush(); </code></pre> <p>Edit: Specifically, I want to do the equivalent of this at runtime, except that I don't want output to go to network.log, I want it to go to a string buffer I've set up for this purpose.</p> <pre><code>&lt;configuration&gt; &lt;system.diagnostics&gt; &lt;sources&gt; &lt;source name="System.Net.Sockets" tracemode="includehex"&gt; &lt;listeners&gt; &lt;add name="System.Net.Sockets" type="System.Diagnostics.TextWriterTraceListener" initializeData="network.log" /&gt; &lt;/listeners&gt; &lt;/source&gt; &lt;/sources&gt; &lt;switches&gt; &lt;add name="System.Net.Sockets" value="Verbose"/&gt; &lt;/switches&gt; &lt;trace autoflush="true" /&gt; &lt;/system.diagnostics&gt; &lt;/configuration&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload