Using Simple Injector I have come across a common theme with my logger where I set the logger name in the constructor of the service object to which the logger is injected. When the service writes to the log, it can be easily identified by this logname.
Since LogNames
will be set for each service, the logger should be unique per object graph request.
I would like to do this automatically when the graph is being built, I have poked around in ExpressionBuilt()
but I am struggling and yet to get what i want to work - is this even possible (or an OK thing to want to do)?
My constructor code is below (this LogName
property setting code is common across most of my services).
Thanks,
Chris
public interface ILogger
{
void LogMessage(string message, LogLevel level,
ILoggableCompany company = null);
string LogName {get; set; }
}
public BusinessUnitService
{
private readonly IUnitOfWork unitOfWork;
private readonly ILogger logger;
public BusinessUnitService(IUnitOfWork unitOfWork,
ILogger logger)
{
this.unitOfWork = unitOfWork;
this.logger = logger;
// it would be great if we could take away this
// line and set it automatically
this.logger.LogName = this.GetType().ToString();
}
}