如何将所有解析请求记录到Autofac容器? [英] How can I log all resolve requests to Autofac container?

查看:223
本文介绍了如何将所有解析请求记录到Autofac容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调试遗留代码库中的一些问题。我认为正在引发异常,因为无法从Autofac容器中解决某些异常。但是,我认为这个例外是在某个地方被埋葬的,我没有看到根本原因。我确信从控制器请求的东西是无法找到的,或者可以找到的东西具有不能满足的依赖性。没有任何保护条款等,所以我认为我得到一个空的参考问题。要尝试调试这个,我想查看在容器中找不到的所有请求。



有没有记录Autofac无法解决的所有请求?或者只是将所有请求记录到容器?

解决方案

您可以通过注册一个特殊的模块来添加到容器的日志记录,该模块将捕获准备所有注册事件:

  public class LogRequestsModule:Module 
{
保护覆盖void AttachToComponentRegistration(
IComponentRegistry componentRegistry,
IComponentRegistration注册)
{
//使用事件参数来记录详细信息
registration.Preparing + =(sender, args)=>
Console.WriteLine(
解析具体类型{0},
args.Component.Activator.LimitType);
}
}

这是最简单的方法,可能会得到你想要什么在准备事件记录信息之后,您将看到异常弹出,您将知道投掷哪个组件。



如果您想获得优惠,可以在容器上设置一些事件处理程序 ChildLifetimeScopeBeginning ResolveOperationBeginning ResolveOperationEnding CurrentScopeEnding 事件。




  • ChildLifetimeScopeBeginning 期间,您需要设置一些东西以自动附加到任何小孩生命周期 ResolveOperationBeginning

  • ResolveOperationBeginning 中,您将记录要解决的内容。

  • ResolveOperationEnding 中,您将记录任何异常。

  • CurrentScopeEnding 您需要取消订阅该范围内的任何事件,因此垃圾收集器可以清除所有事件的生命周期范围es。



Whitebox分析器项目有一个模块,它实现了一些这样更高级的日志记录,但它并没有为最新的Autofac设置,所以你必须使用它作为起点,不是剪切/粘贴样本。



同样,最简单的解决方案是我上面发布的模块。


I am trying to debug some problems in a legacy code base. I think is being caused by an exception being thrown because something can't be resolved from the Autofac container. However I think the exception is getting buried somewhere and I am not seeing the root cause. I am sure something is being requested from a controller that either can't be found or something that can be found has a dependency that can't be satisfied. There aren't any guard clauses etc. so I think I am getting a null reference issue. To try and debug this I want to see all requests that aren't found in the container.

Is there anyway to log all requests that Autofac couldn't resolve? Or alternatively just log all requests to the container?

解决方案

You can add logging for requests to the container by registering a special module that will catch the Preparing event for all registrations:

public class LogRequestsModule : Module
{
  protected override void AttachToComponentRegistration(
    IComponentRegistry componentRegistry,
    IComponentRegistration registration)
  {
    // Use the event args to log detailed info
    registration.Preparing += (sender, args) =>
      Console.WriteLine(
        "Resolving concrete type {0}",
        args.Component.Activator.LimitType);
  }
}

This is the simplest way to go and will probably get you what you want. Right after a Preparing event logs the information, you'll see the exception pop up and you'll know which component was throwing.

If you want to get fancier, you can set up some event handlers on the container ChildLifetimeScopeBeginning, ResolveOperationBeginning, ResolveOperationEnding, and CurrentScopeEnding events.

  • During ChildLifetimeScopeBeginning you'd need to set up something to automatically attach to any child lifetime ResolveOperationBeginning events.
  • During ResolveOperationBeginning you'd log what is going to be resolved.
  • During ResolveOperationEnding you'd log any exceptions coming out.
  • During CurrentScopeEnding you'd need to unsubscribe from any events on that scope so the garbage collector can clean up the lifetime scope with all of its instances.

The Whitebox profiler project has a module that implements some of this more advanced logging but it's not set up for the latest Autofac so you'd have to use it as a starting point, not a cut/paste sample.

Again, the easiest solution is that module I posted above.

这篇关于如何将所有解析请求记录到Autofac容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆