2008-11-17 27 views
1

環境:如何在自定義生成任務中運行Project.Build()時獲取生成輸出,錯誤列表?

C#項目,Visual Studio 2008中,C#,.net 3.5的MSBuild

目的:爲我的C#項目

跑我自己的自定義生成任務,並根據該平臺(Soln配置平臺),我在構建之前對Project對象進行一些操作。就像將BuildAction設置爲EmbeddedResource等。

一旦完成,我打電話給Project.Build()。我不想在GlobalEngine上使用Project對象,因爲它會將文件標記爲髒(並從TFS簽出),一旦您修改了Project對象。

問題:

因爲我用我自己的發動機的情況下,和項目,我不能夠直接生成輸出,錯誤VS.我只從Project.Build()中得到一個布爾值。 我沒有發現任何可以連接的事件,可以訪問BuildErrorEventArgs以及類似的東西。我知道我可以使用Log.LogErrorEvent()將消息記錄到VS錯誤列表中。但是我需要首先獲得構建輸出來實現這一點。

代碼:

// 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(); 
} 

回答

4

我想通了,只是實現自定義記錄器,並在事件處理程序

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); 
      } 
} 

,並在MyCustomBuildTask類,其中使用Log.LogMessage或LOGERROR您設置並加載引擎和項目,

_logger = new MyCustomBuildLogger(); 
_logger.Log = Log; 
_engine.RegisterLogger(_logger); 
相關問題