1 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊
這是我最終得到的模塊:
public class LoggingModule : Module
{
? ? protected override void Load(ContainerBuilder builder)
? ? {
? ? ? ? builder.RegisterType<Log4NetLogFactory>().As<ILogFactory>().SingleInstance();
? ? ? ? builder.Register((c, p) => c.Resolve<ILogFactory>().GetLogger(p.TypedAs<Type>()));
? ? }
? ? protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration)
? ? {
? ? ? ? registration.Preparing +=
? ? ? ? ? ? (sender, args) =>
? ? ? ? ? ? {
? ? ? ? ? ? ? ? var forType = args.Component.Activator.LimitType;
? ? ? ? ? ? ? ? var logParameter = new ResolvedParameter(
? ? ? ? ? ? ? ? ? ? (p, c) => p.ParameterType == typeof(ILog),
? ? ? ? ? ? ? ? ? ? (p, c) => c.Resolve<ILog>(TypedParameter.From(forType)));
? ? ? ? ? ? ? ? args.Parameters = args.Parameters.Union(new[] { logParameter });
? ? ? ? ? ? };
? ? }
}
這可以在容器中注冊(cè):
builder.RegisterModule(new LoggingModule());
- 1 回答
- 0 關(guān)注
- 139 瀏覽
添加回答
舉報(bào)