System.Net(HttpWebRequest的)跟踪,而无需使用文件或App.config中? [英] System.Net (HttpWebRequest) tracing without using files or app.config?

查看:482
本文介绍了System.Net(HttpWebRequest的)跟踪,而无需使用文件或App.config中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕捉某些,但不是全部,在我的调试目的应用HttpWebRequest的流量。这是由IIS托管的Web服务。



我看过的如何配置网络跟踪。这个伟大的工程,但我不希望跟踪定向到一个文件,由于文件系统,数据的敏感性等有关可能的权限问题,我想捕获直接在内存中的东西,我可以检查随后或加密和电子邮件。最好是,这不会涉及到app.config文件的任何更改。



我试过以下,但显然我错过了一步到TextWriterTraceListener会扎入System.Net 。 ?我怎样才能捕捉到System.Net交通到我的StringWriter

 的StringWriter SW =新的StringWriter(); 
TextWriterTraceListener会myListener的=新TextWriterTraceListener会(SW);
Trace.Listeners.Add(myListener的);
HttpWebRequest的REQ =(HttpWebRequest的)WebRequest.Create(http://www.microsoft.com);
HttpWebResponse RESP =(HttpWebResponse)req.GetResponse();
流S = resp.GetResponseStream();

的byte [] buf中=新的字节[4096];
而(s.Read(BUF,0,buf.Length)大于0);
S.CLOSE();

myListener.Flush();
sw.Flush();



编辑:具体来说,我想要做的这个在运行时等价的,不过我不要输出去network.log,我想它去我设置了用于此目的的字符串缓冲区。

 <结构> 

<&System.Diagnostics程序GT;

<来源和GT;
<信源名称=的System.Net.SocketsTRACEMODE =includehex>
<&听众GT;
<添加名称=的System.Net.SocketsTYPE =System.Diagnostics.TextWriterTraceListenerinitializeData =network.log/>
< /听众>
< /源>
< /来源>

<开关与GT;
<添加名称=的System.Net.SocketsVALUE =详细/>
< /开关>

<跟踪自动冲洗=真/>
< /system.diagnostics>
< /结构>


解决方案

您可以创建自己的TraceListener实现。该例如(S)我在网上是在运行配置发现的东西时间不显示与系统跟踪源工作。如果你不介意你的手脏,你可以尝试使用反射来切换私有静态布尔System.Net.Logging.s_LoggingEnabled(.NET 2)。





<:下面的文章中的例子,发送电子邮件到发布,你可以订阅,当你对收到跟踪消息一个静态事件打开它A HREF =http://msdn.microsoft.com/en-us/magazine/cc300790.aspx相对=nofollow>扩展System.Diagnostics程序



这确实会导致性能的下降,因为启用了日志记录所有的时间(全有或全无的配置在web.config)。 (请参见这篇文章和评论解释的重要性的除去缺省跟踪以提高性能。)


I want to capture certain, but not all, HttpWebRequest traffic in my application for debugging purposes. It's a web service hosted by IIS.

I have read How to: Configure Network Tracing. 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.

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?

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) > 0) ;
s.Close();

myListener.Flush();
sw.Flush();

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.

<configuration>

<system.diagnostics>

<sources>
  <source name="System.Net.Sockets" tracemode="includehex">
    <listeners>
      <add name="System.Net.Sockets" type="System.Diagnostics.TextWriterTraceListener" initializeData="network.log" />
    </listeners>
  </source>
</sources>

<switches>
  <add name="System.Net.Sockets" value="Verbose"/>
</switches>

<trace autoflush="true" />
</system.diagnostics>
</configuration>

解决方案

You can create your own TraceListener implementation. The example(s) I have found online that configure things at run-time do not show working with the system trace sources. If you don't mind getting your hands dirty you could try using reflection to toggle the private static bool System.Net.Logging.s_LoggingEnabled (.NET 2).

Take the example in the following article and switch it from sending e-mails to publishing a static event that you can subscribe to when you're interested in receiving trace messages:

Extending System.Diagnostics

This does cause a performance hit since logging is enabled all the time (all or nothing as configured in the web.config). (See this article and comments explaining the importance of removing the default trace to improve performance.)

这篇关于System.Net(HttpWebRequest的)跟踪,而无需使用文件或App.config中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆