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

New Ingercept call within method Advice Type #102

Open
ivang7 opened this issue Sep 19, 2019 · 4 comments
Open

New Ingercept call within method Advice Type #102

ivang7 opened this issue Sep 19, 2019 · 4 comments
Milestone

Comments

@ivang7
Copy link

ivang7 commented Sep 19, 2019

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

@pamidur
Copy link
Owner

pamidur commented Sep 19, 2019

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

@ivang7
Copy link
Author

ivang7 commented Sep 19, 2019

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
Copy link
Author

ivang7 commented Sep 19, 2019

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

@pamidur
Copy link
Owner

pamidur commented Sep 20, 2019

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 pamidur added this to the 3.0 milestone Sep 20, 2019
@pamidur pamidur changed the title mask for hooks method (like as PostSharp, AspectJ) New Ingercept call within method Advice Type Oct 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants