惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
V
Visual Studio Blog
G
Google Developers Blog
Blog — PlanetScale
Blog — PlanetScale
Last Week in AI
Last Week in AI
C
Check Point Blog
D
Docker
M
MIT News - Artificial intelligence
P
Proofpoint News Feed
博客园 - 叶小钗
博客园 - 聂微东
The Cloudflare Blog
云风的 BLOG
云风的 BLOG
Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
酷 壳 – CoolShell
酷 壳 – CoolShell
MyScale Blog
MyScale Blog
WordPress大学
WordPress大学
Engineering at Meta
Engineering at Meta
腾讯CDC
S
SegmentFault 最新的问题
博客园 - 【当耐特】

博客园 - aiya

Effective C# Item 35: Prefer Overrides to Event Handlers Effective C# Item 40: Match Your Collection to Your Needs Effective C# Item 31: Prefer Small, Simple Functions Effective C# Item 30: Prefer CLS-Compliant Assemblies Chapter 4 Create Binary Components Effective C# Item 29: Use the new Modifier Only When Base Class Updates Mandate it Effective C# Item 28: Avoid Conversion Operators Effective C# Item 27: Avoid ICloneable Effective C# Item 26: Implement Ordering Relations with IComparable and IComparer 阿迪耐斯 Effective C# Item 25: Prefer Serializable Type Effective C# Item 24: Prefer Declarative to Imperative Programming Effective C# Item 23: Avoid Returning References to Internal Class Objects Effective C# Item 22: Define Outgoing Interface With Events 使用GZipStream类压缩和解压文件夹 Effective C# Item 21: Express Callbacks with Delegates Effective C# Item 20: Distinguish Between Implementing Interfaces and Overriding Virtual Functions Effective C# Item19: Prefer Defining and Implementing Interfaces to Inheritance Effective C# Item18: Implement the Standard Dispose Pattern
为程序添加自己的跟踪侦听器
aiya · 2007-06-01 · via 博客园 - aiya


      跟踪可以监视应用程序在执行时的情况,获取需要的信息。通过跟踪,可以收集到程序在执行过程中的一些有用的信息。在.Net中可以通过Trace类方便的实现简单的跟踪机制。在默认情况下是使用DefaultTraceListener侦听器对Trace发送的消息进行侦听和存储的。除此之外,.Net Framework还提供了一些特殊化功能的侦听器,同默认侦听器一样,它们都派生自TraceListener类。

  • TextWriterTraceListener将收集的消息输出到Stream中,我们可以方便的将其保存成文件。

  • EventLogTraceListener输出到事件日志

  • ConsoleTraceListener可以将跟踪信息发送到标准输出流

  • XmlWriterTraceListener当然就是将信息以xml的形式输出到Stream中,同样方便保存为xml片断文件

      侦听器可以通过在代码中调用Trace.Listeners.Add和Remove方法进行添加和移除操作。另外也可以通过在应用程序配置文件中设置<system.diagnostics>元素来对消息跟踪、侦听器以及跟踪开关级别进行设置。其中的<trace>子元素可以对消息跟踪进行相关的设置。

<configuration>
   
<system.diagnostics>
      
<trace>
         
<listeners>
            
<add name="myListener" type="System.Diagnostics. XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" initializeData="c:\myListener.xml" />
            
<remove name="Default"/>
         
</listeners>
      
</trace>
   
</system.diagnostics>
</configuration>

      上例中我们添加了一个名为myListener的XmlWriterTraceListener类型的侦听器,并移除了默认的侦听器。

      如果对于跟踪侦听还有其他特殊的要求,也可以实现自己的侦听器并用相似的方法添加它们。下例是一个派生自TextWriterTraceListener的自定义侦听器类。它完成的功能也很简单。通过重写WriteLine方法,该侦听器可以简单的格式化输出形式并添加时间。较常用的还有Write和WriteIf方法。

    public class MyTraceListener : System.Diagnostics.TextWriterTraceListener
    
{
        
public MyTraceListener(string filename)
            : 
base(filename)
        
{
        }


        
public override void WriteLine(string str)
        
{
            
base.WriteLine(string.Format("my trace name='{0}' time='{1}' ", str, DateTime.Now.ToString()));
        }

    }

P.S.对于XmlWriterTraceListener类型的侦听器来说,它会将消息输出为特定格式的xml片断。为了生成标准xml文件,还需要对其进行进一步的操作。最直接的做法就是将跟踪结果先储存在临时的文件中,在跟踪结束后将生成的xml片断添加到正式的trace文件里。但是某些情况下,trace文件会随着时间的增长和操作的增多变得非常巨大,由于xml的结构要求,我们不能像一般文档那样对其在末尾进行简单的追加操作。频繁的读取和导航可能会消耗较多的资源。对于这种情况我们可以使用引用外部实体来合并xml文档。XInclude?貌似支持它的太少了点...

再P.S.使用.Net使用XmlDocument处理xml文件时是禁用DTD的。如果要处理上面说的这种xml文档需要将XmlReaderSettings的ProhibitDtd属性设为false并在Load文档时候传入。