简体   繁体   English

Nlog 3.1与温莎城堡不记录

[英]Nlog 3.1 with Castle Windsor not logging

I'm trying to use NLog (3.1) with Windsor Castle Facility, but it's not working for me (no errors, nothing happens) 我正在尝试将NLog(3.1)与Windsor Castle Facility一起使用,但是它对我不起作用(没有错误,没有任何反应)

These are my steps so far: 到目前为止,这是我的步骤:

  1. Downloaded from Nuget: Castle Windsor NLog integration 从Nuget下载:Castle Windsor NLog集成
  2. Downloaded from Nuget: NLog Configuration 从Nuget下载:NLog配置
  3. Updates nlog config like this: 像这样更新nlog配置:

     <target xsi:type="File" name="f" fileName="d:\\nlog.log" layout="${longdate}|${level:uppercase=true}|${logger}|${message}" /> 

     <logger name="*" minlevel="Trace" writeTo="f" /> 

  4. Added Windsor Installer 添加了温莎安装程序

      public class LoggingInstaller : IWindsorInstaller { public void Install(IWindsorContainer container, IConfigurationStore store) { container.AddFacility<LoggingFacility>(f => f.LogUsing(LoggerImplementation.NLog).WithConfig("NLog.config")); } } 

    Which I'm calling it (I've checked that a breakpoint in there is being hit. 我称之为(我已经检查了其中的断点被击中。

  5. Added the logger class like this: 添加了记录器类,如下所示:

     namespace WebApi.App_Start { public class MyLogger { private ILogger logger = NullLogger.Instance; public ILogger Logger { get { return logger; } set { logger = value; } } } } 
  6. Using it in a controller like this: 在像这样的控制器中使用它:

     new MyLogger().Logger.Info("New Request Created."); 

But I don't see the file created. 但我看不到文件已创建。

Any step missing? 缺少任何步骤?

Thanks in advance. 提前致谢。 Guillermo. 吉列尔莫。

You need to put the Logger property to every class where you want to log something and the instances of the class has to be created/managed by Windsor. 您需要将Logger属性放到要记录某些内容的每个类中,并且该类的实例必须由Windsor创建/管理。

So you need to add it to your controller: 因此,您需要将其添加到您的控制器中:

public class MyController : Controller
{
    private ILogger logger = NullLogger.Instance;

    public ILogger Logger
    {
        get { return logger; }
        set { logger = value; }
    }

    public ActionResult MyAction() 
    {
         Logger.Info("New Request Created.");
    }
}

It was not working with the MyLogger because that class was not managed/created by Windsor so it was not injected your Logger property. 它无法与MyLogger一起使用,因为该类不是由Windsor管理/创建的,因此未注入Logger属性。 But because the controller isntances are created by Windsor and ILogger inejtion is wokring in them: Windsor Tutorial - Part Five - Adding logging support 但是,由于控制器实例是由Windsor创建的,而ILogger inejtion也很麻烦: Windsor教程-第五部分-添加日志支持

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM