ChildLifetimeScopeBeginning

ChildLifetimeScopeBeginning

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.如果你想变得更高级,你可以在容器上设置一些事件处理程序 ChildLifetimeScopeBeginning、ResolveOperationBeginning、ResolveOperationEnding 和 >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 容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-22 10:42
查看更多