开发者

Read attributes of MSBuild custom tasks via events in the Logger

I am trying to write a MSBuild logger module which logs information when receiving TaskStarted events about the Task and its parameters.

The build is run with the command:

MSBuild.exe /logger:MyLogger.dll build.xml

Within the build.xml is a sequence of tasks, most of which have been custom written to compile a (C++ or C#) solution, and are accessed with the following custom Task:

<DoCompile Desc="Building MyProject 1" Param1="$(Param1Value)" /> 
<DoCompile Desc="Building MyProject 2" Param1="$(Param1Value)" /> <!-- etc -->

The custom build task DoCompile is defined as:

public class DoCompile : Microsoft.Build.Utilities.Task
{
    [Required]
    public string Description { se开发者_Go百科t { _description = value; } }

    // ... more code here ...
}

Whilst the build is running, as each task starts, the logger module receives IEventSource.TaskStarted events, subscribed to as follows:

public class MyLogger : Microsoft.Build.Utilities.Logger
{
    public override void Initialize(Microsoft.Build.Framework.IEventSource eventSource)
    {
        eventSource.TaskStarted += taskStarted;
    }

    private void taskStarted(object sender, Microsoft.Build.Framework.TaskStartedEventArgs e)
    {
        // write e.TaskName, attributes and e.Timestamp to log file
    }
}

The problem I have is that in the taskStarted() method above, I want to be able to access the attributes of the task for which the event was fired. I only have access to the logger code and cannot change either the build.xml or the custom build tasks.

Can anyone suggest a way I can do this?


You are approaching this in the wrong way. Loggers should not be able to reach into tasks which are being executed. It is the other way around. Your tasks should log the messages with the logger. So in your case you should enhance the DoCompile task (and other tasks you have created) to register all the messages with the logger. So inside of the Execute method just put some Log.LogMessage(...) messages to log the properties you are interested in.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜