如何在自定义生成任务中运行Project.Build()时获取构建输出,错误列表?

时间:2022-09-06 07:37:44

Environment:

C# projects, Visual studio 2008, C#, .Net 3.5, MSBuild

C#项目,Visual Studio 2008,C#,。Net 3.5,MSBuild

Objective:

Run my own custom build task for my C# projects, and based on the platform (Soln configuration platform), I do some manipulations to the Project object itself before build. Like setting BuildAction to EmbeddedResource etc.

为我的C#项目运行我自己的自定义构建任务,并且基于平台(Soln配置平台),我在构建之前对Project对象本身进行了一些操作。就像将BuildAction设置为EmbeddedResource等。

Once that's done I call Project.Build(). I don't want to use the Project object on the GlobalEngine, because it is going to mark the file dirty (and checkout from TFS), once you modify the Project object in anyway.

完成后,我调用Project.Build()。我不想在GlobalEngine上使用Project对象,因为无论如何都要修改Project对象,它会将文件标记为脏(并从TFS结账)。

Problem:

Because I am using my own instance of the Engine, and Project, I am not able to direct the build output, errors to VS. I only get a bool out of Project.Build(). I don't find any events I could hook up to that can allow access to BuildErrorEventArgs and things like that. I know I can use Log.LogErrorEvent() to log messages to VS error list. But I need to get the build output itself first to do that.

因为我使用自己的Engine实例和Project,所以我无法将构建输出,错误指向VS.我只从Project.Build()获得了一个bool。我没有发现任何可以连接的事件,可以允许访问BuildErrorEventArgs等等。我知道我可以使用Log.LogErrorEvent()将消息记录到VS错误列表。但我需要首先获得构建输出本身才能做到这一点。

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's 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's where I want to get Build output and direct it to VS output window and errorlist window

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

1 个解决方案

#1


4  

I figured it out, simply implement a custom logger and use Log.LogMessage or LogError in the event handlers

我想通了,只需实现一个自定义记录器并在事件处理程序中使用Log.LogMessage或LogError

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 setup and load the engine and project,

在MyCustomBuildTask类中,您可以在其中设置和加载引擎和项目,

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

#1


4  

I figured it out, simply implement a custom logger and use Log.LogMessage or LogError in the event handlers

我想通了,只需实现一个自定义记录器并在事件处理程序中使用Log.LogMessage或LogError

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 setup and load the engine and project,

在MyCustomBuildTask类中,您可以在其中设置和加载引擎和项目,

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