Retrieving Methods Intercepted by Ninject

I use Ninject interception to log errors in some of my methods. My interception class is as follows:

public class ErrorLoggingInterceptor : IInterceptor { private readonly ILogFactory _logFactory; public ErrorLoggingInterceptor(ILogFactory logFactory) { _logFactory = logFactory; } public void Intercept(IInvocation invocation) { try { invocation.Proceed(); } catch (Exception e) { var sb = new StringBuilder(); sb.AppendFormat("Executing {0}.{1} ",invocation.Request.Method.DeclaringType.Name,invocation.Request.Method.Name); sb.AppendFormat(" {0} caught: {1})", e.GetType().Name, e.Message); _logFactory.Error(sb.ToString()); } } } 

This interceptor class works fine, but there are a few problems that I have encountered.

  • invocation.Request.Method.DeclaringType.Name gives me the name of the interface, how to get the name of the real class impementing?

  • Is there any way to get the values โ€‹โ€‹of the arguments? I can get the parameter names using invocation.Request.Method.GetParameters , but I have not found a way to get the actual values

  • my interceptor is for swallowing exceptions. It works on void methods, but is there a way to make it work with non-void methods that provide a default value as a result? Let's say I have a bool DoSomething(..) , and when it fails with an exception, I want it to look like the method returns false.
+4
source share
2 answers

You are talking about Ninject, but I assume that you are only interested in the Castle Dynamic Proxy and IInvocation you mean Castle.DynamicProxy.IInvocation .

  • invocation.TargetType.Name
  • invocation.Arguments
  • invocation.ReturnValue - you can set it when an exception occurs

https://github.com/castleproject/Core/blob/master/src/Castle.Core/DynamicProxy/IInvocation.cs

When it comes to Ninject extensions, I would expect something similar (however, I never used it):

  • invocation.Request.Target.GetType().Name
  • invocation.Request.Arguments
  • invocation.ReturnValue

https://github.com/ninject/ninject.extensions.interception/blob/master/src/Ninject.Extensions.Interception/IInvocation.cs https://github.com/ninject/ninject.extensions.interception/blob/master /src/Ninject.Extensions.Interception/Request/IProxyRequest.cs

+6
source

Is there any way to get the argument values? I can get parameter names using invocation.Request.Method.GetParameters, but I didnโ€™t find a way to get the actual values

for the arguments I created, a little helper that will give you the name of the argument mapped to a value that can then be converted to a dictionary.

  private IEnumerable<KeyValuePair<string, object>> MapParameters(object[] arguments, ParameterInfo[] getParameters) { for (int i = 0; i < arguments.Length; i++) { yield return new KeyValuePair<string, object>(getParameters[i].Name, arguments[i]); } } 

will look like this:

  var mappedParameters = MapParameters(invocation.Arguments, invocation.Method.GetParameters()) .ToDictionary(x => x.Key, x => x.Value?.ToString()); 

Example

Method Signature: Get (int id)

use : receive (1)

output : {[id, 1]}

+2
source

Source: https://habr.com/ru/post/1492494/


All Articles