I'm wanting to register a component to resolve with parameters based on the class that it might be resolving for. (That sounds a bit confusing, so I'll show an example).
Here's an object that uses a logger:
class MyObject : IMyObject
{
public ILogger Logger;
public MyObject(ILogger logger)
{
Logger = logger;
}
}
Now the logger that is passed in COULD be different from class to class. So I've got a rather patched idea for how to do that below:
class MyLogger : ILogger
{
public string Name{get; protected set;}
public static ILogger GetLogger(string className)
{
Name = className;
MyLogger logger;
// Do something to choose a logger for that specific class
return logger;
}
}
So when I register Logger I want to be able to tell it the className. I'm hoping there's a way to do it similar to this:
ContainerBuilder builder = new ContainerBuilder();
builder.Register<MyLogger>(ctx =>
{
string className = //Get resolving class name somehow;
return MyLogger.GetLogger(className);
}).As<ILogger>();
builder.Register<MyObject>().As<IMyObject>();
var container = builder.Build();
IMyObject myObj = container.Resolve<IMyObject>();
//myObject.Logger.Name should now == "MyObject"
The reason I want to do it this way is to avoid registering each class I implement with a logger with autofac in code. I want to be able to register all of the objects in xml, and simply have a LoggerModule, that adds this registration.
Thanks in advance!
When resolving a service, Autofac will automatically chain down the entire dependency hierarchy of the service and resolve any dependencies required to fully construct the service.
Register by Type var builder = new ContainerBuilder(); builder. RegisterType<ConsoleLogger>(); builder. RegisterType(typeof(ConfigReader)); When using reflection-based components, Autofac automatically uses the constructor for your class with the most parameters that are able to be obtained from the container.
AutoFac provides better integration for the ASP.NET MVC framework and is developed using Google code. AutoFac manages the dependencies of classes so that the application may be easy to change when it is scaled up in size and complexity.
Here's what I do (ILog is just my own wrapper around log4net):
public class LoggingModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.Register((c, p) => GetLogger(p.TypedAs<Type>()));
}
protected override void AttachToComponentRegistration(
IComponentRegistry registry, 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});
};
}
public static ILog GetLogger(Type type)
{
return new Log4NetLogger(type);
}
}
public interface ILog
{
void Debug(string format, params object[] args);
void Info(string format, params object[] args);
void Warn(string format, params object[] args);
void Error(string format, params object[] args);
void Error(Exception ex);
void Error(Exception ex, string format, params object[] args);
void Fatal(Exception ex, string format, params object[] args);
}
public class Log4NetLogger : ILog
{
private readonly log4net.ILog _log;
static Log4NetLogger()
{
XmlConfigurator.Configure();
}
public Log4NetLogger(Type type)
{
_log = LogManager.GetLogger(type);
}
public void Debug(string format, params object[] args)
{
_log.DebugFormat(format, args);
}
public void Info(string format, params object[] args)
{
_log.InfoFormat(format, args);
}
public void Warn(string format, params object[] args)
{
_log.WarnFormat(format, args);
}
public void Error(string format, params object[] args)
{
_log.ErrorFormat(format, args);
}
public void Error(Exception ex)
{
_log.Error("", ex);
}
public void Error(Exception ex, string format, params object[] args)
{
_log.Error(string.Format(format, args), ex);
}
public void Fatal(Exception ex, string format, params object[] args)
{
_log.Fatal(string.Format(format, args), ex);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With