1 回答
TA贡献1828条经验 获得超3个赞
这是我最终得到的模块:
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 });
};
}
}
这可以在容器中注册:
builder.RegisterModule(new LoggingModule());
- 1 回答
- 0 关注
- 113 浏览
添加回答
举报