Amber
Amber

Reputation: 1249

Logging for each method in "delegate"

I have a delegate:

private delegate void ActivitySteps(string inputFolder, string outputFolder);

In one of the methods in my class I register various activities to it :

this.activitySteps = A;
this.activitySteps += B;

and so on...

Finally I invoke it :

this.activitySteps.Invoke(inputFolder, outputFolder);

Now I want to add logging so that I know which of the event i.e. method failed while invoking the delegate. (A or B or some other). Is there a way I could do that so that I don't have to log in each method and could centralize the logging within delegate.

Upvotes: 3

Views: 1598

Answers (1)

Kyle
Kyle

Reputation: 6684

You could use GetInvocationList() when invoking the delegates:

foreach( ActivitySteps del in this.activitySteps.GetInvocationList() )
{
    try
    {
        del.Invoke( inputFolder, outputFolder );
    }
    catch( Exception ex )
    {
        Console.WriteLine( "{0} threw {1}", del.Method.Name, ex );
    }
}

Upvotes: 3

Related Questions