且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否可以在项目文件中指定MSBuild自定义记录器?

更新时间:2023-02-16 17:57:57

答案是否定的。记录器是构建引擎的配置设置。您无法启动构建,然后配置构建引擎。这是来自 MSDN -它显示了构建引擎初始化的正常流程:

The answer is no. Loggers are configuration settings for build engine. You cannot start the build, and then configure the build engine. Here is a snippet of code from MSDN -- it shows normal flow of the build engine initialization:

// Instantiate a new Engine object
Engine engine = new Engine();

...

// Instantiate a new FileLogger to generate build log
FileLogger logger = new FileLogger();
// Register the logger with the engine
engine.RegisterLogger(logger);

// Build a project file
bool success = engine.BuildProjectFile(@"c:\temp\validate.proj");

//Unregister all loggers to close the log file
engine.UnregisterAllLoggers();

可能会有方法对其进行破解,因为每个MSBuild任务都会收到对构建引擎对象的引用( IBuildEngine接口),您可以将其转换为Microsoft.BuildEngine.Engine并调用RegisterLogger方法。但是请注意,这将是错误的,因为您将为构建中的所有项目全局修改引擎,并且该引擎可能随时无法工作或停止工作。

There might be ways to hack it, because every MSBuild task receives a reference to the build engine object (IBuildEngine interface), that you could typecast to Microsoft.BuildEngine.Engine and call RegisterLogger method. However be aware that this would be just wrong, because you will be modifying engine globally for all projects in the build, and it might not work or stop working any time.