如何在 Visual Studio 的生成后事件中访问生成警告?

问题描述:

一般任务.我的任务是使构建(一组 C# 项目)失败,如果存在找不到这样的 DLL,因此没有复制到输出文件夹.似乎相应的警告不是编译器的警告,因此我无法为 Visual Studio 中的将警告视为错误"选项提供警告代码.我发现的解决方法是使用 MsBuild 就像它描述的那样 此处.但是我仍然找不到任何使用 Visual Studio 构建的解决方案.

General task. My task is to make a build (of a set of C# projects) failed if there exists such DLLs which were not found and thus were not copied to the output folder. It seems that a corresponding warning is not a warning of a compiler, thus I cannot provide a warning code to the "Treat warnings as errors" option in Visual Studio. The workaround I found is to use MsBuild like it is described here. However I still cannot find any solution for building with a Visual Studio.

问题.有没有办法在 Visual Studio 的构建后事件中获取构建日志,尤其是构建警告,并在存在某些确切警告时使构建失败?

Question. Is there a way to obtain a build log and especially build warnings in a post-build event in Visual Studio and make the build failed if some exact warnings exist?

我看到了什么.看了这里,但建议的宏不是一个方便的解决方案.根据 thisthis Visual Studio 不保存C#的构建日志em> 项目.

What I looked at. Looked here, but suggested macro is not a convinient solution. According to this and this Visual Studio does not save build logs for C# projects.

更具体的.我被要求提供有关我想要出错的特定警告的信息.这是MSB3245:无法解析此引用.无法找到程序集..."

More specific. I have been asked to provide information about the specific warning which I want to make an error of. This is "MSB3245: Could not resolve this reference. Could not locate the assembly..."

我被要求发布从当前问题的讨论和我公司的经验得出的结论.所以就是这里了.

I have been asked to post a conclusion from the discussion of the current question and my company's experience. So here it is.

回答.对于托管代码项目,无法在 Visual Studio 中的构建后事件中访问构建警告"(不包括一些非常具体的情况,请参阅有关 TFS 的建议).

Answer. There is no way "to access build warnings in a post-build event in Visual Studio" for managed code projects (excluding some very specific cases, see suggestion about TFS).

解决方法.目前,我们使用 MsBuild 来捕获警告并将其转换为错误,如此处.因此,开发人员在使用标准 Visual Studio(或 ReSharper)构建过程时无法访问此功能.然而,我们的持续集成服务器完全使用 MsBuild,因此它会通知我们我们感兴趣的警告和错误.

Workaround. Currently we use MsBuild to catch warnings and transform them into errors as it is described here. So developers do not have access to this functionality while they use standard Visual Studio (or ReSharper) build process. However our continuous integration server uses exactly MsBuild and thus it informs us about warnings and errors which we are interested in.

其他注意事项.

  1. 如果MsBuild中的将警告视为错误"功能 被引入,人们可以将其用于类似目的.
  2. 可以创建一个特殊的 MsBuild 任务,分析项目文件 (.csproj) 中的引用并将此引用与复制到输出文件夹的文件进行比较.此任务必须注入到每个必要的项目文件中.
  3. TFS 的某些组件可能会保存构建日志(对此不熟悉).
  1. If the "Treat warnings as errors" functionality in MsBuild were introduced, one could utilize it for similar purposes.
  2. One could create a special MsBuild task analyzing the references in project files (.csproj) and comparing this refernces with the files copied to the output folder. This task must be injected in every necessary project file.
  3. Some components of TFS probably save build logs (not familiar with this).