﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-玄铁剑-文章分类-Debug</title><link>http://www.cnitblog.com/MartinYao/category/4630.html</link><description>成功的途径：抄，创造，研究，发明...</description><language>zh-cn</language><lastBuildDate>Mon, 26 Sep 2011 12:56:24 GMT</lastBuildDate><pubDate>Mon, 26 Sep 2011 12:56:24 GMT</pubDate><ttl>60</ttl><item><title>如何调试 Windows 服务</title><link>http://www.cnitblog.com/MartinYao/articles/21581.html</link><dc:creator>玄铁剑</dc:creator><author>玄铁剑</author><pubDate>Fri, 05 Jan 2007 12:29:00 GMT</pubDate><guid>http://www.cnitblog.com/MartinYao/articles/21581.html</guid><wfw:comment>http://www.cnitblog.com/MartinYao/comments/21581.html</wfw:comment><comments>http://www.cnitblog.com/MartinYao/articles/21581.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/MartinYao/comments/commentRss/21581.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/MartinYao/services/trackbacks/21581.html</trackback:ping><description><![CDATA[<h1 class=title><font size=2>Microsoft Debugging Tools for Windows<br><a href="http://msdl.microsoft.com/download/symbols/debuggers/dbg_x86_6.6.07.5.exe">http://msdl.microsoft.com/download/symbols/debuggers/dbg_x86_6.6.07.5.exe</a><br>注意：这篇文章是由无人工介入的自动的机器翻译系统翻译完成。这些文章是微软为不懂英语的用户提供的, 以使他们能够理解这些文章的内容。微软不保证机器翻译的正确度，也不对由于内容的误译或者客户对它的使用所引起的任何直接的, 或间接的可能的问题负责。</font> </h1>
<div class=articleProperty>
<table>
    <tbody>
        <tr>
            <td><font size=2>文章编号</font> </td>
            <td><font size=2>:</font> </td>
            <td><font size=2>824344</font> </td>
        </tr>
        <tr>
            <td><font size=2>最后修改</font> </td>
            <td><font size=2>:</font> </td>
            <td><font size=2>2006年12月8日</font> </td>
        </tr>
        <tr>
            <td><font size=2>修订</font> </td>
            <td><font size=2>:</font> </td>
            <td><font size=2>5.0</font> </td>
        </tr>
    </tbody>
</table>
</div>
<div class=notice><font size=2></font></div>
<div class=toc id=tocDiv style="DISPLAY: block">
<h5>本页</h5>
<div depth="1"><a onclick="return tocScrollTo(this);" href="http://support.microsoft.com/kb/824344#"><font size=2><img alt=概要 src="http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif"> <span class=tocTxt>概要</span> </font></a><font size=2></font>
<div class=tocLine>
<div depth="2"><a onclick="return tocScrollTo(this);" href="http://support.microsoft.com/kb/824344#"><font size=2><img alt=要求 src="http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif"> <span class=tocTxt>要求</span> </font></a><font size=2></font>
<div class=tocLine></div>
</div>
<div depth="2"><a onclick="return tocScrollTo(this);" href="http://support.microsoft.com/kb/824344#"><font size=2><img alt="服务启动后 WinDbg 调试器附加到服务" src="http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif"> <span class=tocTxt>服务启动后 WinDbg 调试器附加到服务</span> </font></a><font size=2></font>
<div class=tocLine>
<div depth="3"><a onclick="return tocScrollTo(this);" href="http://support.microsoft.com/kb/824344#"><font size=2><img alt="使用进程 ID 的宿主服务， 您要调试进程" src="http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif"> <span class=tocTxt>使用进程 ID 的宿主服务， 您要调试进程</span> </font></a><font size=2></font>
<div class=tocLine>
<div depth="4"><a onclick="return tocScrollTo(this);" href="http://support.microsoft.com/kb/824344#"><font size=2><img alt="方法 1： 使用任务管理器" src="http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif"> <span class=tocTxt>方法 1： 使用任务管理器</span> </font></a><font size=2></font>
<div class=tocLine></div>
</div>
<div depth="4"><a onclick="return tocScrollTo(this);" href="http://support.microsoft.com/kb/824344#"><font size=2><img alt="方法 2： 使用任务列表工具 (Tlist.exe)" src="http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif"> <span class=tocTxt>方法 2： 使用任务列表工具 (Tlist.exe)</span> </font></a><font size=2></font>
<div class=tocLine></div>
</div>
</div>
</div>
<div depth="3"><a onclick="return tocScrollTo(this);" href="http://support.microsoft.com/kb/824344#"><font size=2><img alt="使用进程承载服务， 您要调试的图像名称" src="http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif"> <span class=tocTxt>使用进程承载服务， 您要调试的图像名称</span> </font></a><font size=2></font>
<div class=tocLine></div>
</div>
<div depth="3"><a onclick="return tocScrollTo(this);" href="http://support.microsoft.com/kb/824344#"><font size=2><img alt="启动 WinDbg 调试器并附加到宿主服务， 您要调试进程" src="http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif"> <span class=tocTxt>启动 WinDbg 调试器并附加到宿主服务， 您要调试进程</span> </font></a><font size=2></font>
<div class=tocLine></div>
</div>
</div>
</div>
<div depth="2"><a onclick="return tocScrollTo(this);" href="http://support.microsoft.com/kb/824344#"><font size=2><img alt="配置要使用附加 WinDbg 调试器启动服务 " src="http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif"> <span class=tocTxt>配置要使用附加 WinDbg 调试器启动服务 </span></font></a>
<div class=tocLine>
<div depth="4"><a onclick="return tocScrollTo(this);" href="http://support.microsoft.com/kb/824344#"><font size=2><img alt="方法 1： 使用全局标志编辑器 (gflags.exe)" src="http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif"> <span class=tocTxt>方法 1： 使用全局标志编辑器 (gflags.exe)</span> </font></a><font size=2></font>
<div class=tocLine></div>
</div>
<div depth="4"><a onclick="return tocScrollTo(this);" href="http://support.microsoft.com/kb/824344#"><font size=2><img alt="方法 2： 使用注册表编辑器" src="http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif"> <span class=tocTxt>方法 2： 使用注册表编辑器</span> </font></a><font size=2></font>
<div class=tocLine></div>
</div>
<div depth="4"><a onclick="return tocScrollTo(this);" href="http://support.microsoft.com/kb/824344#"><font size=2><img alt="方法 1： 使用服务控制台" src="http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif"> <span class=tocTxt>方法 1： 使用服务控制台</span> </font></a><font size=2></font>
<div class=tocLine></div>
</div>
<div depth="4"><a onclick="return tocScrollTo(this);" href="http://support.microsoft.com/kb/824344#"><font size=2><img alt="方法 2： 使用注册表编辑器" src="http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif"> <span class=tocTxt>方法 2： 使用注册表编辑器</span> </font></a><font size=2></font>
<div class=tocLine></div>
</div>
</div>
</div>
<div depth="2"><a onclick="return tocScrollTo(this);" href="http://support.microsoft.com/kb/824344#"><font size=2><img alt=疑难解答 src="http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif"> <span class=tocTxt>疑难解答</span> </font></a><font size=2></font>
<div class=tocLine></div>
</div>
</div>
</div>
<div depth="1"><a onclick="return tocScrollTo(this);" href="http://support.microsoft.com/kb/824344#"><font size=2><img alt=参考 src="http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif"> <span class=tocTxt>参考</span> </font></a><font size=2></font>
<div class=tocLine></div>
</div>
</div>
<script type="text/javascript"><![CDATA[
      var sectionFilter = "type != 'notice' && type != 'securedata' && type != 'querywords'";
      var tocArrow = "/library/images/support/kbgraphics/public/en-us/downarrow.gif";
      var depthLimit = 10;
      var depth3Limit = 10;
      var depth4Limit = 5;
      var depth5Limit = 3;
      var tocEntryMinimum = 1;
    ]]&gt;</script><script src="/common/script/gsfx/kbtoc.js??4" type="text/javascript">
		</script><noscript></noscript>
<div class=section>
<h2 class=subTitle id=tocHeadRef><font size=2>概要</font> </h2>
<script type="text/javascript"><![CDATA[oadTOCNode(1, 'summary');]]&gt;</script>
<div class=sbody><font size=2>本文介绍如何通过使用 WinDbg 调试器 (windbg.exe) 调试 Windows 服务。 要调试 Windows 服务, 您可以附加到进程承载服务, 服务启动后 WinDbg 调试器也可以配置服务以启动使用 WinDbg 调试器附加以便可解决服务启动 - 相关问题。 本文介绍这两种方法。 <br><br></font>
<div class=topOfPage>
<table>
    <tbody>
        <tr>
            <td class=image><a href="http://support.microsoft.com/kb/824344#top"><font size=2><img title=回到顶端 alt=回到顶端 src="http://support.microsoft.com/library/images/support/en-us/uparrow.gif"> </font></a></td>
            <td class=text><a href="http://support.microsoft.com/kb/824344#top"><font size=2>回到顶端</font> </a></td>
        </tr>
    </tbody>
</table>
</div>
<h3 id=tocHeadRef><span><a id=3></a></span><font size=2>要求</font> </h3>
<script type="text/javascript"><![CDATA[oadTOCNode(2, 'summary');]]&gt;</script><font size=2>本文假定您已熟悉以下主题： </font>
<table class="list ul">
    <tbody>
        <tr>
            <td class=bullet><font size=2>&#8226;</font> </td>
            <td class=text><font size=2>Windows 服务</font> </td>
        </tr>
        <tr>
            <td class=bullet><font size=2>&#8226;</font> </td>
            <td class=text><font size=2>WinDbg 调试器</font> </td>
        </tr>
    </tbody>
</table>
<div class=topOfPage>
<table>
    <tbody>
        <tr>
            <td class=image><a href="http://support.microsoft.com/kb/824344#top"><font size=2><img title=回到顶端 alt=回到顶端 src="http://support.microsoft.com/library/images/support/en-us/uparrow.gif"> </font></a></td>
            <td class=text><a href="http://support.microsoft.com/kb/824344#top"><font size=2>回到顶端</font> </a></td>
        </tr>
    </tbody>
</table>
</div>
<h3 id=tocHeadRef><span><a id=4></a></span><font size=2>服务启动后 WinDbg 调试器附加到服务</font> </h3>
<script type="text/javascript"><![CDATA[oadTOCNode(2, 'summary');]]&gt;</script><font size=2>此方法是类似于方法， 您可用于将调试器附加到进程和然后调试进程。 </font>
<h4 id=tocHeadRef><span><a id=5></a></span><font size=2>使用进程 ID 的宿主服务， 您要调试进程</font> </h4>
<script type="text/javascript"><![CDATA[oadTOCNode(3, 'summary');]]&gt;</script>
<table class="list ol">
    <tbody>
        <tr>
            <td class=number><font size=2>1.</font> </td>
            <td class=text><font size=2>确定进程的宿主服务， 您要调试, 进程 ID (PID) 使用下列方法之一。 </font>
            <table class="list ul">
                <tbody>
                    <tr>
                        <td class=bullet><font size=2>&#8226;</font> </td>
                        <td class=text>
                        <h5 id=tocHeadRef>方法 1： 使用任务管理器</h5>
                        <script type="text/javascript"><![CDATA[oadTOCNode(4, 'summary');]]&gt;</script>
                        <table class="list al">
                            <tbody>
                                <tr>
                                    <td class=number><font size=2>a. </font></td>
                                    <td class=text><font size=2>右键单击任务栏, 然后单击 任务管理器 。 Windows 任务管理器 对话框出现。 </font></td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>b. </font></td>
                                    <td class=text><font size=2>单击 进程 选项卡的 Windows 任务管理器 对话框中。 </font></td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>c. </font></td>
                                    <td class=text><font size=2>映像名称 , 单击进程承载服务， 您要调试的图像名称。 注意此过程是由相应 PID 字段的值指定的进程 ID。 </font></td>
                                </tr>
                            </tbody>
                        </table>
                        </td>
                    </tr>
                    <tr>
                        <td class=bullet><font size=2>&#8226;</font> </td>
                        <td class=text>
                        <h5 id=tocHeadRef>方法 2： 使用任务列表工具 (Tlist.exe)</h5>
                        <script type="text/javascript"><![CDATA[oadTOCNode(4, 'summary');]]&gt;</script>
                        <table class="list al">
                            <tbody>
                                <tr>
                                    <td class=number><font size=2>a. </font></td>
                                    <td class=text><font size=2>开始 , 依次 运行 。 出现 运行 对话框。 </font></td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>b. </font></td>
                                    <td class=text><font size=2>在 打开 框中, 键入 cmd , 然后单击 确定 。 </font></td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>c. </font></td>
                                    <td class=text><font size=2>在命令提示符下, 更改目录路径以反映计算机上的 tlist.exe 文件位置。 <br><br><strong>注意 </strong>tlist.exe 文件通常位于以下目录： C:\ProgramFiles\Microsoft Files\Debugging 工具用于 Windows </font></td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>d. </font></td>
                                    <td class=text><font size=2>在命令提示符处, 键入 tlist 要列出图像名称和过程的所有计算机上当前运行的进程 ID。 <br><br><strong>注意 </strong>使记录的宿主服务， 您要调试进程的进程 ID。 </font></td>
                                </tr>
                            </tbody>
                        </table>
                        </td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
        <tr>
            <td class=number><font size=2>2.</font> </td>
            <td class=text><font size=2>在命令提示符处, 更改目录路径以反映计算机上的 windbg.exe 文件位置。 <br><br><strong>注意 </strong>如果命令提示符是未打开, 按照和 b 的方法 1。 windbg.exe 文件通常位于以下目录： C:\ProgramFiles\Microsoft Files\Debugging 工具用于 Windows。 </font></td>
        </tr>
        <tr>
            <td class=number><font size=2>3.</font> </td>
            <td class=text><font size=2>在命令提示符处, 键入 <span class=userInput>/ windbg &#251;p ProcessID g </span>将 WinDbg 调试器附加到宿主服务， 您要调试进程。 <br><br><strong>注意 </strong><var>ProcessID </var>是占位符代表进程承载服务， 您要调试的进程 ID。 </font></td>
        </tr>
    </tbody>
</table>
<h4 id=tocHeadRef><span><a id=6></a></span><font size=2>使用进程承载服务， 您要调试的图像名称</font> </h4>
<script type="text/javascript"><![CDATA[oadTOCNode(3, 'summary');]]&gt;</script><font size=2>只有没有的宿主服务， 要运行进程一个运行实例您可使用此方法。 要这样做, 请按照下列步骤操作： </font>
<table class="list ol">
    <tbody>
        <tr>
            <td class=number><font size=2>1.</font> </td>
            <td class=text><font size=2>开始 , 依次 运行 。 出现 运行 对话框。 </font></td>
        </tr>
        <tr>
            <td class=number><font size=2>2.</font> </td>
            <td class=text><font size=2>在 打开 框中, 键入 cmd , 然后单击 确定 要打开命令提示符。 </font></td>
        </tr>
        <tr>
            <td class=number><font size=2>3.</font> </td>
            <td class=text><font size=2>在命令提示符下, 更改目录路径以反映计算机上的 windbg.exe 文件位置。 <br><br><strong>注意 </strong>windbg.exe 文件通常位于以下目录： C:\ProgramFiles\Microsoft Files\Debugging 工具用于 Windows。 </font></td>
        </tr>
        <tr>
            <td class=number><font size=2>4.</font> </td>
            <td class=text><font size=2>在命令提示符处, 键入 <span class=userInput>/ windbg &#251;pn ImageName g </span>将 WinDbg 调试器附加到宿主服务， 您要调试进程。 <br><br><strong>注意 </strong><var>ImageName </var>是占位符代表的宿主服务， 您要调试进程图像名称。 " - pn 命令行选项指定 <var>ImageName </var>命令行参数是的进程图像名称 "。 </font></td>
        </tr>
    </tbody>
</table>
<h4 id=tocHeadRef><span><a id=7></a></span><font size=2>启动 WinDbg 调试器并附加到宿主服务， 您要调试进程</font> </h4>
<script type="text/javascript"><![CDATA[oadTOCNode(3, 'summary');]]&gt;</script>
<table class="list ol">
    <tbody>
        <tr>
            <td class=number><font size=2>1.</font> </td>
            <td class=text><font size=2>启动 Windows 资源管理器。</font> </td>
        </tr>
        <tr>
            <td class=number><font size=2>2.</font> </td>
            <td class=text><font size=2>计算机上定位 windbg.exe 文件。<br><br><strong>注意 </strong>windbg.exe 文件通常位于以下目录： C:\ProgramFiles\Microsoft Files\Debugging 工具用于 Windows </font></td>
        </tr>
        <tr>
            <td class=number><font size=2>3.</font> </td>
            <td class=text><font size=2>运行与 / g 命令行开关来启动 WinDbg 调试器一起 windbg.exe 文件。 命令行开关 / g 允许跟踪过程来设置断点后继续。 </font></td>
        </tr>
        <tr>
            <td class=number><font size=2>4.</font> </td>
            <td class=text><font size=2>在 文件 菜单上, 单击以显示 附加到进程 对话框 附加到进程 。 </font></td>
        </tr>
        <tr>
            <td class=number><font size=2>5.</font> </td>
            <td class=text><font size=2>单击以选中对应到进程该主机服务， 您要调试, 节点， 然后单击 确定 。 </font></td>
        </tr>
        <tr>
            <td class=number><font size=2>6.</font> </td>
            <td class=text><font size=2>在对话框中出现, 单击 是 以保存基区信息。 注意， 现在可以调试 disassembled 代码为您服务。 </font></td>
        </tr>
    </tbody>
</table>
<div class=topOfPage>
<table>
    <tbody>
        <tr>
            <td class=image><a href="http://support.microsoft.com/kb/824344#top"><font size=2><img title=回到顶端 alt=回到顶端 src="http://support.microsoft.com/library/images/support/en-us/uparrow.gif"> </font></a></td>
            <td class=text><a href="http://support.microsoft.com/kb/824344#top"><font size=2>回到顶端</font> </a></td>
        </tr>
    </tbody>
</table>
</div>
<h3 id=tocHeadRef><span><a id=8></a></span><font size=2>配置要使用附加 WinDbg 调试器启动服务 </font></h3>
<script type="text/javascript"><![CDATA[oadTOCNode(2, 'summary');]]&gt;</script><font size=2>您可使用此方法来调试服务如果您想解决服务启动 - 相关问题。 </font>
<table class="list ol">
    <tbody>
        <tr>
            <td class=number><font size=2>1.</font> </td>
            <td class=text><font size=2>配置 " 图像文件执行 " 选项。 要这样做, 使用下列方法之一： </font>
            <table class="list ul">
                <tbody>
                    <tr>
                        <td class=bullet><font size=2>&#8226;</font> </td>
                        <td class=text>
                        <h5 id=tocHeadRef>方法 1： 使用全局标志编辑器 (gflags.exe)</h5>
                        <script type="text/javascript"><![CDATA[oadTOCNode(4, 'summary');]]&gt;</script>
                        <table class="list al">
                            <tbody>
                                <tr>
                                    <td class=number><font size=2>a. </font></td>
                                    <td class=text><font size=2>启动 Windows 资源管理器。</font> </td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>b. </font></td>
                                    <td class=text><font size=2>计算机上定位 gflags.exe 文件。 <br><br><strong>注意 </strong>gflags.exe 文件通常位于以下目录： C:\ProgramFiles\Microsoft Files\Debugging 工具用于 Windows。 </font></td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>c. </font></td>
                                    <td class=text><font size=2>运行 gflags.exe 文件以启动全局标志编辑器。 </font></td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>d. </font></td>
                                    <td class=text><font size=2>在 图像文件名称 文本框中, 键入的宿主服务， 您要调试进程图像名称。 如果要调试服务是由具有作为图像名称, MyService.exe 进程宿主例如, 键入 MyService.exe 。 </font></td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>e. </font></td>
                                    <td class=text><font size=2>在 目标 , 单击以选中 图像文件选项 选项。 </font></td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>f. </font></td>
                                    <td class=text><font size=2>在 图像调试器选项 , 单击以选中 调试器 复选框。 </font></td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>g. </font></td>
                                    <td class=text><font size=2>调试器 文本框中, 键入调试器， 您要使用的完整路径。 例如, 如果要使用 WinDbg 调试器来调试服务, 可键入完整路径是类似于以下： Windows\windbg.exe C:\ProgramFiles\Microsoft Files\Debugging 工具 Windows\windbg.exe C:\ProgramFiles\Microsoft Files\Debugging 工具 ： </font></td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>h. </font></td>
                                    <td class=text><font size=2>单击 应用 , 然后单击 确定 以退出全局标志编辑器。 </font></td>
                                </tr>
                            </tbody>
                        </table>
                        </td>
                    </tr>
                    <tr>
                        <td class=bullet><font size=2>&#8226;</font> </td>
                        <td class=text>
                        <h5 id=tocHeadRef>方法 2： 使用注册表编辑器</h5>
                        <script type="text/javascript"><![CDATA[oadTOCNode(4, 'summary');]]&gt;</script>
                        <table class="list al">
                            <tbody>
                                <tr>
                                    <td class=number><font size=2>a. </font></td>
                                    <td class=text><font size=2>开始 , 依次 运行 。 出现 运行 对话框。 </font></td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>b. </font></td>
                                    <td class=text><font size=2>在 打开 框中, 键入 regedit , 然后单击 确定 以启动注册表编辑器。 </font></td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>c. </font></td>
                                    <td class=text><font size=2><strong>警告 </strong>如果正确修改注册表通过注册表编辑器或通过其他方法可能发生 Serious 问题。 这些问题可能需要重新安装操作系统。 Microsoft 不能保证能够解决这些问题而。 修改注册表需要您自担风险。<br><br>在注册表编辑器, 查找, 并右以下注册表子项： </font>
                                    <div class=indent><font size=2>HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options</font> </div>
                                    </td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>d. </font></td>
                                    <td class=text><font size=2>依次指向 新建 , 密钥 。 在左窗格中的注册表编辑器, 注意 新项 # 1 的新注册表子项 （名称）， 将选中进行编辑。 </font></td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>e. </font></td>
                                    <td class=text><font size=2>类型 <span class=userInput><var>ImageName</var></span> 要替换 新项 # 1 , 并按 ENTER 键。 <br><br><strong>注意 </strong><var>ImageName </var>是占位符代表的宿主服务， 您要调试进程图像名称。 如果要调试服务是由具有作为图像名称, MyService.exe 进程宿主例如, 键入 <span class=userInput>MyService.exe </span>。 </font></td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>f. </font></td>
                                    <td class=text><font size=2>右击步骤 e 中创建注册表子项。 </font></td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>g. </font></td>
                                    <td class=text><font size=2>指向 New , 然后单击 字符串值 。 在右窗格中的注册表编辑器, 注意， 将 新值 # 1 的新注册表项, 名称, 选中进行编辑。 </font></td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>h. </font></td>
                                    <td class=text><font size=2>调试器 , 替换 新值 # 1 ， 然后按 Enter。 </font></td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>i. </font></td>
                                    <td class=text><font size=2>右键单击与您在步骤 h, 创建 调试器 注册表项， 然后单击 修改 。 出现 编辑字符串 对话框。 </font></td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>j. </font></td>
                                    <td class=text><font size=2>在 数值数据 文本框中, 键入 <span class=userInput><var>DebuggerPath</var></span> 然后单击 确定 。 <br><br><strong>注意 </strong><var>DebuggerPath </var>是占位符代表调试器， 您要使用完整路径。 例如, 如果要使用 WinDbg 调试器来调试服务, 键入完整路径是与以下类似： </font>
                                    <div class=indent><span class=userInput><font size=2>C:\Progra~1\Debugg~1\windbg.exe</font> </span></div>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                        </td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
        <tr>
            <td class=number><font size=2>2.</font> </td>
            <td class=text><font size=2>对于可以出现在桌面上, 并可以与其调试器, 调试器窗口服务进行交互。 如果您不要使服务交互式, 调试器将启动但无法看到它并且无法发出命令。 交互式, 使您的服务使用下列方法之一： </font>
            <table class="list ul">
                <tbody>
                    <tr>
                        <td class=bullet><font size=2>&#8226;</font> </td>
                        <td class=text>
                        <h5 id=tocHeadRef>方法 1： 使用服务控制台</h5>
                        <script type="text/javascript"><![CDATA[oadTOCNode(4, 'summary');]]&gt;</script>
                        <table class="list al">
                            <tbody>
                                <tr>
                                    <td class=number><font size=2>a. </font></td>
                                    <td class=text><font size=2>单击 开始 , 然后指向 程序 。 </font></td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>b. </font></td>
                                    <td class=text><font size=2>在 程序 菜单, 指向 管理工具 , 然后单击 服务 。 出现 服务 控制台。 </font></td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>c. </font></td>
                                    <td class=text><font size=2>在右窗格中的 服务 控制台, 右键单击 <strong class=uiterm><var>ServiceName</var></strong> 然后单击 属性 。 <br><br><strong>注意 </strong><var>ServiceName </var>是占位符代表服务， 您要调试的名称。 </font></td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>d. </font></td>
                                    <td class=text><font size=2>在 登录 选项卡, 单击以选中 允许服务与桌面交互以 复选框在 LocalSystem 帐户 , 然后单击 确定 。 </font></td>
                                </tr>
                            </tbody>
                        </table>
                        </td>
                    </tr>
                    <tr>
                        <td class=bullet><font size=2>&#8226;</font> </td>
                        <td class=text>
                        <h5 id=tocHeadRef>方法 2： 使用注册表编辑器</h5>
                        <script type="text/javascript"><![CDATA[oadTOCNode(4, 'summary');]]&gt;</script>
                        <table class="list al">
                            <tbody>
                                <tr>
                                    <td class=number><font size=2>a. </font></td>
                                    <td class=text><font size=2>中注册表编辑器, 找到, 并单击以下注册表子项： </font>
                                    <div class=indent><font size=2>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\ServiceName</font> </div>
                                    <font size=2>替换为对要调试服务名称 <strong>注意 </strong>替换 <var>ServiceName </var>。 例如, 如果要调试服务名 MyService, 找到并单击以下注册表项： </font>
                                    <div class=indent><font size=2>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MyService</font> </div>
                                    </td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>b. </font></td>
                                    <td class=text><font size=2>在右窗格中的注册表编辑器, Name 字段下 类型 , 右击， 然后单击 修改 。 出现 编辑 DWORD 值 对话框。 </font></td>
                                </tr>
                                <tr>
                                    <td class=number><font size=2>c. </font></td>
                                    <td class=text><font size=2>将 数值数据 文本框中文字更改为二进制 OR 操作的结果与二进制值当前文本和二进制值, 0x00000100, 作为两个操作数。 二进制值, 0x00000100, 对应于计算机上 WinNT.h 头文件中定义 SERVICE_INTERACTIVE_PROCESS 常量。 该常量指定一个服务是特性中交互式。 </font></td>
                                </tr>
                            </tbody>
                        </table>
                        </td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
        <tr>
            <td class=number><font size=2>3.</font> </td>
            <td class=text><font size=2>当服务启动, 服务通讯多久到服务控制管理器服务必须具有要启动超时期 （用于服务)。 如果服务控制管理器没有收到启动 " 服务 " 通知来自服务超时内, 这服务控制管理器终止进程承载服务。 此超时期限通常少于 30 秒。 您正试图调试时如果您不要调整此超时期限, 服务控制管理器结束进程并附加调试器。 若要调整此超时期限, 请按照下列步骤： </font>
            <table class="list al">
                <tbody>
                    <tr>
                        <td class=number><font size=2>a. </font></td>
                        <td class=text><font size=2>在注册表编辑器, 查找, 并右以下注册表子项： </font>
                        <div class=indent><font size=2>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control</font> </div>
                        </td>
                    </tr>
                    <tr>
                        <td class=number><font size=2>b. </font></td>
                        <td class=text><font size=2>指向 New , 然后单击 DWORD 值 。 在右窗格中的注册表编辑器, 注意 新值 # 1 （名称） 的新注册表项， 将选中进行编辑。 </font></td>
                    </tr>
                    <tr>
                        <td class=number><font size=2>c. </font></td>
                        <td class=text><font size=2>键入 ServicesPipeTimeout 以替换 新值 # 1 , 并按 ENTER。 </font></td>
                    </tr>
                    <tr>
                        <td class=number><font size=2>d. </font></td>
                        <td class=text><font size=2>右键单击与您在步骤 c, 创建 ServicesPipeTimeout 注册表项， 然后单击 修改 。 出现 编辑 DWORD 值 对话框。 </font></td>
                    </tr>
                    <tr>
                        <td class=number><font size=2>e. </font></td>
                        <td class=text><font size=2>在 数值数据 文本框中, 键入 <span class=userInput><var>TimeoutPeriod</var></span> 然后单击 确定 <br><br><strong>注意 </strong><var>TimeoutPeriod </var>是占位符代表超时期 （毫秒） 对要为服务设置值。 如果要设置超时期到 24 小时 （86400000 毫秒）, 例如, 键入 <span class=userInput>86400000 </span>。 </font></td>
                    </tr>
                    <tr>
                        <td class=number><font size=2>f. </font></td>
                        <td class=text><font size=2>重新启动计算机。 您必须重新启动计算机对服务控制管理器以应用此更改。</font> </td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
        <tr>
            <td class=number><font size=2>4.</font> </td>
            <td class=text><font size=2>启动 Windows 服务。 要这样做, 请按照下列步骤操作： </font>
            <table class="list al">
                <tbody>
                    <tr>
                        <td class=number><font size=2>a. </font></td>
                        <td class=text><font size=2>单击 开始 , 然后指向 程序 。 </font></td>
                    </tr>
                    <tr>
                        <td class=number><font size=2>b. </font></td>
                        <td class=text><font size=2>在 程序 菜单, 指向 管理工具 , 然后单击 服务 。 出现 服务 控制台。 </font></td>
                    </tr>
                    <tr>
                        <td class=number><font size=2>c. </font></td>
                        <td class=text><font size=2>在右窗格中的 服务 控制台, 右键单击 <strong class=uiterm><var>ServiceName</var></strong> 然后单击 开始 。 <br><br><strong>注意 </strong><var>ServiceName </var>是占位符代表服务， 您要调试的名称。 </font></td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
    </tbody>
</table>
<div class=topOfPage>
<table>
    <tbody>
        <tr>
            <td class=image><a href="http://support.microsoft.com/kb/824344#top"><font size=2><img title=回到顶端 alt=回到顶端 src="http://support.microsoft.com/library/images/support/en-us/uparrow.gif"> </font></a></td>
            <td class=text><a href="http://support.microsoft.com/kb/824344#top"><font size=2>回到顶端</font> </a></td>
        </tr>
    </tbody>
</table>
</div>
<h3 id=tocHeadRef><span><a id=9></a></span><font size=2>疑难解答</font> </h3>
<script type="text/javascript"><![CDATA[oadTOCNode(2, 'summary');]]&gt;</script><font size=2>尝试通过网络, 调试服务之前确保该符号和该服务使用源文件是从其中将运行服务计算机访问。 要这样做, 使用下列方法之一： </font>
<table class="list ul">
    <tbody>
        <tr>
            <td class=bullet><font size=2>&#8226;</font> </td>
            <td class=text><font size=2>向所有人对您计算机上包含符号和该服务使用源文件文件夹授予至少读取访问权限。 </font></td>
        </tr>
        <tr>
            <td class=bullet><font size=2>&#8226;</font> </td>
            <td class=text><font size=2>将这些符号和该服务使用源文件复制到计算机将运行服务。 </font></td>
        </tr>
    </tbody>
</table>
<span><a id=10></a></span>
<div class=topOfPage>
<table>
    <tbody>
        <tr>
            <td class=image><a href="http://support.microsoft.com/kb/824344#top"><font size=2><img title=回到顶端 alt=回到顶端 src="http://support.microsoft.com/library/images/support/en-us/uparrow.gif"> </font></a></td>
            <td class=text><a href="http://support.microsoft.com/kb/824344#top"><font size=2>回到顶端</font> </a></td>
        </tr>
    </tbody>
</table>
</div>
</div>
<h2 class=subTitle id=tocHeadRef><font size=2>参考</font> </h2>
<script type="text/javascript"><![CDATA[oadTOCNode(1, 'references');]]&gt;</script>
<div class=sbody><font size=2>请要下载工具和实用程序, 本文中提到访问以下 MicrosoftWeb 站点： </font>
<div class=indent><font size=2>用于 Windows 调试工具： 概述<br></font><a href="http://www.microsoft.com/whdc/devtools/debugging/default.mspx"><font size=2>http://www.microsoft.com/whdc/devtools/debugging/default.mspx</font> </a><span class=pLink><font size=2>(http://www.microsoft.com/whdc/devtools/debugging/default.mspx)</font> </span></div>
<font size=2>请, 有关访问下列 Microsoft Developer Network (MSDN) Web 站点： </font>
<div class=indent><font size=2>WinDbg 调试器<br></font><a href="http://www.microsoft.com/whdc/devtools/debugging/installx86.mspx"><font size=2>http://www.microsoft.com/whdc/devtools/debugging/installx86.mspx</font> </a><span class=pLink><font size=2>(http://www.microsoft.com/whdc/devtools/debugging/installx86.mspx)</font> </span></div>
<div class=indent><font size=2>服务<br></font><a href="http://msdn.microsoft.com/library/en-us/dllproc/base/services.asp"><font size=2>http://msdn.microsoft.com/library/en-us/dllproc/base/services.asp</font> </a><span class=pLink><font size=2>(http://msdn.microsoft.com/library/en-us/dllproc/base/services.asp)</font> </span></div>
<div class=indent><font size=2>TList： 任务列表应用程序示例 <br></font><a href="http://msdn.microsoft.com/library/en-us/vcsample98/html/vcsmptlist.asp"><font size=2>http://msdn.microsoft.com/library/en-us/vcsample98/html/vcsmptlist.asp</font> </a><span class=pLink><font size=2>(http://msdn.microsoft.com/library/en-us/vcsample98/html/vcsmptlist.asp)</font> </span></div>
<div class=indent><font size=2>使用全局标志实用程序 <br><span class=ll><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devtest_g/hh/DevTest_g/DV_66c1db5c-fbc6-43a9-b318-4b3c0cda1009.xml.asp">http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devtest_g/hh/DevTest_g/DV_66c1db5c-fbc6-43a9-b318-4b3c0cda1009.xml.asp</a></span><span class=pLink> (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devtest_g/hh/DevTest_g/DV_66c1db5c-fbc6-43a9-b318-4b3c0cda1009.xml.asp)</span></font> </div>
<div class=topOfPage>
<table>
    <tbody>
        <tr>
            <td class=image><a href="http://support.microsoft.com/kb/824344#top"><font size=2><img title=回到顶端 alt=回到顶端 src="http://support.microsoft.com/library/images/support/en-us/uparrow.gif"> </font></a></td>
            <td class=text><a href="http://support.microsoft.com/kb/824344#top"><font size=2>回到顶端</font> </a></td>
        </tr>
    </tbody>
</table>
</div>
</div>
</div>
<div class=appliesTo><font size=2>
<hr>
</font><a id=appliesto></a>
<h5>这篇文章中的信息适用于:</h5>
<table class=list>
    <tbody>
        <tr>
            <td class=bullet><font size=2>&#8226;</font> </td>
            <td class=text><font size=2>Microsoft Windows 2000 Server</font> </td>
        </tr>
        <tr>
            <td class=bullet><font size=2>&#8226;</font> </td>
            <td class=text><font size=2>Microsoft Windows 2000 Advanced Server</font> </td>
        </tr>
        <tr>
            <td class=bullet><font size=2>&#8226;</font> </td>
            <td class=text><font size=2>Microsoft Windows 2000 Datacenter Server</font> </td>
        </tr>
        <tr>
            <td class=bullet><font size=2>&#8226;</font> </td>
            <td class=text><font size=2>Microsoft Windows 2000 Professional Edition</font> </td>
        </tr>
        <tr>
            <td class=bullet><font size=2>&#8226;</font> </td>
            <td class=text><font size=2>Microsoft Windows Server 2003, Enterprise Edition (32-bit x86)</font> </td>
        </tr>
        <tr>
            <td class=bullet><font size=2>&#8226;</font> </td>
            <td class=text><font size=2>Microsoft Windows Server 2003, Standard Edition (32-bit x86)</font> </td>
        </tr>
        <tr>
            <td class=bullet><font size=2>&#8226;</font> </td>
            <td class=text><font size=2>Microsoft Windows Server 2003, Datacenter Edition (32-bit x86)</font> </td>
        </tr>
        <tr>
            <td class=bullet><font size=2>&#8226;</font> </td>
            <td class=text><font size=2>Microsoft Windows XP Home Edition</font> </td>
        </tr>
        <tr>
            <td class=bullet><font size=2>&#8226;</font> </td>
            <td class=text><font size=2>Microsoft Windows XP Home Edition</font> </td>
        </tr>
        <tr>
            <td class=bullet><font size=2>&#8226;</font> </td>
            <td class=text><font size=2>Microsoft Windows XP Service Pack 1a</font> </td>
        </tr>
        <tr>
            <td class=bullet><font size=2>&#8226;</font> </td>
            <td class=text><font size=2>Microsoft Windows XP Home Edition</font> </td>
        </tr>
        <tr>
            <td class=bullet><font size=2>&#8226;</font> </td>
            <td class=text><font size=2>Microsoft Windows XP Professional</font> </td>
        </tr>
        <tr>
            <td class=bullet><font size=2>&#8226;</font> </td>
            <td class=text><font size=2>Microsoft Windows XP Professional</font> </td>
        </tr>
        <tr>
            <td class=bullet><font size=2>&#8226;</font> </td>
            <td class=text><font size=2>Microsoft Windows XP Service Pack 1a</font> </td>
        </tr>
        <tr>
            <td class=bullet><font size=2>&#8226;</font> </td>
            <td class=text><font size=2>Microsoft Windows XP Professional</font> </td>
        </tr>
    </tbody>
</table>
</div>
<img src ="http://www.cnitblog.com/MartinYao/aggbug/21581.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/MartinYao/" target="_blank">玄铁剑</a> 2007-01-05 20:29 <a href="http://www.cnitblog.com/MartinYao/articles/21581.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Windows Service Debugger</title><link>http://www.cnitblog.com/MartinYao/articles/21510.html</link><dc:creator>玄铁剑</dc:creator><author>玄铁剑</author><pubDate>Thu, 04 Jan 2007 13:47:00 GMT</pubDate><guid>http://www.cnitblog.com/MartinYao/articles/21510.html</guid><wfw:comment>http://www.cnitblog.com/MartinYao/comments/21510.html</wfw:comment><comments>http://www.cnitblog.com/MartinYao/articles/21510.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/MartinYao/comments/commentRss/21510.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/MartinYao/services/trackbacks/21510.html</trackback:ping><description><![CDATA[
		<ul class="download">
				<li>
						<a href="http://www.codeproject.com/Purgatory/WindowsServiceDebugger/WindowsServiceHelper.zip">WindowsServiceDebugger - 16.5 Kb</a>
				</li>
		</ul>
		<!-- Add the rest of your HTML here -->
		<h2>Introduction</h2>
		<h1 style="MARGIN: 0in 0in 0pt">
				<u>
						<font face="Times New Roman" size="3">Debugging a Windows service using a VS.NET macro</font>
				</u>
		</h1>
		<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">
				<u>
						<font face="Times New Roman">
								<font size="3"> <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?><o:p></o:p></font>
						</font>
				</u>
		</p>
		<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">
				<font face="Times New Roman" size="3">While I started writing windows services I wished  Microsoft provided a way to debug with a single click as with the other project types.</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">
				<font face="Times New Roman" size="3">I recently started reading about macros and here is a macro that can debug a windows service.</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">
				<font face="Times New Roman">
						<font size="3"> <o:p></o:p></font>
				</font>
		</p>
		<h1 style="MARGIN: 0in 0in 0pt">
				<u>
						<font face="Times New Roman" size="3">Running the macro</font>
				</u>
		</h1>
		<ol style="MARGIN-TOP: 0in" type="1">
				<li class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in">
						<font face="Times New Roman" size="3">Open Windows Service project you want to debug in VS.NET</font>
				</li>
				<li class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in">
						<font face="Times New Roman" size="3">Open Macro Explorer.<span style="mso-spacerun: yes">  Tools -&gt; Macros -&gt; Macro Explorer</span></font>
				</li>
				<li class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in">
						<font face="Times New Roman" size="3">Load Macro Project in Macro Explorer</font>
				</li>
				<li class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in">
						<font face="Times New Roman" size="3">Double click on “Debug” to run the macro.</font>
				</li>
		</ol>
		<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">
				<font face="Times New Roman">
						<font size="3"> <o:p></o:p></font>
				</font>
		</p>
		<h1 style="MARGIN: 0in 0in 0pt">
				<u>
						<font face="Times New Roman" size="3">Troubleshooting</font>
				</u>
		</h1>
		<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">
				<font face="Times New Roman" size="3">Here is one of errors I got while I was creating the macro:</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">
				<font face="Times New Roman" size="3">Unmanaged debugging not supported</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">
				<font face="Times New Roman">
						<font size="3"> <o:p></o:p></font>
				</font>
		</p>
		<h1 style="MARGIN: 0in 0in 0pt">
				<u>
						<font face="Times New Roman">
								<font size="3">Cause and Solution<span style="TEXT-DECORATION: none; text-underline: none"><o:p></o:p></span></font>
						</font>
				</u>
		</h1>
		<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">
				<font face="Times New Roman" size="3">This happens when debugger tries to attach to the Windows service process.</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">
				<font face="Times New Roman" size="3">For some reason it thinks it is an unmanaged process and hence it cannot attach itself.</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">
				<font face="Times New Roman">
						<font size="3">
								<span style="mso-spacerun: yes"> The solution for this is </span>
						</font>
				</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">
				<font face="Times New Roman" size="3">Install VC++.NET<span style="mso-spacerun: yes">  which includes unmanaged debugger.</span></font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">
				<font face="Times New Roman">
						<font size="3"> <o:p></o:p></font>
				</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">
				<font face="Times New Roman" size="3">For details on how to do that refer to:</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">
				<a href="http://support.microsoft.com/default.aspx?scid=kb;en-us;813134">
						<u>
								<font face="Times New Roman" color="#800080" size="3">http://support.microsoft.com/default.aspx?scid=kb;en-us;813134</font>
						</u>
				</a>
		</p>
		<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">
				<font face="Times New Roman">
						<font size="3"> <o:p></o:p></font>
				</font>
		</p>
<img src ="http://www.cnitblog.com/MartinYao/aggbug/21510.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/MartinYao/" target="_blank">玄铁剑</a> 2007-01-04 21:47 <a href="http://www.cnitblog.com/MartinYao/articles/21510.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>在 Visual C# .NET 中跟踪和调试</title><link>http://www.cnitblog.com/MartinYao/articles/21509.html</link><dc:creator>玄铁剑</dc:creator><author>玄铁剑</author><pubDate>Thu, 04 Jan 2007 13:30:00 GMT</pubDate><guid>http://www.cnitblog.com/MartinYao/articles/21509.html</guid><wfw:comment>http://www.cnitblog.com/MartinYao/comments/21509.html</wfw:comment><comments>http://www.cnitblog.com/MartinYao/articles/21509.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/MartinYao/comments/commentRss/21509.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/MartinYao/services/trackbacks/21509.html</trackback:ping><description><![CDATA[
		<div class="toc" id="tocDiv" style="DISPLAY: block">
				<h5>本页</h5>
				<div depth="1">
						<a onclick="return tocScrollTo(this);" href="http://support.microsoft.com/kb/815788/zh-cn#">
								<img alt="概要" src="http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif" />
								<span class="tocTxt">概要</span>
						</a>
						<div class="tocLine">
								<div depth="2">
										<a onclick="return tocScrollTo(this);" href="http://support.microsoft.com/kb/815788/zh-cn#">
												<img alt="要求" src="http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif" />
												<span class="tocTxt">要求</span>
										</a>
										<div class="tocLine">
										</div>
								</div>
								<div depth="2">
										<a onclick="return tocScrollTo(this);" href="http://support.microsoft.com/kb/815788/zh-cn#">
												<img alt="方法说明" src="http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif" />
												<span class="tocTxt">方法说明</span>
										</a>
										<div class="tocLine">
										</div>
								</div>
								<div depth="2">
										<a onclick="return tocScrollTo(this);" href="http://support.microsoft.com/kb/815788/zh-cn#">
												<img alt="使用 Debug 类创建一个示例" src="http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif" />
												<span class="tocTxt">使用 Debug 类创建一个示例</span>
										</a>
										<div class="tocLine">
										</div>
								</div>
								<div depth="2">
										<a onclick="return tocScrollTo(this);" href="http://support.microsoft.com/kb/815788/zh-cn#">
												<img alt="使用 Trace 类 " src="http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif" />
												<span class="tocTxt">使用 Trace 类 </span>
										</a>
										<div class="tocLine">
										</div>
								</div>
								<div depth="2">
										<a onclick="return tocScrollTo(this);" href="http://support.microsoft.com/kb/815788/zh-cn#">
												<img alt="确认它可以使用" src="http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif" />
												<span class="tocTxt">确认它可以使用</span>
										</a>
										<div class="tocLine">
										</div>
								</div>
								<div depth="2">
										<a onclick="return tocScrollTo(this);" href="http://support.microsoft.com/kb/815788/zh-cn#">
												<img alt="完整代码列表" src="http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif" />
												<span class="tocTxt">完整代码列表</span>
										</a>
										<div class="tocLine">
										</div>
								</div>
								<div depth="2">
										<a onclick="return tocScrollTo(this);" href="http://support.microsoft.com/kb/815788/zh-cn#">
												<img alt="疑难解答" src="http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif" />
												<span class="tocTxt">疑难解答</span>
										</a>
										<div class="tocLine">
										</div>
								</div>
						</div>
				</div>
				<div depth="1">
						<a onclick="return tocScrollTo(this);" href="http://support.microsoft.com/kb/815788/zh-cn#">
								<img alt="参考" src="http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif" />
								<span class="tocTxt">参考</span>
						</a>
						<div class="tocLine">
						</div>
				</div>
		</div>
		<script type="text/javascript"><![CDATA[
      var sectionFilter = "type != 'notice' && type != 'securedata' && type != 'querywords'";
      var tocArrow = "/library/images/support/kbgraphics/public/en-us/downarrow.gif";
      var depthLimit = 10;
      var depth3Limit = 10;
      var depth4Limit = 5;
      var depth5Limit = 3;
      var tocEntryMinimum = 1;
    ]]&gt;</script>
		<script src="/common/script/gsfx/kbtoc.js??4" type="text/javascript">
		</script>
		<noscript>
		</noscript>
		<div class="section">
				<h2 class="subTitle" id="tocHeadRef">概要</h2>
				<script type="text/javascript"><![CDATA[oadTOCNode(1, 'summary');]]&gt;</script>
				<div class="sbody">本文介绍如何使用 <b>Debug</b> 和 <b>Trace</b> 类。Microsoft .NET Framework 中提供了这两个类。在应用程序开发期间或部署到产品后，可以使用这两个类提供有关应用程序性能的信息。这两个类只是 .NET Framework 中所提供配置功能的一部分。 <br /><br /><div class="topOfPage"><table><tbody><tr><td class="image"><a href="http://support.microsoft.com/kb/815788/zh-cn#top"><img title="回到顶端" alt="回到顶端" src="http://support.microsoft.com/library/images/support/en-us/uparrow.gif" /></a></td><td class="text"><a href="http://support.microsoft.com/kb/815788/zh-cn#top">回到顶端</a></td></tr></tbody></table></div><h3 id="tocHeadRef"><span><a id="3"></a></span>要求</h3><script type="text/javascript"><![CDATA[oadTOCNode(2, 'summary');]]&gt;</script>
下面的列表概括了推荐的硬件、软件、网络结构以及所需的 Service Pack：<br /><table class="list ul"><tbody><tr><td class="bullet">•</td><td class="text">Microsoft Windows 2000 或 Microsoft Windows XP</td></tr><tr><td class="bullet">•</td><td class="text">Microsoft Visual C# .NET</td></tr></tbody></table>本文还假定您熟悉程序调试。<br /><br /><span><a id="4"></a></span><div class="topOfPage"><table><tbody><tr><td class="image"><a href="http://support.microsoft.com/kb/815788/zh-cn#top"><img title="回到顶端" alt="回到顶端" src="http://support.microsoft.com/library/images/support/en-us/uparrow.gif" /></a></td><td class="text"><a href="http://support.microsoft.com/kb/815788/zh-cn#top">回到顶端</a></td></tr></tbody></table></div><h3 id="tocHeadRef">方法说明</h3><script type="text/javascript"><![CDATA[oadTOCNode(2, 'summary');]]&gt;</script><br />在<span><a href="http://support.microsoft.com/kb/815788/zh-cn#5">使用 Debug 类创建一个示例</a></span>一节中介绍的步骤演示了如何创建使用 <b>Debug</b> 类以提供有关程序执行信息的控制台应用程序。 <br /><br />当程序运行时，您可以使用 <b>Debug</b> 类的方法来生成消息，以帮助您监视程序执行顺序、检测故障或提供性能度量信息。默认情况下，<b>Debug</b> 类产生的消息显示在 Visual Studio 集成开发环境 (IDE) 的“输出”窗口中。<br /><br />该代码示例使用 <b>WriteLine</b> 方法生成后面带有行结束符的消息。当您使用此方法生成消息时，每条消息在“输出”窗口中均显示为单独的一行。<br /><br />如果使用 <b>Debug</b> 类的 <b>Assert</b> 方法，那么只有在指定条件计算为 false 时，“输出”窗口才显示消息。该消息还在一个模式对话框中向用户显示。该对话框包括消息、项目名和 <b>Debug.Assert</b> 语句编号。该对话框还包括下列三个命令按钮：<br /><table class="list ul"><tbody><tr><td class="bullet">•</td><td class="text"><strong class="uiterm">终止：</strong>应用程序停止运行。 </td></tr><tr><td class="bullet">•</td><td class="text"><strong class="uiterm">重试：</strong>应用程序进入调试模式。 </td></tr><tr><td class="bullet">•</td><td class="text"><strong class="uiterm">忽略：</strong>应用程序继续。</td></tr></tbody></table>用户必须先单击这些按钮中的一个，然后应用程序才可以继续。<br /><br />您还可以指示从 <b>Debug</b> 类向“输出”窗口以外的目标进行输出。<b>Debug</b> 类有一个名为 <b>Listeners</b> 的集合，该集合包括一些 <b>Listener</b> 对象。<br /><br />每个 <b>Listener</b> 对象都监视 <b>Debug</b> 输出并使输出指向指定的目标。 <br /><br /><b>Listener</b> 集合中的每个 <b>Listener</b> 都接收 <b>Debug</b> 类生成的任何输出。请使用 <b>TextWriterTraceListener</b> 类定义 <b>Listener</b> 对象。可以通过 <b>TextWriterTraceListener</b> 类的构造函数为该类指定目标。<br /><br />一些可能的输出目标包括： 
<table class="list ul"><tbody><tr><td class="bullet">•</td><td class="text">使用 <b>System.Console.Out</b> 属性指定“控制台”窗口作为输出目标。</td></tr><tr><td class="bullet">•</td><td class="text">使用 <b>System.IO.File.CreateText("FileName.txt")</b> 语句指定文本文件 (.txt) 作为输出目标。</td></tr></tbody></table>创建 <b>TextWriterTraceListener</b> 对象后，必须将该对象添加到 <b>Debug.Listeners</b> 集合才可接收调试输出。<br /><br /><span><a id="5"></a></span><div class="topOfPage"><table><tbody><tr><td class="image"><a href="http://support.microsoft.com/kb/815788/zh-cn#top"><img title="回到顶端" alt="回到顶端" src="http://support.microsoft.com/library/images/support/en-us/uparrow.gif" /></a></td><td class="text"><a href="http://support.microsoft.com/kb/815788/zh-cn#top">回到顶端</a></td></tr></tbody></table></div><h3 id="tocHeadRef">使用 Debug 类创建一个示例</h3><script type="text/javascript"><![CDATA[oadTOCNode(2, 'summary');]]&gt;</script><table class="list ol"><tbody><tr><td class="number">1.</td><td class="text">启动 Visual Studio .NET。</td></tr><tr><td class="number">2.</td><td class="text">新建一个名为 <span class="userInput">conInfo</span> 的新 Visual C# .NET 控制台应用程序项目。将创建 Class1。</td></tr><tr><td class="number">3.</td><td class="text">在 Class1 的顶部添加以下名称空间。<code><pre class="code">using System.Diagnostics;</pre></code></td></tr><tr><td class="number">4.</td><td class="text">要初始化变量以使其包含产品的相关信息，请将下面的声明语句添加到 <b>Main</b> 方法：<code><pre class="code">string sProdName = "Widget";
int iUnitQty = 100;
double dUnitCost = 1.03;</pre></code></td></tr><tr><td class="number">5.</td><td class="text">将类生成的消息指定为 <b>WriteLine</b> 方法的第一个输入参数。按 CTRL+ALT+O 组合键以确保“输出”窗口可见。<code><pre class="code">Debug.WriteLine("Debug Information-Product Starting ");</pre></code></td></tr><tr><td class="number">6.</td><td class="text">为了清晰易读，请使用 <b>Indent</b> 方法在“输出”窗口中缩进后面的消息：<code><pre class="code">Debug.Indent();</pre></code></td></tr><tr><td class="number">7.</td><td class="text">要显示所选变量的内容，请使用 <b>WriteLine</b> 方法，如下所示：<code><pre class="code">Debug.WriteLine("The product name is " + sProdName);
Debug.WriteLine("The available units on hand are" + iUnitQty.ToString());
Debug.WriteLine("The per unit cost is " + dUnitCost.ToString());</pre></code></td></tr><tr><td class="number">8.</td><td class="text">您还可以使用 <b>WriteLine</b> 方法显示现有对象的名称空间和类名称。例如，下面的代码在“输出”窗口中显示 <b>System.Xml.XmlDocument</b> 命名空间：<code><pre class="code">System.Xml.XmlDocument oxml = new System.Xml.XmlDocument();
Debug.WriteLine(oxml);</pre></code></td></tr><tr><td class="number">9.</td><td class="text">要整理输出，可以包括一个类别作为 <b>WriteLine</b> 方法的第二个可选的输入参数。如果您指定一个类别，则“输出”窗口消息的格式为“类别:消息”。例如，以下代码的第一行在“输出”窗口中显示“Field:The product name is Widget”： <code><pre class="code">Debug.WriteLine("The product name is " + sProdName,"Field");
Debug.WriteLine("The units on hand are" + iUnitQty,"Field");
Debug.WriteLine("The per unit cost is" + dUnitCost.ToString(),"Field");
Debug.WriteLine("Total Cost is  " + (iUnitQty * dUnitCost),"Calc");</pre></code></td></tr><tr><td class="number">10.</td><td class="text">仅在使用 <b>Debug</b> 类的 <b>WriteLineIf</b> 方法将指定条件计算为 true 时，“输出”窗口才可以显示消息。将要计算的条件是 <b>WriteLineIf</b> 方法的第一个输入参数。<b>WriteLineIf</b> 的第二个参数是仅在第一个参数的条件计算为真时才显示的消息。<code><pre class="code">Debug.WriteLineIf(iUnitQty &gt; 50, "This message WILL appear");
Debug.WriteLineIf(iUnitQty &lt; 50, "This message will NOT appear");
</pre></code></td></tr><tr><td class="number">11.</td><td class="text">使用 <b>Debug</b> 类的 <b>Assert</b> 方法，使“输出”窗口仅在指定条件计算为 false 时才显示消息：<code><pre class="code">Debug.Assert(dUnitCost &gt; 1, "Message will NOT appear");
Debug.Assert(dUnitCost &lt; 1, "Message will appear since dUnitcost &lt; 1 is false");
</pre></code></td></tr><tr><td class="number">12.</td><td class="text">为“控制台”窗口 (tr1) 和名为 Output.txt (tr2) 的文本文件创建 <b>TextWriterTraceListener</b> 对象，然后将每个对象添加到 <b>Debug Listeners</b> 集合中：<code><pre class="code">TextWriterTraceListener tr1 = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(tr1);
        
TextWriterTraceListener tr2 = new TextWriterTraceListener(System.IO.File.CreateText("Output.txt"));
Debug.Listeners.Add(tr2);</pre></code></td></tr><tr><td class="number">13.</td><td class="text">为了清晰易读，请使用 <b>Unindent</b> 方法去除 <b>Debug</b> 类为后续消息生成的缩进。当您将 <b>Indent</b> 和 <b>Unindent</b> 两种方法一起使用时，读取器可以将输出分成组。<code><pre class="code">Debug.Unindent();
Debug.WriteLine("Debug Information-Product Ending");</pre></code></td></tr><tr><td class="number">14.</td><td class="text">为了确保每个 <b>Listener</b> 对象收到它的所有输出，请为 <b>Debug</b> 类缓冲区调用 <b>Flush</b> 方法：<code><pre class="code">Debug.Flush();</pre></code></td></tr></tbody></table><span><a id="6"></a></span><div class="topOfPage"><table><tbody><tr><td class="image"><a href="http://support.microsoft.com/kb/815788/zh-cn#top"><img title="回到顶端" alt="回到顶端" src="http://support.microsoft.com/library/images/support/en-us/uparrow.gif" /></a></td><td class="text"><a href="http://support.microsoft.com/kb/815788/zh-cn#top">回到顶端</a></td></tr></tbody></table></div><h3 id="tocHeadRef">使用 Trace 类 </h3><script type="text/javascript"><![CDATA[oadTOCNode(2, 'summary');]]&gt;</script>
您还可以使用 <b>Trace</b> 类生成监视应用程序执行的消息。<b>Trace</b> 和 <b>Debug</b> 类共享大多数相同的方法来生成输出，这些方法包括： 
<table class="list ul"><tbody><tr><td class="bullet">•</td><td class="text"><strong class="uiterm">WriteLine</strong></td></tr><tr><td class="bullet">•</td><td class="text"><strong class="uiterm">WriteLineIf</strong></td></tr><tr><td class="bullet">•</td><td class="text"><strong class="uiterm">Indent</strong></td></tr><tr><td class="bullet">•</td><td class="text"><strong class="uiterm">Unindent</strong></td></tr><tr><td class="bullet">•</td><td class="text"><strong class="uiterm">Assert</strong></td></tr><tr><td class="bullet">•</td><td class="text"><strong class="uiterm">Flush</strong></td></tr></tbody></table>您可以在同一应用程序中分别或同时使用 <b>Trace</b> 和 <b>Debug</b> 类。在一个“调试解决方案配置”项目中，<b>Trace</b> 和 <b>Debug</b> 两种输出均为活动状态。该项目从这两个类为 <b>Listener</b> 对象生成输出。但是，“发布解决方案配置”项目仅从 <b>Trace</b> 类生成输出。该“发布解决方案配置”项目忽略任何 <b>Debug</b> 类方法调用。<code><pre class="code">Trace.WriteLine("Trace Information-Product Starting ");
Trace.Indent();

Trace.WriteLine("The product name is "+sProdName);
Trace.WriteLine("The product name is"+sProdName,"Field" );
Trace.WriteLineIf(iUnitQty &gt; 50, "This message WILL appear");
Trace.Assert(dUnitCost &gt; 1, "Message will NOT appear");
        
Trace.Unindent();
Trace.WriteLine("Trace Information-Product Ending");

Trace.Flush();

Console.ReadLine();
</pre></code><span><a id="7"></a></span><div class="topOfPage"><table><tbody><tr><td class="image"><a href="http://support.microsoft.com/kb/815788/zh-cn#top"><img title="回到顶端" alt="回到顶端" src="http://support.microsoft.com/library/images/support/en-us/uparrow.gif" /></a></td><td class="text"><a href="http://support.microsoft.com/kb/815788/zh-cn#top">回到顶端</a></td></tr></tbody></table></div><h3 id="tocHeadRef">确认它可以使用</h3><script type="text/javascript"><![CDATA[oadTOCNode(2, 'summary');]]&gt;</script><table class="list ol"><tbody><tr><td class="number">1.</td><td class="text">确保 <b>Debug</b> 是当前的解决方案配置。</td></tr><tr><td class="number">2.</td><td class="text">如果“解决方案资源管理器”窗口不可见，请按 CTRL+ALT+L 组合键以显示此窗口。</td></tr><tr><td class="number">3.</td><td class="text">右键单击“conInfo”，然后单击“属性”。</td></tr><tr><td class="number">4.</td><td class="text">在 conInfo 属性页左窗格中，在“配置”文件夹下，请确保箭头指向“调试”。</td></tr><tr><td class="number">5.</td><td class="text">在“配置”文件夹上面的“配置”下拉列表框中，单击“活动（调试）”或“调试”，然后单击“确定”。</td></tr><tr><td class="number">6.</td><td class="text">按 CTRL+ALT+O 以显示“输出”窗口。</td></tr><tr><td class="number">7.</td><td class="text">按 F5 键以运行该代码。在出现“断言失败”对话框时，单击“忽略”。</td></tr><tr><td class="number">8.</td><td class="text">在“控制台”窗口中，按 ENTER 键。此时程序即已完成，“输出”窗口应显示以下输出：<code><pre class="code">    Debug Information-Product Starting 
    The product name is Widget
    The available units on hand are100
    The per unit cost is 1.03
    System.Xml.XmlDocument
    Field: The product name is Widget
    Field: The units on hand are100
    Field: The per unit cost is1.03
    Calc: Total Cost is  103
    This message WILL appear
    ---- DEBUG ASSERTION FAILED ----
---- Assert Short Message ----
Message will appear since dUnitcost  &lt; 1 is false
---- Assert Long Message ----


    at Class1.Main(String[] args)  &lt;%Path%&gt;\class1.cs(34)

    The product name is Widget
    The available units on hand are100
    The per unit cost is 1.03
Debug Information-Product Ending
Trace Information-Product Starting 
    The product name is Widget
    Field: The product name isWidget
    This message WILL appear
Trace Information-Product Ending
                    
</pre></code></td></tr><tr><td class="number">9.</td><td class="text">“控制台”窗口和 Output.txt 文件应显示以下输出：<code><pre class="code">The product name is Widget
    The available units on hand are 100
    The per unit cost is 1.03
Debug Information-Product Ending
Trace Information-Product Starting 
    The product name is Widget
    Field: The product name is Widget
    This message WILL appear
Trace Information-Product Ending			
</pre></code></td></tr></tbody></table><b>注意</b>：Output.txt 文件与 conInfo 可执行文件 (conInfo.exe) 位于同一目录中。通常情况下，该目录是存储项目源的 \bin 文件夹，默认情况下为 C:\Documents and Settings\User login\My Documents\Visual Studio Projects\conInfo\bin。<br /><br /><span><a id="8"></a></span><div class="topOfPage"><table><tbody><tr><td class="image"><a href="http://support.microsoft.com/kb/815788/zh-cn#top"><img title="回到顶端" alt="回到顶端" src="http://support.microsoft.com/library/images/support/en-us/uparrow.gif" /></a></td><td class="text"><a href="http://support.microsoft.com/kb/815788/zh-cn#top">回到顶端</a></td></tr></tbody></table></div><h3 id="tocHeadRef">完整代码列表</h3><script type="text/javascript"><![CDATA[oadTOCNode(2, 'summary');]]&gt;</script><code><pre class="code">   using System;
   using System.Diagnostics;

   class Class1
   {
      [STAThread]
      static void Main(string[] args)
      {
         string sProdName = "Widget";
         int iUnitQty = 100;
         double  dUnitCost = 1.03;
         Debug.WriteLine("Debug Information-Product Starting ");
         Debug.Indent();
         Debug.WriteLine("The product name is "+sProdName);
         Debug.WriteLine("The available units on hand are"+iUnitQty.ToString());
         Debug.WriteLine("The per unit cost is "+ dUnitCost.ToString());

         System.Xml.XmlDocument oxml = new System.Xml.XmlDocument();
         Debug.WriteLine(oxml);

         Debug.WriteLine("The product name is "+sProdName,"Field");
         Debug.WriteLine("The units on hand are"+iUnitQty,"Field");
         Debug.WriteLine("The per unit cost is"+dUnitCost.ToString(),"Field");
         Debug.WriteLine("Total Cost is  "+(iUnitQty * dUnitCost),"Calc");

         Debug.WriteLineIf(iUnitQty &gt; 50, "This message WILL appear");
         Debug.WriteLineIf(iUnitQty &lt; 50, "This message will NOT appear");

         Debug.Assert(dUnitCost &gt; 1, "Message will NOT appear");
         Debug.Assert(dUnitCost &lt; 1, "Message will appear since dUnitcost  &lt; 1 is false");

         TextWriterTraceListener tr1 = new TextWriterTraceListener(System.Console.Out);
         Debug.Listeners.Add(tr1);
        
         TextWriterTraceListener tr2 = new TextWriterTraceListener(System.IO.File.CreateText("Output.txt"));
         Debug.Listeners.Add(tr2);
         
 
         Debug.WriteLine("The product name is "+sProdName);
         Debug.WriteLine("The available units on hand are"+iUnitQty);
         Debug.WriteLine("The per unit cost is "+dUnitCost);
         Debug.Unindent();
         Debug.WriteLine("Debug Information-Product Ending");
         Debug.Flush();
         
         Trace.WriteLine("Trace Information-Product Starting ");
         Trace.Indent();

         Trace.WriteLine("The product name is "+sProdName);
         Trace.WriteLine("The product name is"+sProdName,"Field" );
         Trace.WriteLineIf(iUnitQty &gt; 50, "This message WILL appear");
         Trace.Assert(dUnitCost &gt; 1, "Message will NOT appear");
        

         Trace.Unindent();
         Trace.WriteLine("Trace Information-Product Ending");

         Trace.Flush();

         Console.ReadLine();
      }			
   }</pre></code><font face="宋体"><b>注意</b>：要使此代码示例发挥作用，必须通过将 <b>using System.Diagnostics;</b> 粘贴为第一行代码来添加 <b>System.Diagonstics</b> 名称空间。<br /><br /><span><a id="9"></a></span></font><div class="topOfPage"><table><tbody><tr><td class="image"><a href="http://support.microsoft.com/kb/815788/zh-cn#top"><font face="宋体"><img title="回到顶端" alt="回到顶端" src="http://support.microsoft.com/library/images/support/en-us/uparrow.gif" /></font></a></td><td class="text"><a href="http://support.microsoft.com/kb/815788/zh-cn#top">回到顶端</a></td></tr></tbody></table></div><h3 id="tocHeadRef">疑难解答</h3><script type="text/javascript"><![CDATA[oadTOCNode(2, 'summary');]]&gt;</script><table class="list ul"><tbody><tr><td class="bullet">•</td><td class="text">如果解决方案配置类型是 <b>Release</b>，则会忽略 <b>Debug</b> 类输出。</td></tr><tr><td class="bullet">•</td><td class="text">在为特定目标创建 <b>TextWriterTraceListener</b> 类后，<b>TextWriterTraceListener</b> 会从 <b>Trace</b> 类和 <b>Debug</b> 类接收输出。无论您是否使用<b>Trace</b> 或 <b>Debug</b> 类的 <b>Add</b> 方法将 <b>TextWriterTraceListener</b> 添加到 <b>Listeners</b> 类，都会出现这种情况。</td></tr><tr><td class="bullet">•</td><td class="text">如果将同一目标的 <b>Listeners</b> 对象添加到 <b>Trace</b> 类和 <b>Debug</b> 类中，则无论是 <b>Debug</b> 类还是 <b>Trace</b> 类生成输出，各行输出都会重复出现。 <code><pre class="code">         TextWriterTraceListener myWriter = new TextWriterTraceListener(System.Console.Out);
         Debug.Listeners.Add(myWriter);
        
         TextWriterTraceListener myCreator = new TextWriterTraceListener(System.Console.Out);
         Trace.Listeners.Add(myCreator);
         
</pre></code></td></tr></tbody></table><span><a id="10"></a></span><div class="topOfPage"><table><tbody><tr><td class="image"><a href="http://support.microsoft.com/kb/815788/zh-cn#top"><img title="回到顶端" alt="回到顶端" src="http://support.microsoft.com/library/images/support/en-us/uparrow.gif" /></a></td><td class="text"><a href="http://support.microsoft.com/kb/815788/zh-cn#top">回到顶端</a></td></tr></tbody></table></div></div>
				<h2 class="subTitle" id="tocHeadRef">参考</h2>
				<script type="text/javascript"><![CDATA[oadTOCNode(1, 'references');]]&gt;</script>
				<div class="sbody">有关详细信息，请参阅 .NET Framework 类库文档中的下列主题： 
<div class="indent">Trace 类<br /><span class="ll"><a href="http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemDiagnosticsTraceClassTopic.asp">http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDiagnosticsTraceClassTopic.asp </a></span><span class="pLink">(http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemDiagnosticsTraceClassTopic.asp)</span><br /><br />Debug 类<br /><span class="ll"><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDiagnosticsDebugClassTopic.asp">http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDiagnosticsDebugClassTopic.asp </a></span><span class="pLink">(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDiagnosticsDebugClassTopic.asp)</span></div><br /><br />有关跟踪的详细信息，请参阅 Microsoft GotDotNet 快速入门教程中的以下主题： 
<div class="indent">如何使用跟踪？<br /><a href="http://samples.gotdotnet.com/quickstart/howto/doc/Trace.aspx" target="_blank">http://samples.gotdotnet.com/quickstart/howto/doc/Trace.aspx </a><span class="pLink">(http://samples.gotdotnet.com/quickstart/howto/doc/Trace.aspx)</span></div><div class="topOfPage"><table><tbody><tr><td class="image"><a href="http://support.microsoft.com/kb/815788/zh-cn#top"><img title="回到顶端" alt="回到顶端" src="http://support.microsoft.com/library/images/support/en-us/uparrow.gif" /></a></td><td class="text"><a href="http://support.microsoft.com/kb/815788/zh-cn#top">回到顶端</a></td></tr></tbody></table></div></div>
		</div>
<img src ="http://www.cnitblog.com/MartinYao/aggbug/21509.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/MartinYao/" target="_blank">玄铁剑</a> 2007-01-04 21:30 <a href="http://www.cnitblog.com/MartinYao/articles/21509.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>