2017-06-29 77 views
0

我真的很努力地獲得一個使用NLog的基本自定義目標。無法讓NLog自定義目標正常工作

的Program.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> 

如果我註釋掉 「傑拉德」 的目標我得到記錄到文件,控制檯,並調試輸出。隨着那條線,沒有任何工作 我有什麼問題?

謝謝,

亞當。

回答

0

啓用內部日誌,看看錯誤在C:\ TEMP \ n日誌-internal.log:

​​
+1

這給了我我需要的線索。謝謝,朱利安。在對Target.Register的調用之後,顯然需要創建記錄器*。 –