我想在动态代理拦截器方法中找到Controller和Action名称 我检查堆栈跟踪approch不是很好的方式beacase它不是最后的堆栈 这是我的代码
全球asax城堡配置
IWindsorContainer ioc = new WindsorContainer();
ioc.Register(
Component.For<IMyService>().DependsOn()
.ImplementedBy<MyService>()
.Interceptors<MyInterceptor>()
.LifeStyle.PerWebRequest);
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(ioc));
ioc.Register(
Component.For<IInterceptor>()
.ImplementedBy<MyInterceptor>());
控制器类
private IMyService _service;
public HomeController(IMyService service)
{
_service = service;
}
public ActionResult Index()
{
_service.HelloWorld();
return View();
}
服务类
public class MyService : IMyService
{
public void HelloWorld()
{
throw new Exception("error");
}
}
public interface IMyService
{
void HelloWorld();
}
拦截器类
//i want to find Controller name
public class MyInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
//?? controller name ?? method Name
invocation.Proceed();
}
}
答案 0 :(得分:1)
DynamicProxy不会公开来电者信息。
答案 1 :(得分:0)
我能够在loggingInterceptor
中获取类名和方法名使用 invocation.TargetType.Name
public class LoggingInterceptor : IInterceptor
{
...
public void Intercept(IInvocation invocation)
{
try
{
this.Logger.InfoFormat(
"{0} | Entering method [{1}] with paramters: {2}",
invocation.TargetType.Name,
invocation.Method.Name,
this.GetInvocationDetails(invocation));
invocation.Proceed();
}
catch (Exception e)
{
this.Logger.ErrorFormat(
"{0} | ...Logging an exception has occurred: {1}", invocation.TargetType.Name, e);
throw;
}
finally
{
this.Logger.InfoFormat(
"{0} | Leaving method [{1}] with return value {2}",
invocation.TargetType.Name,
invocation.Method.Name,
invocation.ReturnValue);
}
}
}