Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
phillip-haydon opened this issue Aug 11, 2020 · 9 comments
Open

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

phillip-haydon opened this issue Aug 11, 2020 · 9 comments

Comments

@phillip-haydon
Copy link

#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
@SimonCropp
Copy link
Member

happy to accept a pull request for this

@phillip-haydon
Copy link
Author

I did start trying to fix this by getting it building on Linux first:

#271

@PonyoLoveCode
Copy link

I am also using it, please tell me how to deal with this .

@PonyoLoveCode
Copy link

我确实开始尝试通过首先在 Linux 上构建它来解决这个问题:

第271章

I did start trying to fix this by getting it building on Linux first:

#271
How to deal with it? Thanks

@NurOrNuLL
Copy link

NurOrNuLL commented Apr 1, 2022

Still seeing this error

System: Windows - 10

    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
    </PropertyGroup>
    
    <ItemGroup>
        <PackageReference Include="Fody" Version="6.6.0" />
        <PackageReference Include="MethodDecorator.Fody" Version="1.1.0" />
    </ItemGroup>

do you plan to fix the bug?

@SimonCropp
Copy link
Member

@NurOrNuLL how about you step up and submit a pull request that fixes this

@phillip-haydon
Copy link
Author

#271

I actually had a fix for this bug that I wanted to submit after this but that was 2 years ago.

I no longer have that branch as I wiped my laptop and moved away from fody as it seemed there was no traction on getting this fixed.

@SimonCropp
Copy link
Member

@phillip-haydon yeah i dropped the ball on that one.

@alelom
Copy link

alelom commented Apr 9, 2023

Any update on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants