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

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

问题描述

我正在尝试调试遗留代码库中的一些问题.我认为是由于无法从 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.

无论如何要记录 Autofac 无法解决的所有请求?或者只是将所有请求记录到容器中?

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

推荐答案

您可以通过注册一个特殊模块来为容器添加请求日志,该模块将捕获所有注册的 Preparing 事件:

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);
  }
}

这是最简单的方法,可能会得到你想要的.在 Preparing 事件记录信息后,您会立即看到异常弹出,并且您会知道哪个组件正在抛出.

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.

如果你想变得更高级,你可以在容器上设置一些事件处理程序 ChildLifetimeScopeBeginningResolveOperationBeginningResolveOperationEnding>CurrentScopeEnding 事件.

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

  • ChildLifetimeScopeBeginning 期间,您需要设置一些东西以自动附加到任何子生命周期 ResolveOperationBeginning 事件.
  • ResolveOperationBeginning 期间,您将记录将要解决的问题.
  • ResolveOperationEnding 期间,您会记录任何出现的异常.
  • CurrentScopeEnding 期间,您需要取消订阅该范围内的任何事件,以便垃圾收集器可以清理生命周期范围及其所有实例.
  • 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.

Whitebox 分析器项目有一个模块 实现了一些更高级的日志记录,但它不是为最新的 Autofac 设置的,因此您必须将其用作起点,而不是剪切/粘贴示例.

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天全站免登陆