Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>AFAIK MSBuild has no built-in support to retrieve the warning count at a given point of the build script. You can however follow these steps to achieve this goal:</p> <ol> <li>Create a custom logger that listens for the warning event and counts the number of warnings</li> <li>Create a custom task that exposes an [Output] WarningCount property</li> <li>The custom task gets somehow the value of the warning count from the custom logger</li> </ol> <p>The most difficult step is step 3. For this there are several options and you can freely search them under IPC - Inter Process Comunication. Follows a working example of how you can achieve this. Each item is a different <em>Class Library</em>.</p> <p><strong>SharedMemory</strong></p> <p><a href="http://weblogs.asp.net/rosherove/archive/2003/05/01/6295.aspx" rel="noreferrer">http://weblogs.asp.net/rosherove/archive/2003/05/01/6295.aspx</a></p> <blockquote> <p>I've created a wrapper for named shared memory that was part of a larger project. It basically allows serialized types and object graphs to be stored in and retrieved from shared memory (including as you'd expect cross process). Whether the larger project ever gets completed is another matter ;-).</p> </blockquote> <p><strong>SampleLogger</strong></p> <p>Implements the custom logger that keeps track of the warning count.</p> <pre><code>namespace SampleLogger { using System; using Microsoft.Build.Utilities; using Microsoft.Build.Framework; using DM.SharedMemory; public class MySimpleLogger : Logger { private Segment s; private int warningCount; public override void Initialize(IEventSource eventSource) { eventSource.WarningRaised += new BuildWarningEventHandler(eventSource_WarningRaised); this.s = new Segment("MSBuildMetadata", SharedMemoryCreationFlag.Create, 65535); this.s.SetData(this.warningCount.ToString()); } void eventSource_WarningRaised(object sender, BuildWarningEventArgs e) { this.warningCount++; this.s.SetData(this.warningCount.ToString()); } public override void Shutdown() { this.s.Dispose(); base.Shutdown(); } } } </code></pre> <p><strong>SampleTasks</strong></p> <p>Implements the custom task that reads the number of warnings raised in the MSbuild project. The custom task reads from the shared memory written by the custom logger implemented in class library <em>SampleLogger</em>.</p> <pre><code>namespace SampleTasks { using System; using Microsoft.Build.Utilities; using Microsoft.Build.Framework; using DM.SharedMemory; public class BuildMetadata : Task { public int warningCount; [Output] public int WarningCount { get { Segment s = new Segment("MSBuildMetadata", SharedMemoryCreationFlag.Attach, 0); int warningCount = Int32.Parse(s.GetData() as string); return warningCount; } } public override bool Execute() { return true; } } } </code></pre> <p>Going for a spin.</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Main"&gt; &lt;UsingTask TaskName="BuildMetadata" AssemblyFile="F:\temp\SampleLogger\bin\debug\SampleTasks.dll" /&gt; &lt;Target Name="Main"&gt; &lt;Warning Text="Sample warning #1" /&gt; &lt;Warning Text="Sample warning #2" /&gt; &lt;BuildMetadata&gt; &lt;Output TaskParameter="WarningCount" PropertyName="WarningCount" /&gt; &lt;/BuildMetadata&gt; &lt;Error Text="A total of $(WarningCount) warning(s) were raised." Condition="$(WarningCount) &gt; 0" /&gt; &lt;/Target&gt; &lt;/Project&gt; </code></pre> <p>If you run the following command:</p> <pre><code>c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MSBuild test.xml /logger:SampleLogger.dll </code></pre> <p>This will be the output:</p> <pre><code>Microsoft (R) Build Engine Version 2.0.50727.3053 [Microsoft .NET Framework, Version 2.0.50727.3053] Copyright (C) Microsoft Corporation 2005. All rights reserved. Build started 30-09-2008 13:04:39. __________________________________________________ Project "F:\temp\SampleLogger\bin\debug\test.xml" (default targets): Target Main: F:\temp\SampleLogger\bin\debug\test.xml : warning : Sample warning #1 F:\temp\SampleLogger\bin\debug\test.xml : warning : Sample warning #2 F:\temp\SampleLogger\bin\debug\test.xml(15,3): error : A total of 2 warning(s) were raised. Done building target "Main" in project "test.xml" -- FAILED. Done building project "test.xml" -- FAILED. Build FAILED. F:\temp\SampleLogger\bin\debug\test.xml : warning : Sample warning #1 F:\temp\SampleLogger\bin\debug\test.xml : warning : Sample warning #2 F:\temp\SampleLogger\bin\debug\test.xml(15,3): error : A total of 2 warning(s) were raised. 2 Warning(s) 1 Error(s) Time Elapsed 00:00:00.01 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload