ASP.net 的trace

          trace对于诊断应用问题非常有效,.Net framework里面提供了System.Diagnostics,里面有很好的trace机制,那么asp.net2.0自己的trace又是怎么回事,二者关系如何? 

         基本上来讲,asp.net的trace是独立的机制,但是他和framework的System.Diagnostics的机制有结合的方法

       1。通过设置trace配置(配置文件或者@page的属性设置),可以看到自己的页面或者刚才执行过的页面  (注意,只是页面)的情况,包括

  • The sequence of page events that occurred while the page ran.
  • The types, names, and sizes of the controls on the page.
  • The cookies and their contents.
  • The server variables, which is a collection of all information that the browser sends to the server.
显示结果可以在当前页面上也可以通过 ASP.NET Trace viewer (Trace.axd)来观察

 

2。可以使用trace.warn或者trace.write方法写一些自己的trace信息,这些也会和页面自己的trace一起显示

 
3。设置了trace以后,信息被记录,可以通过事件的方式获取修改这些trace信息
 
4。如果page里面调用了其他对象,比如调用业务对象,业务对象内部记录的trace信息,实际上是trace不出来的,这个时候就需要使用 System.Diagnostics 的方法了,有两个选择:
a。Routing All Tracing Output to the Web Form
这种方式就是添加一个 WebPageTraceListener,然后所有使用System.Diagnostics里面的trace类记录的信息就会和asp.net自己的那些trace信息一起显示了
 
   
      
            type="System.Web.WebPageTraceListener, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
   
 
b。Routing All Tracing Output to .NET Framework Tracing
这种方式就是使用System.Diagnostics的机制,添加listener,让所有trace信息都到listener里面去,而不再是asp.net自己的trace展示方式了(页面中或者trace.axd的方式)
这种方式首先要修改config,告诉asp.net使用 System.Diagnostics
然后就是标准的trace的listener添加了
   
 
   
         
        type="System.Diagnostics.TextWriterTraceListener, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        initializeData="/Asptesttrace.log" />
   
 
 
 
但是,前提是,由于是System.Diagnostics,所以必须To compile your application with trace enabled
 
   
              extension=".cs"
              compilerOptions="/d:TRACE"
              type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="1" />
   
              extension=".vb"
              compilerOptions="/d:Trace=true"
              type="Microsoft.VisualBasic.VBCodeProvider, System,                                        Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
 

你可能感兴趣的:(微软技术架构)