views:

99

answers:

1

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 { set { _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?

A: 

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.

Sayed Ibrahim Hashimi
Thanks, but unfortunately I am unable to change the code for the custom tasks for various reasons.I don't really want to 'reach into' the task, just find which parameters it was passed (in the same way I can find out its name). Even obtaining an task's object reference would be enough to be able to then use reflection, although this would just be a hack.I do have various `Log.LogMessage()` calls from the tasks but none so consistent as the parameter being passed in. `LogMessage()` is quite vague too - perhaps listening for a custom event may be better in the general case?
gt