I am using Ninject interception to log errors on some of my methods. My interception class looks like this
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 just fine but there are a few problems I came across.
invocation.Request.Method.DeclaringType.Name
gives me the name of the interface, how to get the name of the real impementing class?
is there a way to get the argument values? I can get the parameter names using invocation.Request.Method.GetParameters
but I did not found a way to get the actual values
bool DoSomething(..)
and when it fails with exception I want it to look like the method returned false.You're talking about Ninject but I assume that you're only interested in features of Castle Dynamic Proxy and by IInvocation
you mean Castle.DynamicProxy.IInvocation
.
invocation.TargetType.Name
invocation.Arguments
invocation.ReturnValue
- you may set it when exception occurshttps://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've 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With