环境:
C#项目,Visual Studio 2008,C#,。Net 3.5,MSBuild
目的:
为我的C#项目运行我自己的自定义构建任务,并且基于平台(Soln配置平台),我在构建之前对Project对象本身进行了一些操作。就像将BuildAction设置为EmbeddedResource等。
完成后,我致电Project.Build()
。我不想在GlobalEngine上使用Project对象,因为一旦你修改了Project对象,它就会将文件标记为脏(并从TFS结账)。
问题:
因为我正在使用我自己的Engine实例和Project,所以我无法将构建输出,错误指向VS.我只从Project.Build()
中得到一个bool。
我没有发现任何我可以联系的事件,可以允许访问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();
}
答案 0 :(得分:4)
我想通了,只需实现自定义记录器并在事件处理程序中使用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);
}
}
并在MyCustomBuildTask类中设置并加载引擎和项目,
_logger = new MyCustomBuildLogger();
_logger.Log = Log;
_engine.RegisterLogger(_logger);