【发布时间】:2015-09-05 23:50:00
【问题描述】:
我有一个装饰器 SomethingLoggerDecorator,它应该用日志记录装饰 ISomething 实例:
public class SomethingLoggerDecorator : ISomething
{
private readonly ISomething decoratee;
private readonly ILogger logger;
public SomethingLoggerDecorator(ISomething decoratee, ILogger logger)
{
this.decoratee = decoratee;
this.logger = logger;
}
public void DoSomething()
{
this.logger.Info("Doing Something");
this.decoratee.DoSomething();
}
public void DoSomethingElse(string withThis)
{
this.logger.Info("Doing Something Else with " + withThis);
this.decoratee.DoSomethingElse(withThis);
}
}
如何使用 Simple Injector 用 SomethingLoggerDecorator 装饰 ISomething 的实例,并使用静态工厂方法 LogManager.GetLogger(decoratee.GetType()) 将 ILogger 的实例注入到每个装饰器中,其中 decoratee 是要执行的实际实例被装饰?此外,注入的ILogger 和SomethingLoggerDecorator 的生命周期应始终与被装饰者的生命周期相匹配。
【问题讨论】:
标签: c# logging dependency-injection decorator simple-injector