Skip to content

Instantly share code, notes, and snippets.

@maxfridbe
Created November 1, 2012 22:54
Show Gist options
  • Save maxfridbe/3997274 to your computer and use it in GitHub Desktop.
Save maxfridbe/3997274 to your computer and use it in GitHub Desktop.
Autofac Bootstrap and logging
public class LogInjectionModule : Module
{
protected override void Load(ContainerBuilder builder)
{
//log4net added by extension
log4net.Config.XmlConfigurator.Configure();
builder.Register<ILogger>((c, p) => new Log4NetLog(p.TypedAs<Type>()));
}
protected override void AttachToComponentRegistration(IComponentRegistry registry, IComponentRegistration registration)
{
log4net.Config.XmlConfigurator.Configure();
registration.Preparing += OnComponentPreparing;
}
static void OnComponentPreparing(object sender, PreparingEventArgs e)
{
var t = e.Component.Activator.LimitType;
e.Parameters = e.Parameters.Union(
new[] {
new ResolvedParameter((p, i) => p.ParameterType == typeof(ILogger), (p, i) => new Log4NetLog(t))
}
);
}
}
public static class Bootstrapper
{
public static IContainer Container { get; set; }
public static void Initialize()
{
var builder = new ContainerBuilder();
builder.RegisterModule<LogInjectionModule>();
builder.RegisterModule<OperationsModule>();
builder.RegisterModule<StudioToExternalCommonModule>();
Container = builder.Build();
}
public static T Get<T>()
{
return Container.Resolve<T>();
}
public static ILogger GetLogger(Type type)
{
return Container.Resolve<ILogger>(TypedParameter.From(type));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment