我是依赖注入的新手,但Ninject
对Ninject.Extensions.Logging
我[Inject]
需要ILogger
它的地方感到满意。
然而,一些DelegatingHandlers破坏了所有的乐趣。
public class HttpsHandler : DelegatingHandler
{
[Inject]
public ILogger Logger { get; set; }
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
if (!string.Equals(request.RequestUri.Scheme, "https", StringComparison.OrdinalIgnoreCase))
{
Logger.Info(String.Format("{0}: is using HTTP", request.RemoteAddress());
return
Task.Factory.StartNew(
() =>
new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent("HTTPS Required")
});
}
return base.SendAsync(request, cancellationToken);
}
}
谁能指出我如何在委托处理程序中将 Ninject.Extensions.Logger.Nlog2 注入 Ilogger 的正确方向?
更新
我认为皮特在评论中让我朝着正确的方向前进(谢谢!)。我将以下构造函数添加到HttpsHandler
:
public HttpsHandler()
{
var kernel = new StandardKernel();
var logfactory = kernel.Get<ILoggerFactory>();
this.Logger = logfactory.GetCurrentClassLogger();
}
现在我已经让 Logger 工作了!
我唯一的问题是,这是正确的做法,还是反模式?