System.Net Logging Tips

How many times you asked your self, what is going under the hood , if you call the function System.Net.WebClient.DownloadData, with .Net 2.0 you can see whole the requests going and coming when you call this method with 2 steps.

1) Just create a app.config file for your application

2) Add the following entries to your app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.diagnostics>
    <trace autoflush="true" />
    <sources>
      <source name="System.Net">
        <listeners>
          <add name="TraceFile" />
        </listeners>
      </source>   
    </sources>
    <sharedListeners>
      <add
        name="TraceFile"
        type="System.Diagnostics.TextWriterTraceListener"
        initializeData="NetTrace.log"
      />
    </sharedListeners>
    <switches>
      <add name="System.Net" value="Verbose" />
    </switches>
  </system.diagnostics>
</configuration>

3) Just Call WebClient.DownloadData from your code, you will have entries like this in your nettrace.log file and debug window:

ystem.Net Verbose: 0 : [3816] WebClient#47633461::DownloadData(http://www.yahoo.com/#2136786598)

System.Net Verbose: 0 : [3816] WebClient#47633461::DownloadData(http://www.yahoo.com/#2136786598)

System.Net Verbose: 0 : [3816] WebRequest::Create(http://www.yahoo.com/)

System.Net Verbose: 0 : [3816] HttpWebRequest#63130991::HttpWebRequest(http://www.yahoo.com/#2136786598)

System.Net Verbose: 0 : [3816] Exiting HttpWebRequest#63130991::HttpWebRequest()

System.Net Verbose: 0 : [3816] Exiting WebRequest::Create() -> HttpWebRequest#63130991

System.Net Verbose: 0 : [3816] HttpWebRequest#63130991::GetResponse()

System.Net Information: 0 : [3816] Associating HttpWebRequest#63130991 with ServicePoint#64981649

System.Net Information: 0 : [3816] Associating Connection#38493088 with HttpWebRequest#63130991

System.Net Information: 0 : [3816] Associating HttpWebRequest#63130991 with ConnectStream#54467399

System.Net Information: 0 : [3816] HttpWebRequest#63130991 - Request: GET http://www.yahoo.com/ HTTP/1.1

System.Net Information: 0 : [3816] ConnectStream#54467399 - Sending headers

{

Host: www.yahoo.com

Proxy-Connection: Keep-Alive

}.

System.Net Information: 0 : [3816] Connection#38493088 - Received status line: Version=1.1, StatusCode=407, StatusDescription=Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy service is denied. ).

System.Net Information: 0 : [3816] Connection#38493088 - Received headers

{

Proxy-Authenticate: NTLM,Basic realm="istwwebacs0.euea.corp.bshg.com",Kerberos,Negotiate

Via: 1.1 BLABLABLA

Connection: Keep-Alive

Proxy-Connection: Keep-Alive

Pragma: no-cache

Cache-Control: no-cache

Content-Type: text/html

Content-Length: 2381

}.

System.Net Information: 0 : [3816] ConnectStream#35113868::ConnectStream(Buffered 2381 bytes.)

System.Net Information: 0 : [3816] Associating HttpWebRequest#63130991 with ConnectStream#35113868

System.Net Information: 0 : [3816] Associating HttpWebRequest#63130991 with HttpWebResponse#36620214

System.Net Verbose: 0 : [3816] ConnectStream#35113868::Read()

System.Net Verbose: 0 : [3816] Data from ConnectStream#35113868::Read

System.Net Verbose: 0 : [3816] 00000000 : 3C 21 44 4F 43 54 59 50-45 20 48 54 4D 4C 20 50 : <!DOCTYPE HTML P

System.Net Verbose: 0 : [3816] 00000010 : 55 42 4C 49 43 20 22 2D-2F 2F 57 33 43 2F 2F 44 : UBLIC "-//W3C//D

System.Net Verbose: 0 : [3816] 00000020 : 54 44 20 48 54 4D 4C 20-34 2E 30 20 54 72 61 6E : TD HTML 4.0 Tran

System.Net Verbose: 0 : [3816] 00000030 : 73 69 74 69 6F 6E 61 6C-2F 2F 45 4E 22 3E 0D 0A : sitional//EN">..

System.Net Verbose: 0 : [3816] 00000040 : 3C 48 54 4D 4C 20 64 69-72 3D 6C 74 72 3E 3C 48 : <HTML dir=ltr><H

System.Net Verbose: 0 : [3816] 00000050 : 45 41 44 3E 3C 54 49 54-4C 45 3E 54 68 65 20 70 : EAD><TITLE>The p

System.Net Verbose: 0 : [3816] 00000060 : 61 67 65 20 63 61 6E 6E-6F 74 20 62 65 20 64 69 : age cannot be di

System.Net Verbose: 0 : [3816] 00000070 : 73 70 6C 61 79 65 64 3C-2F 54 49 54 4C 45 3E 0D : splayed</TITLE>.

System.Net Verbose: 0 : [3816] 00000080 : 0A 3C 53 54 59 4C 45 3E-41 3A 6C 69 6E 6B 20 7B : .<STYLE>A:link {

System.Net Verbose: 0 : [3816] 00000090 : 0D 0A 09 46 4F 4E 54 3A-20 38 70 74 2F 31 31 70 : ...FONT: 8pt/11p

System.Net Verbose: 0 : [3816] 000000A0 : 74 20 76 65 72 64 61 6E-61 3B 20 43 4F 4C 4F 52 : t verdana; COLOR

System.Net Verbose: 0 : [3816] 000000B0 : 3A 20 23 66 66 30 30 30-30 0D 0A 7D 0D 0A 41 3A : : #ff0000..}..A:

System.Net Verbose: 0 : [3816] 000000C0 : 76 69 73 69 74 65 64 20-7B 0D 0A 09 46 4F 4E 54 : visited {...FONT

System.Net Verbose: 0 : [3816] 000000D0 : 3A 20 38 70 74 2F 31 31-70 74 20 76 65 72 64 61 : : 8pt/11pt verda

 

This logging feature is enable for whole System.Net based classes , which is also used by the webservices.

 

Happy Tracing :)

Published Thursday, August 31, 2006 8:52 AM by keremkusmezer

Comments

No Comments
Anonymous comments are disabled