【发布时间】:2012-06-22 02:42:52
【问题描述】:
使用Simple Injector 我遇到了一个与我的记录器相同的主题,我在注入记录器的服务对象的构造函数中设置记录器名称。当服务写入日志时,可以通过这个日志名轻松识别。
由于将为每个服务设置LogNames,因此每个对象图请求的记录器应该是唯一的。
我想在构建图表时自动执行此操作,我已经在ExpressionBuilt() 中四处寻找,但我正在苦苦挣扎,但还没有得到我想要的工作 - 这甚至可能(或者想要的好东西做)?
我的构造函数代码如下(LogName 属性设置代码在我的大多数服务中都很常见)。
谢谢,
克里斯
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();
}
}
【问题讨论】:
标签: c# .net design-patterns dependency-injection simple-injector