XPO使用了标准的System.Diagnostics的Trace Log机制,只需要在config文件中加入如下代码,即可在Debug时在输出窗口看到XPO生成的SQL语句。

Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    
<system.diagnostics>
        
<switches>
            
<add name="XPO" value="3" />
        
</switches>
    
</system.diagnostics>
</configuration>

 

 

也可以将其记录到日志文件:

Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    
<system.diagnostics>
        
<trace autoflush="true" indentsize="4">
            
<listeners>
                
<add name="LogFileTraceListener" type="System.Diagnostics.TextWriterTraceListener" 
                    initializeData
="trace.log" />
                
<remove name="Default" />
            
</listeners>
        
</trace>
        
<switches>
            
<add name="XPO" value="3" />
        
</switches>
    
</system.diagnostics>
</configuration>

 

 

因为采用.NET标准的Trace Log机制,我们也可以自己实现一个Log类:

MyTraceListener
using System;

namespace Test
{
    
public class MyTraceListener: System.Diagnostics.TraceListener
    {
        
public MyTraceListener()
        {
            
        }
        
public MyTraceListener(string name)
            : 
base(name)
        {
            
        }

        
public override void Write(string message)
        {
            
throw new NotImplementedException();
        }

        
public override void WriteLine(string message)
        {
            
throw new NotImplementedException();
        }
    }
}

 

然后在Config文件中做配置 :

Config
 <system.diagnostics>
    
<trace autoflush="true" indentsize="4">
      
<listeners>
        
<!--注意命名空间和类名别写错-->
        
<add name="MyTraceListener" type="Test.MyTraceListener,Test"/>
        
<remove name="Default" />
      
</listeners>
    
</trace>
    
<switches>
      
<add name="XPO" value="3" />
    
</switches>
  
</system.diagnostics>

 

或者在程序的入口点加上:

// Remove the original default trace listener.
Trace.Listeners.RemoveAt(0);

// Create and add a new MyTraceListener.
Trace.Listeners.Add(new MyTraceListener());

 

 

这样Log内容想记哪儿都可以随自己喜欢了,MyTraceListener的具体实现就不展开了。 

 

MSDN: TraceListener 类 

posted on 2010-02-05 18:00  Elvin Chen  阅读(932)  评论(0编辑  收藏  举报