【问题标题】:Cannot get NLog custom target to work无法让 NLog 自定义目标工作
【发布时间】:2020-12-08 18:40:20
【问题描述】:

我真的很难让一个基本的自定义目标与 NLog 一起使用。

程序.cs

using NLog;
using NLog.Targets;



namespace NLogTestConsole
{
    class Program
    {
        private static Logger logger = null;

        static Program()
        {
            logger = LogManager.GetCurrentClassLogger();
            Target.Register<NLogTestConsole.Gerald>("Gerald");
        }

        static void Main(string[] args)
        {
            logger.Trace("Sample trace message");
            logger.Debug("Sample debug message");
            logger.Info("Sample informational message");
            logger.Warn("Sample warning message");
            logger.Error("Sample error message");
            logger.Fatal("Sample fatal error message");
        }
    }
}

NLogCustomTarget.cs

using NLog;
using NLog.Targets;



namespace NLogTestConsole
{

    [Target("Gerald")]
    public sealed class Gerald : TargetWithLayout
    {
        public Gerald()
        {
            // this.Host = "localhost";
        }

        //[RequiredParameter]
        //public string Host { get; set; }

        protected override void Write(LogEventInfo logEvent)
        {
            // Breakpoint here never gets hit
            string logMessage = this.Layout.Render(logEvent);

            System.Console.WriteLine("MYCUSTOMMSG:    " + logMessage);
        }

        private void SendTheMessageToRemoteHost(string host, string message)
        {
            // TODO - write me 
        }
    }

}

NLog.config

<?xml version="1.0" encoding="utf-8" ?>
<nlog autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off"
      internalLogFile="c:\temp\nlog-internal.log">

  <!--  optional, add some variables
        https://github.com/nlog/NLog/wiki/Configuration-file#variables    -->
  <variable name="myvar" value="myvalue"/>

  <!--  See https://github.com/nlog/nlog/wiki/Configuration-file
        for information on customizing logging rules and outputs. -->
  <targets>
    <!--    add your targets here
            See https://github.com/nlog/NLog/wiki/Targets for possible targets.
            See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.    -->

    <!--    Write events to a file with the date in the filename.
            <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
                layout="${longdate} ${uppercase:${level}} ${message}" />                    -->

    <target name="logfile" type="File" fileName="file.txt" />
    <target name="console" type="Console" />
    <target name="debugger" type="Debugger"/>
    <target name="Gerald" type="Gerald"/>
  </targets>

  <rules>
    <!--  Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace)  to "f"
          <logger name="*" minlevel="Debug" writeTo="f" />    -->

    <logger name="*" minlevel="Trace" writeTo="logfile" />
    <logger name="*" minlevel="Info" writeTo="console" />
    <logger name="*" minlevel="Trace" writeTo="debugger" />
    <logger name="*" minlevel="Trace" writeTo="Gerald" />
  </rules>
</nlog>

如果我注释掉“Gerald”目标,我会记录到文件、控制台和调试输出。有了那条线,没有任何效果 我做错了什么?

谢谢,

亚当。

【问题讨论】:

  • 感谢您的问题和以下答案,我的工作开始了。你为我节省了很多时间。
  • 不客气 :-)

标签: c# nlog


【解决方案1】:

启用内部日志并在 c:\temp\nlog-internal.log: 中查看错误:

internalLogLevel="Warn"

【讨论】:

  • 这给了我需要的线索。谢谢,朱利安。显然需要在调用 Target.Register 之后创建记录器。
【解决方案2】:

您需要修复您的代码,以便在您向 NLog 注册目标后创建记录器。请参阅下面突出显示和注释的代码行:

using NLog;
using NLog.Targets;       

namespace NLogTestConsole
{
    class Program
    {
        private static Logger logger = null;

        static Program()
        {
            // **********************************************************
            // the next two code lines are swapped around,
            // now the custom logger will work...
            // **********************************************************
            Target.Register<NLogTestConsole.Gerald>("Gerald");
            logger = LogManager.GetCurrentClassLogger();
        }

        static void Main(string[] args)
        {
            logger.Trace("Sample trace message");
            logger.Debug("Sample debug message");
            logger.Info("Sample informational message");
            logger.Warn("Sample warning message");
            logger.Error("Sample error message");
            logger.Fatal("Sample fatal error message");
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多