65.9K
CodeProject 正在变化。 阅读更多。
Home

将未处理的异常记录为自动化构建过程的一部分

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2008年2月1日

CPOL

4分钟阅读

viewsIcon

23026

本文介绍了如何通过将 Red Gate Software 的新分析工具 Exception Hunter 与 CruiseControl.NET 集成,自动生成关于方法抛出的未处理异常的报告。

这是我们对 The Code Project 赞助商的展示性评测。这些评测旨在为您提供我们认为对开发人员有用且有价值的产品和服务信息。

Cruise Control .NET 等持续集成 (CI) 服务器通过检查代码更改并在需要时启动软件的自动化构建,为您省去了手动构建软件的麻烦,使您的工作更加轻松。这使得开发团队可以更自由地贡献代码更改。CI 服务器还可以在构建完成后执行一项或多项自动化任务,例如使用 FxCop 分析新构建的程序集是否符合最佳实践,或者将文件发布到 FTP 服务器。

Exception Hunter™ 查找托管程序集中的未处理异常

Exception Hunter 是 Red Gate Software 出品的一款独一无二的工具,旨在分析 .NET 程序集,找出其中可能发生的未处理异常。这为开发人员提供了机会,让他们能够就异常处理实践和编写更具弹性的代码做出明智的决定。Exception Hunter 除了图形用户界面版本外,还包括一个命令行可执行文件,从而可以自动化报告包含未处理异常的方法。利用 CI 服务器的功能,可以将这些报告集成到您的产品文档中,甚至集成到 CI 服务器的日志中。

在构建过程中自动创建异常报告

server/ccnet.config 文件中,通过 CC .NET 的 exec 任务来将 Exception Hunter 方法或异常报告添加到自动化构建过程中。exec 任务放置在 <project> 节点内,但位于源代码控制和构建任务之后。使用 Exception Hunter 的 /mr (Method Report) 参数,可以创建一个 HTML 报告,详细说明所有未处理的异常。由于我构建的项目创建了两个可执行文件,我通过为 Hunt.exe 指定两个 /assembly 参数,来创建一个涵盖这两个程序集的单一方法报告。

<exec>
    <executable>Hunt.exe</executable>
    <baseDirectory>c:\program files\red gate\exception hunter 1</baseDirectory>
    <buildArgs>/q 
        /xml:c:\Builds\ChatApplication\ExceptionReport.XML 
        /er:c:\Builds\ChatApplication\ExceptionReport.htm 
        /allpublicmethods 
        /ignoreCodeBranches:on 
        /force 
        /Assembly:c:\Builds\ChatApplication\ChatClient\Bin\Debug\ChatClient.exe 
        /Assembly:c:\Builds\ChatApplication\ChatServer\Bin\Debug\ChatServer.exe
    </buildArgs>
    <buildTimeoutSeconds>2800</buildTimeoutSeconds>
</exec>

此异常报告将创建在 build 文件夹中,可用作关于刚构建的所有程序集中发生的未处理异常的类型和数量的内部文档。

将异常报告集成到自动化构建日志中

更有趣的是,Exception Hunter 方法和异常报告可以直接输出到 CC .NET 的构建日志中,从而允许构建管理器在 CC .NET 报告中查看异常。这些报告将包含在最新的构建日志中,以及它们自己的独立报告类别中。

image002.gif

CC .NET 仪表板中的异常和方法报告

这个巧妙的技巧是通过从 Exception Hunter 可执行文件 (Hunt.exe) 中提取 XSL 模板,将其添加到 CC .NET 的 Web 仪表板中,并使用一个名为 "xmllogger" 的 CC .NET 发布器将 Exception Hunter 的 XML 输出合并到构建日志来实现的。

image003.gif

使用 .NET Reflector 从 Hunt.exe 提取 XSL 模板

第一步是从 Hunt.exe 中提取 XSL 模板。这可以使用诸如 Reflector for .NET 这样的工具来完成,该工具可以读取程序集中的资源。安装 Reflector 并打开 %programfiles%\red gate\exception hunter 1\hunt.exe 后,单击 Resources 文件夹并找到所有以 *.xslt 结尾的资源。以 Exceptions.xslt 结尾的资源是用于创建按异常类型组织的报告的模板。Summary.xslt 是用于创建按方法组织的报告的模板。

右键单击 Exceptions.xslt 并选择 view resource。然后,在 "disassembler" 窗口中高亮显示内容并复制到剪贴板。在 CC .NET 的 webdashboard/xsl 文件夹中创建一个名为 ExceptionHunt-Methods.xsl 的新文件。将剪贴板中的内容粘贴进去并保存文件。最后,在 webdashboard/xsl 文件夹中创建一个名为 ExceptionHunt-Exceptions.xsl 的新文件,并对 Summary.xslt 资源执行相同的操作。确保这两个文件都已保存。

接下来,通过编辑 webdashboard/dashboard.config 在 CC .NET 的报告列表中启用这些报告,将 Exception Hunter XSLT 文件添加到 <xslFileNames> 节点。为每个报告配置一个 <xslReportBuildPlugin>

</buildPlugins>
    <buildReportBuildPlugin>
        <xslFileNames>
            <xslFile>xsl\ExceptionHunt-Methods.xsl</xslFile>
            <xslFile>xsl\ExceptionHunt-Exceptions.xsl</xslFile>
        </xslFileNames>
    </buildReportBuildPlugin>
    <xslReportBuildPlugin description="Methods with Unhandled Exceptions" 
        actionName="UnhandledMethods" xslFileName="xsl\ExceptionHunt-Methods.xsl"/>
    <xslReportBuildPlugin description="Unhandled Exceptions By Type" 
        actionName="UnhandledExceptions" xslFileName="xsl\ExceptionHunt-Exceptions.xsl"/>
</buildPlugins>

这将为 CC .NET 仪表板添加两个新报告,分别称为“带有未处理异常的方法”和“按类型的未处理异常”。

为了将 Exception Hunter 的输出集成到这些报告中,我们需要在 server/ccnet.config 文件中的构建任务之后添加一个 exec 任务,以指示 Exception Hunter 生成 XML 输出,然后配置一个 XML 日志记录器将此输出发送到 CC .NET 日志流。要创建 exec 任务,请编辑 server/ccnet.config 并添加一个运行 Hunt.exe 并带 /xml 参数的 exec 任务。

<exec>
    <executable>Hunt.exe</executable>
    <baseDirectory>c:\program files\red gate\exception hunter 1</baseDirectory>
    <buildArgs>/q 
        /xml:c:\Builds\ChatApplication\ExceptionReport.XML 
        /allpublicmethods 
        /ignoreCodeBranches:on 
        /force 
        /Assembly:c:\Builds\ChatApplication\ChatClient\Bin\Debug\ChatClient.exe 
        /Assembly:c:\Builds\ChatApplication\ChatServer\Bin\Debug\ChatServer.exe
    </buildArgs>
    <buildTimeoutSeconds>2800</buildTimeoutSeconds>
</exec>

最后一步是在 server/ccnet.config 中添加一个 merge publisher,它会将 Exception Hunter 的输出与其余的构建日志合并。

<publishers>
    <merge>
        <files>
            <file>c:\Builds\ChatApplication\ExceptionReport.XML</file>
        </files>
    </merge>
    <xmllogger />
</publishers>

请注意,可以使用同一个命令行生成 HTML 异常和方法报告,以及一个 XML 文件。Exception Hunter 不需要运行三次即可实现此目的。

<cruisecontrol>
<project name="Demo Exception Hunter">
<sourcecontrol type="vss" autoGetSource="true" applyLabel="true">
    <executable>C:\Program Files\Microsoft Visual SourceSafe\SS.EXE</executable>
    <project>$/PROJECTS/ChatApplication.root/ChatApplication</project>
    <username>Automated.Build</username>
    <password>P@ssw0rd</password>
    <ssdir>C:\VSS</ssdir>
    <workingDirectory>C:\builds\ChatApplication</workingDirectory>
    <culture>en-GB</culture>
    <cleanCopy>false</cleanCopy>
</sourcecontrol>
<tasks>
    <devenv>
        <solutionfile>c:\builds\ChatApplication\ChatApplication.sln</solutionfile>
        <configuration>debug</configuration>
        <buildtype>Build</buildtype>
        <executable>
            c:\program files\microsoft visual studio 8\Common7\ide\devenv.com
        </executable>
    </devenv>
    <exec>
        <executable>Hunt.exe</executable>
        <baseDirectory>c:\program files\red gate\exception hunter 1</baseDirectory>
        <buildArgs>/q 
            /xml:c:\Builds\ChatApplication\ExceptionReport.XML 
            /allpublicmethods 
            /ignoreCodeBranches:on 
            /force 
            /Assembly:c:\Builds\ChatApplication\ChatClient\Bin\Debug\ChatClient.exe 
            /Assembly:c:\Builds\ChatApplication\ChatServer\Bin\Debug\ChatServer.exe
        </buildArgs>
        <buildTimeoutSeconds>2800</buildTimeoutSeconds>
    </exec>
</tasks>
<publishers>
    <merge>
        <files>
            <file>c:\Builds\ChatApplication\ExceptionReport.XML</file>
        </files>
    </merge>
    <xmllogger />
</publishers>
</project>
</cruisecontrol>

检查异常日志可识别代码中的潜在问题

查看此项目在 Cruise Control .NET 中的构建日志,现在将显示刚构建的程序集的 Exception Hunter 方法和异常信息。利用这些报告,决策者和程序员可以更轻松地评估其软件的质量,并决定是否需要对程序代码进行更改,以通过更彻底的异常处理来提高其弹性。

© . All rights reserved.