London Escorts sunderland escorts 1v1.lol unblocked yohoho 76 https://www.symbaloo.com/mix/yohoho?lang=EN yohoho https://www.symbaloo.com/mix/agariounblockedpvp https://yohoho-io.app/ https://www.symbaloo.com/mix/agariounblockedschool1?lang=EN
7.8 C
New York
Thursday, November 14, 2024

Easy methods to use DispatchProxy for AOP in .NET Core




public class CustomLogger : DispatchProxy the place T : class
{
    personal readonly ILogger _logger;
    personal T goal;
    protected override object Invoke
    (MethodInfo targetMethod, object[] args)
    {
       throw new NotImplementedException();
    }
    public static T Create(T goal)
    {
       throw new NotImplementedException();
    }
}

The CustomLogger class proven within the previous code makes use of Serilog to log knowledge on this instance. The next code snippet reveals how one can replace the Invoke methodology we created within the MyClassDispatchProxy class earlier to include logging capabilities.


protected override object Invoke
    (MethodInfo targetMethod, object[] args)
    {
        if(targetMethod == null)
            throw new ArgumentNullException(nameof(targetMethod));
        _logger.Info($"Coming into methodology: {targetMethod.Title}...");
         var outcome = targetMethod.Invoke(goal, args);
        _logger.Info($"Exiting methodology: {targetMethod.Title}...");
        return outcome;
    }

Word how the tactic calls are logged earlier than and after invocation within the Invoke methodology. If the targetMethod occasion is null, the Invoke methodology throws an exception. Within the Create methodology, we create a brand new occasion of the CustomLogger<T> class after which set the goal object primarily based on which the proxy object will intercept methodology calls.

Related Articles

Social Media Auto Publish Powered By : XYZScripts.com