System.Net (HttpWebRequest) tracing without using files or app.config?

后端 未结 3 1307
面向向阳花
面向向阳花 2020-12-05 08:16

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: Conf

3条回答
  •  -上瘾入骨i
    2020-12-05 08:59

    Here's how to wireup System.Net logging in code via reflection. Code is in VB but is trivial to convert to c#...

    Dim logging = GetType(Net.HttpWebRequest).Assembly.GetType("System.Net.Logging")
    Dim enabled = logging.GetField("s_LoggingEnabled", Reflection.BindingFlags.NonPublic Or  Reflection.BindingFlags.Static)
    enabled.SetValue(Nothing, True)
    Dim webTr = logging.GetProperty("Web", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static)
    Dim tr as TraceSource = webTr.GetValue(Nothing, Nothing)
    tr.Switch.Level = SourceLevels.Verbose
    tr.Listeners.Add(New MyTraceListener())
    

    Put this in Global.asax Application_Start() with whatever conditions you want to turn it on. You may need to Flush() tr before reading.

提交回复
热议问题