불행히도 분석은 현재 Autofac의 약점 중 하나입니다. There is a repository where some work was started on an analytics package하지만 확실히 약간의 차이가 있습니다 - 예를 들어, 객체가 가을 활성화 수명 범위 배치 할 때 당신이 볼 수있는 경우 추적 할 수 있지만, 개별 객체이의 일부로 배치 할 때 추적 할 수 없습니다 범위. 그것에 대한 이벤트가 없습니다. 정품 인증에 대한
아주 간단한 추적 모듈은 다음과 같을 것이다 :
using System;
using System.Collections.Generic;
using System.Linq;
using Autofac;
using Autofac.Core;
namespace DiagnosticDemo
{
public class TrackingModule : Module
{
private readonly IDictionary<Type, int> _activations = new Dictionary<Type, int>();
private readonly object _syncRoot = new object();
public void WriteActivations()
{
foreach (var pair in this._activations.Where(p => p.Value > 0))
{
Console.WriteLine("* {0} = {1}", pair.Key, pair.Value);
}
}
protected override void AttachToComponentRegistration(
IComponentRegistry componentRegistry,
IComponentRegistration registration)
{
if (registration.Ownership == InstanceOwnership.OwnedByLifetimeScope)
{
registration.Activated += this.OnRegistrationActivated;
}
}
private void OnRegistrationActivated(
object sender,
ActivatedEventArgs<object> e)
{
if (e.Instance is IDisposable)
{
var type = e.Instance.GetType();
Console.WriteLine("Activating {0}", type);
lock (this._syncRoot)
{
if (this._activations.ContainsKey(type))
{
this._activations[type]++;
}
else
{
this._activations[type] = 1;
}
}
}
}
}
}
당신이 뭔가를 같이 사용할 것이다 : 그러나
static void Main(string[] args)
{
var trackingModule = new TrackingModule();
var builder = new ContainerBuilder();
// Register types, then register the module
builder.RegisterType<Consumer>().As<IConsumer>();
builder.RegisterType<DisposableDependency>().As<IDependency>();
builder.RegisterType<NonDisposableDependency>().As<IDependency>();
builder.RegisterModule(trackingModule);
var container = builder.Build();
// Do whatever it is you want to do...
using (var scope = container.BeginLifetimeScope())
{
scope.Resolve<IConsumer>();
}
// Dump data
Console.WriteLine("Activation totals:");
trackingModule.WriteActivations();
}
, 이것은 어떤 항목을 알려하지 않을
은이 아니므로 알고 싶습니다. 그래도 아이디어를 얻거나 적어도 도움이 될 수 있습니다.
Autofac에서 분석 기능을 개선하는 데 관심이 있으시면 take PRs or specific design ideas for how to improve입니다.
링크를 가져 와서 링크 된 autofac-analytics 저장소로 이식하기 시작하지 않았습니까? 델타 란 무엇입니까? –
애널리틱스 저장소가 현재 양식에서 실제로 사용할 수 없다고 생각합니다. 화이트 박스는 아직 완전히 완성 된 방법이라고 생각합니다. (애널리틱스가 앞으로 나아지지 않으면 나는 그것을 놓쳤습니다 :-)) –