Nlog 3.1与温莎城堡不记录 [英] Nlog 3.1 with Castle Windsor not logging

查看:78
本文介绍了Nlog 3.1与温莎城堡不记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将NLog(3.1)与Windsor Castle Facility一起使用,但是它对我不起作用(没有错误,没有任何反应)

I'm trying to use NLog (3.1) with Windsor Castle Facility, but it's not working for me (no errors, nothing happens)

这是我到目前为止的步骤:

These are my steps so far:

  1. 从Nuget下载:Castle Windsor NLog集成
  2. 从Nuget下载:NLog配置
  3. 像这样更新nlog配置:

  1. Downloaded from Nuget: Castle Windsor NLog integration
  2. Downloaded from Nuget: NLog Configuration
  3. Updates nlog config like this:

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

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

添加了Windsor安装程序

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.

添加了记录器类,如下所示:

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; }
        }
    }
}

  • 在像这样的控制器中使用它:

  • Using it in a controller like this:

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

  • 但是我看不到文件创建.

    But I don't see the file created.

    缺少任何步骤吗?

    先谢谢了.吉列尔莫.

    推荐答案

    您需要将Logger属性放到要记录某些内容的每个类中,并且该类的实例必须由Windsor创建/管理.

    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.

    因此您需要将其添加到您的控制器中:

    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.");
        }
    }
    

    它不能与MyLogger一起使用,因为该类不是由Windsor管理/创建的,因此不会注入您的Logger属性.但是,由于控制器对等是由Windsor创建的,并且ILogger inejtion正在其中起作用:

    It was not working with the MyLogger because that class was not managed/created by Windsor so it was not injected your Logger property. But because the controller isntances are created by Windsor and ILogger inejtion is wokring in them: Windsor Tutorial - Part Five - Adding logging support

    这篇关于Nlog 3.1与温莎城堡不记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆