如何从自定义MSBuild任务向控制台写入输出?

时间:2022-08-25 16:30:54

I'm trying to debug an MSBuild task, and I know there is some way to write to the MSBuild log from within a custom task but I forget how.

我正在调试一个MSBuild任务,我知道有一种方法可以从自定义任务中写入MSBuild日志,但是我忘记了怎么写。

2 个解决方案

#1


7  

The base Task class has a Log property you can use:

基本任务类有一个可以使用的日志属性:

Log.LogMessage("My message");

#2


1  

For unit testing purposes, I wrap the logger around a helper class

出于单元测试的目的,我围绕一个helper类包装日志记录器

public static void Log(ITask task, string message, MessageImportance importance)
{
    try
    {
        BuildMessageEventArgs args = new BuildMessageEventArgs(message, string.Empty, 
            task.ToString(), importance);
        task.BuildEngine.LogMessageEvent(args);
    }
    catch (NullReferenceException)
    {
        // Don't throw as task and BuildEngine will be null in unit test.
    }
}

Nowadays I'd probably convert that into an extension method for convenience.

现在为了方便,我可能会把它转换成扩展方法。

#1


7  

The base Task class has a Log property you can use:

基本任务类有一个可以使用的日志属性:

Log.LogMessage("My message");

#2


1  

For unit testing purposes, I wrap the logger around a helper class

出于单元测试的目的,我围绕一个helper类包装日志记录器

public static void Log(ITask task, string message, MessageImportance importance)
{
    try
    {
        BuildMessageEventArgs args = new BuildMessageEventArgs(message, string.Empty, 
            task.ToString(), importance);
        task.BuildEngine.LogMessageEvent(args);
    }
    catch (NullReferenceException)
    {
        // Don't throw as task and BuildEngine will be null in unit test.
    }
}

Nowadays I'd probably convert that into an extension method for convenience.

现在为了方便,我可能会把它转换成扩展方法。