C# 将Autofac的诊断代码从V5转换为V6
我们在自己的代码中充分使用了Autofac生存期范围,因此能够在各种对象激活时跟踪其生存期范围非常重要,因此我编写了一个自定义模块来实现这一点(编译并适用于V5):C# 将Autofac的诊断代码从V5转换为V6,c#,autofac,C#,Autofac,我们在自己的代码中充分使用了Autofac生存期范围,因此能够在各种对象激活时跟踪其生存期范围非常重要,因此我编写了一个自定义模块来实现这一点(编译并适用于V5): 公共类LogRequestModule:模块 { 公共整数深度=0; 受保护的覆盖无效AttachToComponentRegistration(IComponentRegistry componentRegistry, iComponent(组件注册) { registration.Preparing+=此.Registratio
公共类LogRequestModule:模块
{
公共整数深度=0;
受保护的覆盖无效AttachToComponentRegistration(IComponentRegistry componentRegistry,
iComponent(组件注册)
{
registration.Preparing+=此.RegistrationUnpreparing;
registration.Activating+=this.RegistrationOnActivating;
基本。附件组件注册(组件注册,注册);
}
私有字符串GetPrefix()
{
返回新字符串('-',深度*2);
}
私有无效注册未准备(对象发送方,PreparingEventArgs PreparingEventArgs)
{
var lifetimeScopeTag=“*未知*”;
if(preparingEventArgs.Context为IInstanceLookup instanceLookup)
lifetimeScopeTag=instanceLookup.ActivationScope.Tag.ToString();
Console.WriteLine($“{this.GetPrefix()}{lifetimeScopeTag}{preparingEventArgs.Component.Activator.LimitType}”);
深度++;
}
私有void RegistrationOnActivating(对象发送方,ActivationEventArgs ActivationEventArgs)
{
var lifetimeScopeTag=“*未知*”;
if(activatingEventArgs.Context为IInstanceLookup instanceLookup)
lifetimeScopeTag=instanceLookup.ActivationScope.Tag.ToString();
深度--;
Console.WriteLine($“{this.GetPrefix()}{lifetimeScopeTag}激活{activatingEventArgs.Component.Activator.LimitType}”);
}
}
正在尝试更新到V6,并发现此代码未编译
我的问题只是如何在V6中实现这一点?
新的“System.Diagnostics.DiagnosticsSource”类能否用于实现等效功能
我查看了此文档,但不知道如何访问“LimitType”属性之类的功能,也不知道如何处理“OnPreparing”与“OnActivating”:
听起来您可能想使用v6()中添加的新内置诊断侦听器,或者编写自己的()来捕获特定信息(如生存期范围标记) 将诊断侦听器添加到解析路径确实会稍微降低速度;如果您需要更快(但粒度更小)的运行时诊断,您可能可以遵循类似于我们的log4net示例()的方法,这将允许您移植模块以使用管道而不是事件
public class LogRequestModule : Module
{
public int depth = 0;
protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry,
IComponentRegistration registration)
{
registration.Preparing += this.RegistrationOnPreparing;
registration.Activating += this.RegistrationOnActivating;
base.AttachToComponentRegistration(componentRegistry, registration);
}
private string GetPrefix()
{
return new string('-', depth * 2);
}
private void RegistrationOnPreparing(object sender, PreparingEventArgs preparingEventArgs)
{
var lifetimeScopeTag = "*Unknown*";
if (preparingEventArgs.Context is IInstanceLookup instanceLookup)
lifetimeScopeTag = instanceLookup.ActivationScope.Tag.ToString();
Console.WriteLine($"{this.GetPrefix()} {lifetimeScopeTag} {preparingEventArgs.Component.Activator.LimitType}");
depth++;
}
private void RegistrationOnActivating(object sender, ActivatingEventArgs<object> activatingEventArgs)
{
var lifetimeScopeTag = "*Unknown*";
if (activatingEventArgs.Context is IInstanceLookup instanceLookup)
lifetimeScopeTag = instanceLookup.ActivationScope.Tag.ToString();
depth--;
Console.WriteLine($"{this.GetPrefix()} {lifetimeScopeTag} Activating {activatingEventArgs.Component.Activator.LimitType}");
}
}