aspect-injector icon indicating copy to clipboard operation
aspect-injector copied to clipboard

New `Ingercept call within method` Advice Type

Open ivang7 opened this issue 6 years ago • 4 comments

hello,

Does have support intercept runs method from foreign libs? example from postsharp (https://www.postsharp.net/logging): [assembly: Log( AttributePriority = 3, AttributeTargetAssemblies = "mscorlib" AttributeTargetTypes = "Microsoft.Win32.Registry*" )]

It needs for customize logging assert libs: https://github.com/allure-framework/allure-java/blob/f50f4b9ecd2d37bf04fca0193548ad954f715118/allure-assertj/src/main/java/io/qameta/allure/assertj/AllureAspectJ.java#L58

ivang7 avatar Sep 19 '19 05:09 ivang7

Hi, if you mean assembly-wide propagation with filter it is done like that:

using AspectInjector.Broker;
using ConsoleApp2;
using System;
using System.Reflection;

[assembly: LogTextMembers]

namespace ConsoleApp2
{
    class Program
    {
        public Program() { }
        public string Text { get; set; }
        static void Main(string[] args)
        {
            var prog = new Program();
            prog.Text = "Hello World!";
            Console.WriteLine(prog.Text);
        }
    }       

    [Aspect(Scope.Global)]
    [Injection(typeof(LogTextMembers), Propagation = PropagateTo.Everything, PropagationFilter ="Text")]
    class LogTextMembers : Attribute
    {
        [Advice(Kind.Before)]
        public void LogCall(
            [Argument(Source.Metadata)] MethodBase info, 
            [Argument(Source.Arguments)] object[] args
            )
        {
            Console.WriteLine($"Calling {info.Name}({string.Join(',', args)})");
        }
    }
}

Note that PropagationFilter is full featured regex filter

pamidur avatar Sep 19 '19 12:09 pamidur

thank you, that is i am need. where i can find informations about parameters (PropagationFilter, Propagation, etc), how set filter for concrete assembly (librariries), for example FluentAsserts and all her methods

ivang7 avatar Sep 19 '19 20:09 ivang7

in your example calling "Console.WriteLine(prog.Text);" not logged, it possible intercept call all functions?

ivang7 avatar Sep 19 '19 20:09 ivang7

Hi Now I see your point. You want to intercept method calls within the method body, something like this (after injection is applied):

static void Main(string[] args)
        {
            var prog = new Program();
            prog.Text = "Hello World!";
            LogCall(()=>Console.WriteLine(prog.Text));
        }

So you can use LogCall to do what ever you want with function call.

If this is the case, unfortunately this is not how aspect-injector currently works. It does not inject into the call site. It injects into the call's target. In this example the injection would be done not in the Main() method body, but in Console.WriteLine method body, which is (almost) impossible.

So by now, you can only intercept function written by you (the ones you're compiling).

However this good candidate for enhancement. I'll need to think how this can be implemented. Stay tuned :)

pamidur avatar Sep 20 '19 07:09 pamidur