Skip to content

OnExit is invoked before the end of the function happens. #277

@phillip-haydon

Description

@phillip-haydon

#28

One of the things I'm trying to use this lib for is to figure out the execution time of some methods by running Stopwatch on the Entry/Exit, but what I've realised is happening is that due to the OnExit being executed in the try instead of finally (I THINK), it results in logs like:

[05:20:57 INF] Invocation of Forward started at 08/11/2020 05:20:57
[05:20:57 INF] Invocation at Forward finished at 08/11/2020 05:20:57 and took 318ms to complete
[05:20:58 INF] Hello World 08/11/2020 05:20:58

So I think issue #28 is still an issue.

This is running on the latest version:

Fody 6.2.0
MethodDecorator 1.1.1
.NET Core 3.1 project (AWS .NET Core Lambda)

public class Banana
{
    [HandlerInterceptor]
    public async Task<string> DoSomething()
    {
        var result = await Task.Delay(2000).ContinueWith(x => "Hello World");

        if (result == "Hello World")
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine(DateTime.UtcNow);
        }
        else
        {
            Console.WriteLine("got nothing...");
            Console.WriteLine(DateTime.UtcNow);
        }

        return result;
    }

    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Assembly | AttributeTargets.Module)]
    [ExcludeFromCodeCoverage]
    public class HandlerInterceptorAttribute : Attribute, IMethodDecorator
    {
        private readonly Stopwatch stopwatch = new Stopwatch();

        public void Init(object instance, MethodBase method, object[] args)
        {
        }

        public void OnEntry()
        {
            stopwatch.Restart();
            Console.WriteLine("Start");
            Console.WriteLine(DateTime.UtcNow);
        }

        public void OnExit()
        {
            stopwatch.Stop();
            Console.WriteLine("End");
            Console.WriteLine(stopwatch.ElapsedMilliseconds);
        }

        public void OnException(Exception exception)
        {
        }
    }
}

The output of this is:

Start
8/11/2020 6:11:39 AM
End
0
Hello World!
8/11/2020 6:11:41 AM

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions