pamidur / aspect-injector

AOP framework for .NET (c#, vb, etc)
Apache License 2.0
745 stars 112 forks source link

aspect-injector autogenerated methods are displayed in stacktrace #156

Open alexsanv opened 3 years ago

alexsanv commented 3 years ago

Probably this is a duplicate of #30, but I can reproduce this issue in 2.5.0

using System;
using AspectInjector.Broker;

namespace aspect_injector_sampple
{
    [SampleAspect]
    class Program
    {
        public static void Main(string[] args)
        {
            throw new Exception("Sample Exception");
        }
    }

    [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class)]
    [Aspect(Scope.Global)]
    [Injection(typeof(SampleAspectAttribute))]
    public class SampleAspectAttribute : Attribute
    {
        [Advice(Kind.Around, Targets = Target.Method | Target.Public)]
        public object Handle(
            [Argument(Source.Target)] Func<object[], object> target,
            [Argument(Source.Arguments)] object[] args,
            [Argument(Source.Name)] string methodName,
            [Argument(Source.ReturnType)] Type returnType)
        {
           return target(args);
        }
    }
}

Stacktrace: image

pamidur commented 3 years ago

Hi @alexsanv , sorry for delay and thanks for your report! I'll take a look at it asap!

In any case it isn't always possible to hide parts of stacktrace, do you have any specific usecase where for example you parse stacktrace or is it just a matter of convenience?

alexsanv commented 3 years ago

@pamidur, this mostly is a matter of convenience. But there may be a valid use case, let me describe.

I implemented a helper library\nuget package for NUnit which uses aspect-injector to catch exceptions thrown in NUnit methods (e.g OneTimeSetup) wrapped around (Kind.Around) by aspect.

When you investigate a test failure, probably the first thing you look at is the exception's stacktrace. Having aspect-injector's autogenerated methods in exception's stacktrace in test failure may confuse people who are not familiar with the aspect-injector itself or don't even know that it is used in solution (directly or indirectly, as in my case with the library that uses aspect-injector).

Does this use case make any sense?

pamidur commented 3 years ago

Yes, totally. What was done before - we mark our methods with the attributes that prevent those methods appear in the stacktrace in a debugger (read - visual studio)

To hide them completely we'll need other approach, wild guess - either process the stacktrace and hide frames or probably properly rethrowing exceptions might work as well. Or maybe there are some other attributes to try. Anyway it requires investigation

jerviscui commented 3 years ago

153

pamidur commented 3 years ago

@jerviscui , yes it makes sense. With Kind.Exception it is up to user to decide how stack trace looks

Dreamescaper commented 2 years ago

Would it make sense to add StackTraceHiddenAttribute for auto-generated methods?

pamidur commented 2 years ago

omg it exists :) but it seems like it is only for net6 and higher

AspectInjector supports all libs that can target netstandard2,0 and netstandard2,0 does not have StackTraceHiddenAttribute

though I believe we can add this attribute only if target assembly is net6