Search code examples
msbuildmsbuild-taskmsbuild-target

In an MSBuild task, how can I access all warnings generated by the build?


My overall goal is to read all warnings at the end of a solution build, then log them as errors. This is distinct from the TreatWarningsAsErrors setting, because this should also create binaries for projects that had those warnings.

I expect to solve this with a custom MSBuild task, but I don't know what I need to pass it from the .targets file.

EDIT: I don't think this is a duplicate of this question, because that addresses parsing the log for a certain string, and issuing an error for that specific code. I want to get a list of all warnings in the build, regardless of their content.


Solution

  • You can't get build warnings from inside a task as it simply doesn't have access to this information. Warnings are raised and send to the MS Build logging infrastructure and handled there, which is totally divorced from the task execution.

    You can however write a custom logger to track warnings emitted by the build. With your custom logger, whenever a warning event is raised, simply record it and then when the build completes you can do whatever you want with that information.

    See the documentation here and here on how to write a custom logger.