Created
July 7, 2012 13:32
-
-
Save pawelpabich/3066496 to your computer and use it in GitHub Desktop.
Log4Net and NLog modules for Autofac
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using Autofac; | |
using Autofac.Core; | |
using NLog; | |
using log4net; | |
using LogManager = NLog.LogManager; | |
namespace AutofacIdea | |
{ | |
public class NLogModule : LogModule<Logger> | |
{ | |
protected override Logger CreateLoggerFor(Type type) | |
{ | |
return LogManager.GetLogger(type.FullName); | |
} | |
} | |
public class Log4NetModule : LogModule<ILog> | |
{ | |
protected override ILog CreateLoggerFor(Type type) | |
{ | |
return log4net.LogManager.GetLogger(type.FullName); | |
} | |
} | |
public abstract class LogModule<TLogger> : Module | |
{ | |
protected abstract TLogger CreateLoggerFor(Type type); | |
protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration) | |
{ | |
var type = registration.Activator.LimitType; | |
if (HasPropertyDependencyOnLogger(type)) | |
{ | |
registration.Activated += InjectLoggerViaProperty; | |
} | |
if (HasConstructorDependencyOnLogger(type)) | |
{ | |
registration.Preparing += InjectLoggerViaConstructor; | |
} | |
} | |
private bool HasPropertyDependencyOnLogger(Type type) | |
{ | |
return type.GetProperties().Any(property => property.CanWrite && property.PropertyType == typeof(TLogger)); | |
} | |
private bool HasConstructorDependencyOnLogger(Type type) | |
{ | |
return type.GetConstructors() | |
.SelectMany(constructor => constructor.GetParameters() | |
.Where(parameter => parameter.ParameterType == typeof(TLogger))) | |
.Any(); | |
} | |
private void InjectLoggerViaProperty(object sender, ActivatedEventArgs<object> @event) | |
{ | |
var type = @event.Instance.GetType(); | |
var propertyInfo = type.GetProperties().First(x => x.CanWrite && x.PropertyType == typeof(TLogger)); | |
propertyInfo.SetValue(@event.Instance, CreateLoggerFor(type), null); | |
} | |
private void InjectLoggerViaConstructor(object sender, PreparingEventArgs @event) | |
{ | |
var type = @event.Component.Activator.LimitType; | |
@event.Parameters = @event.Parameters.Union(new[] | |
{ | |
new ResolvedParameter((parameter, context) => parameter.ParameterType == typeof(TLogger), (p, i) => CreateLoggerFor(type)) | |
}); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Autofac; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using NLog; | |
namespace AutofacIdea | |
{ | |
[TestClass] | |
public class LogModuleTests | |
{ | |
[TestMethod] | |
public void ShouldInjectLoggerViaConstructor() | |
{ | |
var builder = new ContainerBuilder(); | |
builder.RegisterModule<NLogModule>(); | |
builder.RegisterType<ClassWithLoggerInjectedViaConstructor>().AsSelf(); | |
var container = builder.Build(); | |
var instance = container.Resolve<ClassWithLoggerInjectedViaConstructor>(); | |
Assert.IsNotNull(instance.Logger); | |
} | |
[TestMethod] | |
public void ShouldInjectLoggerViaProperty() | |
{ | |
var builder = new ContainerBuilder(); | |
builder.RegisterModule<NLogModule>(); | |
builder.RegisterType<ClassWithLoggerInjectedViaProperty>().AsSelf().PropertiesAutowired(); | |
var container = builder.Build(); | |
var instance = container.Resolve<ClassWithLoggerInjectedViaProperty>(); | |
Assert.IsNotNull(instance.Logger); | |
} | |
public class ClassWithLoggerInjectedViaConstructor | |
{ | |
public Logger Logger { get; private set; } | |
public ClassWithLoggerInjectedViaConstructor(Logger logger) | |
{ | |
Logger = logger; | |
} | |
} | |
public class ClassWithLoggerInjectedViaProperty | |
{ | |
public Logger Logger { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment