<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>SQL中国研发中心 : Trace Management Object</title><link>http://blogs.msdn.com/sqlcrd/archive/tags/Trace+Management+Object/default.aspx</link><description>Tags: Trace Management Object</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>使用Trace Management Object监测和诊断SQL Server（二）</title><link>http://blogs.msdn.com/sqlcrd/archive/2009/01/24/trace-management-object-sql-server-2.aspx</link><pubDate>Sat, 24 Jan 2009 06:29:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9373596</guid><dc:creator>sqlcrdft</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/sqlcrd/comments/9373596.aspx</comments><wfw:commentRss>http://blogs.msdn.com/sqlcrd/commentrss.aspx?PostID=9373596</wfw:commentRss><description>&lt;P mce_keep="true"&gt;在这篇文章中我们将介绍一个replay trace的示例，通过重放抓到的trace文件来诊断应用程序在SQL Server上运行是否有问题。&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;STRONG&gt;Replay trace示例&lt;/STRONG&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;这个例子模仿你使用Profiler工具对抓到的trace文件进行重放，从而对SQL Server及你的应用程序进行诊断的过程。下面是详细的步骤和描述。&lt;/P&gt;
&lt;P mce_keep="true"&gt;1、TraceReplay类是对trace file或trace table进行重放的关键类，你需要设置TraceReplay的三个属性，Connection属性代表你将在哪个数据库实例上进行重放，Source属性代表重放哪个文件或数据表，OutputFile代表重放的结果将输出到哪个文件，你也可以使用OutputTable属性指定将重放结果输出到数据表中。下面的代码将使用capture trace例子里抓到的trace file作为重放的输入并将结果存为traceoutput.trc文件。&lt;/P&gt;
&lt;P class=MsoNormal align=left&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: #2b91af; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;TraceReplay&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt; replay = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;TraceReplay&lt;/SPAN&gt;();&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: #2b91af; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;SqlConnectionInfo&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt; connInfo = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;SqlConnectionInfo&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"."&lt;/SPAN&gt;);&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;connInfo.UseIntegratedSecurity = &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;replay.Connection = connInfo;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: #2b91af; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;TraceFile&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt; traceFile = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;TraceFile&lt;/SPAN&gt;();&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;traceFile.InitializeAsReader(&lt;SPAN style="COLOR: #a31515"&gt;@"c:\tracefile.trc"&lt;/SPAN&gt;);&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;replay.Source = traceFile;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: #2b91af; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;TraceFile&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt; traceFileOutput = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;TraceFile&lt;/SPAN&gt;();&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;traceFileOutput.InitializeAsReplayOutputWriter(&lt;SPAN style="COLOR: #a31515"&gt;@"c:\replayoutput.trc"&lt;/SPAN&gt;);&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA"&gt;replay.OutputFile = traceFileOutput;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;2、TraceReplay类还有个比较重要的属性Options，你可以通过设置Options属性来控制如何重放。Options属性是个TraceReplayOptions对象，Profiler工具中重放配置对话框上的很多配置项都可以通过Options属性来设置。下面的代码中设置了3个属性，Mode设置重放模式为连接层面上同步（另一个模式为SequentialReplay，指在所有连接上完全同步），NumberOfReplayThreads设置重放的线程数为2，KeepResults将控制在重放结果文件中是否出现SQL等的执行结果，比如你的trace里有select 1语句，那设置KeepResults为false将避免在结果文件中出现select 1返回的result set。&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;replay.Options.Mode = &lt;SPAN style="COLOR: #2b91af"&gt;ReplayMode&lt;/SPAN&gt;.ConnectionLevelSync;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;replay.Options.NumberOfReplayThreads = 2;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: #010001; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;replay&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;.&lt;SPAN style="COLOR: #010001"&gt;Options&lt;/SPAN&gt;.&lt;SPAN style="COLOR: #010001"&gt;KeepResults&lt;/SPAN&gt; = &lt;SPAN style="COLOR: blue"&gt;false&lt;/SPAN&gt;;&lt;?xml:namespace prefix = o /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;3、TraceReplay类提供了5个事件，你可以通过提供你自己的event handler来进一步控制重放操作，其中最有用的event是ReplayEvent，它将在每个event被重放之前调用，你可以控制是否跳过该event的重放。下面的代码在设置了ReplayEvent的处理函数后启动重放。请注意Start函数是同步操作，它将一直阻塞直到重放结束，所以如果你需要在中途停掉的话，你可以在event handler里或者其它线程中调用Stop函数。&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;replay.ReplayEvent += &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ReplayEventHandler&lt;/SPAN&gt;(ReplayHandler);&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;replay.Start();&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;traceFile.Close();&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;4、这里我们的ReplayHandler处理函数将对SPID进行过滤，所有SPID不是54的事件都将被忽略。代码中空的catch是为了避免某些event不含有SPID列会造成读取该列失败。&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;private&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt; &lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; ReplayHandler(&lt;SPAN style="COLOR: #2b91af"&gt;Object&lt;/SPAN&gt; sender, &lt;SPAN style="COLOR: #2b91af"&gt;ReplayEventArgs&lt;/SPAN&gt; args)&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;{&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;IDataRecordChanger&lt;/SPAN&gt; recordChanger = args.CurrentRecord;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; spid = 0;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;try&lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;{&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;spid = (&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;)recordChanger[&lt;SPAN style="COLOR: #a31515"&gt;"SPID"&lt;/SPAN&gt;];&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;}&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;catch&lt;/SPAN&gt; { }&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (spid != 54)&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;args.SkipRecord = &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;}&lt;/SPAN&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;5、Replay的结果文件可以用Profiler工具打开查看，当然你仍然可以通过TraceFile打开，只要简单的调用InitializeAsReader然后循环调用Read即可，下面的代码将打出TextData列的内容。&lt;/SPAN&gt;&lt;/P&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;
&lt;P class=MsoNormal align=left&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: #2b91af; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;TraceFile&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt; &lt;SPAN style="COLOR: #010001"&gt;traceFileOutput&lt;/SPAN&gt; = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;TraceFile&lt;/SPAN&gt;();&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: #010001; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;traceFileOutput&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;.&lt;SPAN style="COLOR: #010001"&gt;InitializeAsReader&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;@"c:\replayoutput.trc"&lt;/SPAN&gt;);&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;while&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt; (&lt;SPAN style="COLOR: #010001"&gt;traceFileOutput&lt;/SPAN&gt;.&lt;SPAN style="COLOR: #010001"&gt;Read&lt;/SPAN&gt;())&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;{&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.&lt;SPAN style="COLOR: #010001"&gt;WriteLine&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #010001"&gt;traceFileOutput&lt;/SPAN&gt;[&lt;SPAN style="COLOR: #a31515"&gt;"TextData"&lt;/SPAN&gt;]);&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;软件开发工程师 徐进&lt;/SPAN&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9373596" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/sqlcrd/attachment/9373596.ashx" length="7425" type="application/x-zip-compressed" /><category domain="http://blogs.msdn.com/sqlcrd/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/sqlcrd/archive/tags/Trace+Management+Object/default.aspx">Trace Management Object</category></item><item><title>使用Trace Management Object监测和诊断SQL Server（一）</title><link>http://blogs.msdn.com/sqlcrd/archive/2009/01/14/trace-management-object-sql-server.aspx</link><pubDate>Wed, 14 Jan 2009 12:09:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9318382</guid><dc:creator>sqlcrdft</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/sqlcrd/comments/9318382.aspx</comments><wfw:commentRss>http://blogs.msdn.com/sqlcrd/commentrss.aspx?PostID=9318382</wfw:commentRss><description>&lt;P&gt;大家一定用过Profiler工具，我们可以用它来对SQL Server建立trace来监测某些感兴趣的事件，也可以replay抓到的trace来诊断是哪些SQL语句的执行造成你的SQL Server耗费了大量的CPU资源。但Profiler是个GUI程序，有没有办法通过程序来抓trace和重放trace呢？也许有些读者会想到用SQLCMD.exe执行sp_trace_create等存储过程来操作，但那毕竟还是有些麻烦，这里我们要介绍的Trace Management Object（TMO）则是.NET对象，你可以把它理解成trace/replay的API，你可以非常方便地在你的.NET程序中使用。请注意由于SQL Express版本不支持trace，因而TMO对象也无法在SQL Express版本上运行，即使是SQL Server 2008 Express with Advanced Services也不支持。&lt;/P&gt;
&lt;P&gt;在SQL Server 2005里TMO对象被实现在了Microsoft.SqlServer.ConnectionInfo.dll里，在SQL Server 2008里TMO对象则被移到了Microsoft.SqlServer.ConnectionInfoExtended.dll里，但仍然在Microsoft.SqlServer.Management.Trace命名空间里。下面我们将以SQL Server 2008为例。&lt;/P&gt;
&lt;P&gt;用VS2005新建一个Visual C#的Console Application工程，在Project菜单里点击Add Reference…增加对下表几个组件的引用：&lt;/P&gt;
&lt;TABLE class="" cellSpacing=0 cellPadding=0 border=1&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="" vAlign=top width=269&gt;
&lt;P&gt;组件&lt;SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" vAlign=top&gt;描述&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="" vAlign=top width=269&gt;
&lt;P&gt;Microsoft.SqlServer.ConnectionInfo&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" vAlign=top&gt;
&lt;P&gt;需要SqlServerInfo类来建立对SQL的连接&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="" vAlign=top width=269&gt;
&lt;P&gt;Microsoft.SqlServer.ConnectionInfoExtended&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" vAlign=top&gt;
&lt;P&gt;TMO对象在这个Assembly里&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="" vAlign=top width=269&gt;
&lt;P&gt;Micrososft.SqlServer.Management.Sdk.Sfc&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" vAlign=top&gt;
&lt;P&gt;SQL Server 2008里很多组件是基于它建立，所以必须增加这个引用&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;
&lt;P&gt;组件描述Microsoft.SqlServer.ConnectionInfo 需要SqlServerInfo类来建立对SQL的连接 Microsoft.SqlServer.ConnectionInfoExtended TMO对象在这个Assembly里 Micrososft.SqlServer.Management.Sdk.Sfc SQL Server 2008里很多组件是基于它建立，所以必须增加这个引用 &lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.codeplex.com/" mce_href="http://www.codeplex.com/"&gt;http://www.codeplex.com/&lt;/A&gt;上SQL Server 2008的Samples里有个Readme_Tracer的例子（&lt;A href="http://www.codeplex.com/MSFTEngProdSamples/Release/ProjectReleases.aspx?ReleaseId=18651" mce_href="http://www.codeplex.com/MSFTEngProdSamples/Release/ProjectReleases.aspx?ReleaseId=18651"&gt;http://www.codeplex.com/MSFTEngProdSamples/Release/ProjectReleases.aspx?ReleaseId=18651&lt;/A&gt;），这个例子使用Standard.tdf模板启动一个live trace，trace的内容将打印在Console窗口上，但把结果打印到Console窗口上非常乱，而且也没有太大的实用价值，大家有兴趣可以去参考一下。本文第一部分将介绍一个capture trace的示例，和Readme_Tracer有点类似，但我们会把trace结果输出到trace文件，第二部分将介绍一个replay trace的示例，这也是Profiler最常用的两个功能。&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Capture trace示例&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;这个例子模仿你使用Profiler工具监测SQL Server操作的过程，程序启动一个trace，将抓到的trace event输出到文件中，等待60秒后退出。读者可以尝试将TraceFile类改为TraceTable类来输出到数据库表中。下面是详细的步骤和描述：&lt;/P&gt;
&lt;P&gt;1、TraceServer类代表连接到SQL Server Instance的一个trace，下面的代码演示了如何创建一个TraceServer对象。代码前两行使用Windows认证方式建立一个SqlConnectionInfo对象，你也可以通过提供用户名/密码的方式建立这个对象，InitializeAsReader函数的第二个参数是trace模板，这里使用的TSQL_Replay模板，你需要根据你的SQL Server安装目录进行修改。&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: #2b91af; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;SqlConnectionInfo&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt; &lt;SPAN style="COLOR: #020002"&gt;connInfo&lt;/SPAN&gt; = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;SqlConnectionInfo&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"."&lt;/SPAN&gt;);&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: #020002; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;connInfo&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;.&lt;SPAN style="COLOR: #020002"&gt;UseIntegratedSecurity&lt;/SPAN&gt; = &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: #2b91af; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;TraceServer&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt; &lt;SPAN style="COLOR: #020002"&gt;traceServer&lt;/SPAN&gt; = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;TraceServer&lt;/SPAN&gt;();&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: #020002; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA"&gt;traceServer&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA"&gt;.&lt;SPAN style="COLOR: #020002"&gt;InitializeAsReader&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #020002"&gt;connInfo&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;@"C:\Program Files\Microsoft SQL Server\100\Tools\Profiler\Templates\Microsoft SQL Server\100\TSQL_Replay.tdf"&lt;/SPAN&gt;);&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;2、TraceFile类代表一个trace文件，它既可以是capture trace的输出文件，也可以是replay trace的输入文件，下面的代码将traceFile对象设为traceServer所代表的trace的输出文件，最后一行为traceFile增加了一个WriteNotify事件的event handler，通过它我们将对输出做一些过滤。&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: #2b91af; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;TraceFile&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt; &lt;SPAN style="COLOR: #020002"&gt;traceFile&lt;/SPAN&gt; = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;TraceFile&lt;/SPAN&gt;();&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: #020002; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;traceFile&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;.&lt;SPAN style="COLOR: #020002"&gt;InitializeAsWriter&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #020002"&gt;traceServer&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;@"d:\tracefile.trc"&lt;/SPAN&gt;);&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;traceFile.WriteNotify += &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;WriteNotifyEventHandler&lt;/SPAN&gt;(WriteHandler);&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3、这两个对象建立完毕，此时trace已经开始了，但tracefile.trc却始终是0字节，为什么呢？因为你还需要调用TraceFile类的Write函数来输出，但Write函数有两个问题，一是调用一次只输出一个trace event，你需要不停地调用它；二是Write函数是同步的，如果没有可以输出的内容的话它会阻塞，所以你需要另起一个线程来调用Write函数。下面的代码启动WriteTraceProc线程并在60秒后结束capture trace。请注意traceServer.Close()必须在thread.Join()之前调用，否则WriteTraceProc线程可能会一直阻塞在Write函数调用上。&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: #2b91af; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;Thread&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt; thread = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Thread&lt;/SPAN&gt;(WriteTraceProc);&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;thread.Start(traceFile); &lt;SPAN style="COLOR: green"&gt;//pass traceFile as parameter&lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: #2b91af; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;Thread&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;.Sleep(60000);&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;lock&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt; (&lt;SPAN style="COLOR: #010001"&gt;flagLock&lt;/SPAN&gt;)&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;{&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #010001"&gt;exitFlag&lt;/SPAN&gt; = &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;}&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;traceServer.Close();&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;thread.Join();&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;traceFile.Close();&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;4、接着是flagLock，exitFlag的定义及WriteTraceProc的代码，WriteTraceProc将持续调用Write直到exitFlag被主线程置为true。&lt;BR&gt;
&lt;P align=left&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;private&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt; &lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; &lt;SPAN style="COLOR: #010001"&gt;flagLock&lt;/SPAN&gt; = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt;();&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;private static&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt; &lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt; exitFlag = &lt;SPAN style="COLOR: blue"&gt;false&lt;/SPAN&gt;;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;private&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt; &lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; WriteTraceProc(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; obj)&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;{&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;TraceFile&lt;/SPAN&gt; traceFile = (&lt;SPAN style="COLOR: #2b91af"&gt;TraceFile&lt;/SPAN&gt;)obj;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;while&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt; (&lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;)&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;{&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;lock&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #010001"&gt;flagLock&lt;/SPAN&gt;)&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;{&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #010001"&gt;exitFlag&lt;/SPAN&gt;)&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;break&lt;/SPAN&gt;;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;}&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #010001"&gt;traceFile&lt;/SPAN&gt;.&lt;SPAN style="COLOR: #010001"&gt;Write&lt;/SPAN&gt;();&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;}&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;}&lt;?xml:namespace prefix = o /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;5、最后是WriteHandler的代码，我们将使用EventClass列来过滤所有Audit Login及Audit Logout事件，你可以根据需要设置你的过滤条件。由于Books Online没有提供详细的文档，你也许需要使用IDataRecordChanger接口的GetName()来枚举所有你能使用的列。&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;private&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt; &lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; WriteHandler(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; sender, &lt;SPAN style="COLOR: #2b91af"&gt;TraceEventArgs&lt;/SPAN&gt; args)&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;{&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;IDataRecordChanger&lt;/SPAN&gt; recordChanger = args.CurrentRecord;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; eventClass = (&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;)recordChanger[&lt;SPAN style="COLOR: #a31515"&gt;"EventClass"&lt;/SPAN&gt;];&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (eventClass.StartsWith(&lt;SPAN style="COLOR: #a31515"&gt;"Audit"&lt;/SPAN&gt;))&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;args.SkipRecord = &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Arial','sans-serif'; mso-fareast-font-family: Fixedsys; mso-no-proof: yes; mso-font-kerning: 0pt"&gt;}&lt;/SPAN&gt;&lt;EM&gt;&amp;nbsp;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;待续&lt;/EM&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;软件开发工程师 徐进&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9318382" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/sqlcrd/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/sqlcrd/archive/tags/Trace+Management+Object/default.aspx">Trace Management Object</category></item></channel></rss>