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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…