如何在 Visual Studio 2017 (C#) 中抑制初始构建后事件错误? [英] How to suppress initial post-build event error in Visual Studio 2017 (C#)?

查看:38
本文介绍了如何在 Visual Studio 2017 (C#) 中抑制初始构建后事件错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Visual Studio 2017 中有一个 C# 解决方案.我还有一个名为 foobar.bat 的批处理脚本,其中包含以下代码:

I have a C# solution in Visual Studio 2017. I also have a batch script called foobar.bat that contains the following code:

echo foobar : error 1: This is a test error.           

我的目标是让上面的测试错误消息在我构建特定项目时出现在 Visual Studio 的错误列表中,并在它出现时停止构建.所以我把[path to script]\foobar.bat 放在项目的post-build 事件命令行,然后build.现在我在 Visual Studio 错误列表中收到两条错误消息:

My goal is to get only the test error message above to appear in Visual Studio's Error List when I build a particular project and for the build to stop when it appears. So I put [path to script]\foobar.bat in the project's post-build event command line and then build. Now I'm getting two error messages in the Visual Studio Error List:

  1. 命令[脚本路径]\foobar.bat"以代码-1退出.
  2. 这是一个测试错误.

在这种情况下,查看仅打印出我的构建后事件内容的第一条错误消息并没有帮助.我想抑制这个初始错误,以便在错误列表中只显示我的自定义错误消息(或至少将其更改为更有用的内容).

In this case, seeing that first error message that just prints out the contents of my post-build event isn't helpful. I want to suppress this initial error so that only my custom error messages show up in the Error List (or at least change it to say something more useful).

这是我尝试过的:

  • 在批处理脚本的末尾添加 2>nul 没有任何效果.
  • 在批处理脚本的末尾添加 1>nul 可以抑制两个错误,这不是我想要的.
  • &set errorlevel=0 添加到我的批处理脚本的末尾没有任何效果.
  • 将行 exit 0 添加到批处理脚本的末尾没有任何效果.
  • 在我的 .csproj 文件末尾添加以下内容(根据 这篇文章) 抑制了第一个错误,但使构建不再失败:
  • Adding 2>nul to the end of my batch script has no effect.
  • Adding 1>nul to the end of my batch script suppresses both errors, which isn't what I want.
  • Adding &set errorlevel=0 to the end of my batch script has no effect.
  • Adding the line exit 0 to the end of my batch script has no effect.
  • Adding the following to the end of my .csproj file (per this article) suppresses the first error, but makes it so the build no longer fails:
<Target
    Name="PostBuildEvent"
    Condition="'$(PostBuildEvent)'!=''"
    DependsOnTargets="$(PostBuildEventDependsOn)">
    <Exec WorkingDirectory="$(OutDir)" Command="$(PostBuildEvent)" IgnoreExitCode="true" />
</Target>

最后一个选项几乎让我得到我想要的.但是,尽管有错误消息,错误列表也不会弹出,构建也不会失败.似乎任何会导致初始错误消息不出现的事情也会导致构建不再失败.是这样吗?或者有什么方法可以让构建失败而不显示初始错误消息?

The last option almost gets me what I want. However, in spite of there being an error message, the Error List doesn't pop up and the build does not fail. It appears as though anything that would cause the initial error message to not appear will also cause the build to no longer fail. Is that the case? Or is there some way I can get the build to fail without showing that initial error message?

推荐答案

您可以做的是使用 exec 和一个 error 任务.

What you can do is use an exec and an error task together.

您需要编辑 .csproj 文件并将这些任务添加到上面最后一个要点中的 Target PostBuildEvent 之后.

You need to edit the .csproj file and add these tasks after your the Target PostBuildEvent from your last bullet point above.

此解决方案的工作原理是获取 exec 任务的 ExitCodeOutput,并使用它们触发错误任务,然后停止构建并记录消息.
Exec 任务需要三个参数:

This solution works by getting the ExitCode and Output of your exec task and using them to trigger the error task which will then stop the build and log the message.
The Exec task needs three parameters:

  • IgnoreStandardErrorWarningFormatIgnoreExitCode 防止在这一步记录错误
  • ConsoleToMsBuild 参数是获取输出所必需的(在 VS 2017 中拼写为 ConsoleToMSBuild).
  • IgnoreStandardErrorWarningFormat and IgnoreExitCode prevent the error from being logged at this step
  • ConsoleToMsBuild parameter is required to get the output (spelled ConsoleToMSBuild in VS 2017).

所以任务看起来像这样:

So the tasks look like this:

  <Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="$(PostBuildEvent)" IgnoreStandardErrorWarningFormat="true" IgnoreExitCode="true" ConsoleToMsBuild="true">
      <Output TaskParameter="ConsoleOutput" PropertyName="OutputMessage" />
      <Output TaskParameter="ExitCode" PropertyName="ExitCode" />
    </Exec>
    <Error Text="$(OutputMessage)" Condition="$(ExitCode) == 10" />
    <!-- <Error Text="(optional text) : $(OutputMessage)" Condition="$(ExitCode) == 11" />
    <Error Text="(optional text) : $(OutputMessage)" Condition="$(ExitCode) == 12" /> -->
  </Target>

并编辑文件foobar.bat:

echo foobar : error 1: This is a test error.
exit /b 10 <-- /b parameter needed if used in a .bat file

重要的部分是exit,它将设置我们以后要使用的代码.

The important part is the exit that will set the code we want to use afterwards.

您可以让多个 Error 任务执行更多条件日志记录,或者直接使用输出.

You can have more than one Error task do to more conditional logging or just use the output as is.

这篇关于如何在 Visual Studio 2017 (C#) 中抑制初始构建后事件错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆