ServiceStack AppHost提供了一个Funq.Container,用于注册可在构造时注入服务的类型。这个容器可以用来注册一个ILog工厂,它返回一个适合它所在类型的ILog吗?
另外,给出以下AppHost:
public class AppHost : AppHostBase
{
public AppHost() : base("Example Web Services", Assembly.GetExecutingAssembly())
{
}
public override void Configure(Funq.Container container)
{
var baseLogFactory = new ServiceStack.Logging.NLogger.NLogFactory();
LogManager.LogFactory = new ServiceStack.Logging.Elmah.ElmahLogFactory(baseLogFactory);
// Would prefer to register a Func<Type, ILog> one time here
}
}
和服务:
public class FooService : IService<FooRequest>
{
static ILog Log { get { return LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); } }
// Would prefer:
// public ILog { get; set; }
public object Execute(FooRequest request)
{
Log.Info("Received request: " + request.Dump());
return new FooResponse();
}
}
我可以添加到AppHost.Configure以避免所有服务中的静态ILog样板(而只是使用普通的旧ILog属性)吗?
第三种方式,最简洁的是,我可以使用Funq.Container进行ILog注入而不是LogManager吗?
答案 0 :(得分:1)
container.Register<ILog>(
ctx => LogManager.LogFactory.GetLogger(typeof(IService))
);
现在您的服务可能如下所示:
public class FooService : IService<FooRequest>
{
public ILog { get; set; }
public object Execute(FooRequest request)
{
Log.Info("Received request: " + request.Dump());
return new FooResponse();
}
}