How to get build output, list of errors when running Project.Build () in custom build task?

Environment:

C # Projects, Visual Studio 2008, C #,. Net 3.5, MSBuild

Purpose:

Run my own build task for my C # projects and on platform (Soln config framework) I do some manipulation on the Project object before build. How to set BuildAction to EmbeddedResource etc.

Then I call Project.Build()

. I don't want to use the Project object in the GlobalEngine because it will flag the file as dirty (and checkout from TFS) as soon as you modify the Project object anyway.

Problem:

Since I am using my own Engine and Project instance, I cannot pipe the build output, errors in VS. I am getting bool from Project.Build()

. I can't find any events that I could hook up that might allow access to BuildErrorEventArgs

and the like. I know what I can use Log.LogErrorEvent()

to log messages to the VS error list. But I need to do the build output myself first.

Code:

// Execute method in my custom build task class, derives from a BaseTask class
public override bool Execute()
{
Engine engine = new Engine();
Project project = new Project(engine);
project.Load(ProjectName);
Log.LogMessage(Microsoft.Build.Framework.MessageImportance.High, "Got the Project");
// Set the project DefaultTargets to "Build" to be able to build with CSharp targets
project.DefaultTargets = "Build";
IsBuilt = project.Build(); // Isbuilt bool is a property in my BaseTask class
// Here where I want to get Build output and direct it to VS output window and errorlist window

engine.Shutdown();
return base.Execute();
}

      

+1


source to share


1 answer


I figured out, just implement my own logger and use Log.LogMessage or LogError in the event handlers

public class MyCustomBuildLogger : ILogger
{
    private IEventSource source;

 public void Initialize(IEventSource eventSource)
        {
            this.source = eventSource;
            //Events.ProjectStarted += new ProjectStartedEventHandler(Events_ProjectStarted);
            //Events.ProjectFinished += new ProjectFinishedEventHandler(Events_ProjectFinished);
            Events.WarningRaised += new BuildWarningEventHandler(Events_WarningRaised);
            Events.ErrorRaised += new BuildErrorEventHandler(Events_ErrorRaised);
            Events.BuildFinished += new BuildFinishedEventHandler(Events_BuildFinished);
            //Events.BuildStarted += new BuildStartedEventHandler(Events_BuildStarted);
            Events.MessageRaised += new BuildMessageEventHandler(Events_MessageRaised);
            //Events.CustomEventRaised += new CustomBuildEventHandler(Events_CustomEventRaised);

            Events.MessageRaised += new BuildMessageEventHandler(Events_MessageRaised);
        }

 void Events_ErrorRaised(object sender, BuildErrorEventArgs e)
        {
            // This logs the error to VS Error List tool window
            Log.LogError(String.Empty, 


             String.Empty,
                    String.Empty, 
                    e.File, 
                    e.LineNumber, 
                    e.ColumnNumber,
                    e.LineNumber, 
                    e.ColumnNumber, 
                    e.Message);
           }
}

      



and in the MyCustomBuildTask class, where you configure and load the engine and project,

_logger = new MyCustomBuildLogger();
_logger.Log = Log;
_engine.RegisterLogger(_logger);

      

+4


source







All Articles