2016-12-01 2 views
1

Nlog 2.1を使用していて、別のeventIdを使用してWindowsイベントロガーにエラーを書き込もうとしています。さまざまなエラーをより明確に区別するため。 eventIdプロパティを指定すると機能しますが、そうでない場合はWindowsイベントロガーにレコードが表示されません。NLog 2.1 EventLogのEventIdが指定されていないと動作しない

NLog.configファイル:

<?xml version="1.0" encoding="utf-8" ?> 
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

<targets> 

<target name="console" xsi:type="ColoredConsole" 
     layout="${date:format=HH\:mm\:ss}|${level:uppercase=true}|${message}" /> 

<target xsi:type="EventLog" 
    name="eventlog" 
    layout="{${newline} 
    &quot;Logger&quot;: &quot;${logger}&quot;,${newline} 
    &quot;StackTrace&quot;: &quot;${stacktrace}&quot;,${newline} 
    &quot;Message&quot;: &quot;${message}&quot;,${newline} 
    &quot;Exception&quot;: &quot;${exception:format=ToString,Data}&quot;${newline}}" 
    machineName="." 
    source="CareFusion Analytics Agent Service" 
    eventId="${event-properties:EventID}" 
    log="Application" /> 
    </targets> 

    <rules> 
    <logger name="*" minlevel="Trace" writeTo="console" /> 
    <logger name="*" minlevel="Error" writeTo="eventlog" /> 
    </rules> 
</nlog> 

使用法:

private static void Main(string[] args) 
    { 
     Logger logger = LogManager.GetCurrentClassLogger(); 

     logger.Error("Sample error message"); //This is not working 

     LogEventInfo logEvent = new LogEventInfo() 
     { 
      Level = LogLevel.Error, 
      Message = "Hello",     
      LoggerName = logger.Name 

     }; 
     logEvent.Properties.Add("EventID", 400); 

     logger.Log(logEvent); //This is working 


     Console.WriteLine("Press any key...."); 
     Console.ReadKey(); 
    } 
+0

あなたのコードでそれをテストし、「動作しない」と「作業言います"、それはあなた自身のlogeventinfoを構築するときに機能しますか? – Julian

+0

はい、LogEventInfoを使用して動作します – Farukh

答えて

1

コールlogger.Error("Sample error message");は整数に${event-properties:EventID}を変換するEventLogTarget試みとして、うまくいきません。

NLogのレイアウトレンダリングでnull(ただしstring.Empty)が返されることはないため、例外が発生します。これはNLogによってキャッチされます。 NLog's internal logにはFormatExceptionが表示されます。

だから我々はあなたがwhenEmptyであることを行うことができ、デフォルト値を指定する必要があります。

<target xsi:type="EventLog" 
     ... 
     eventId="${event-properties:EventID:whenEmpty=0}" /> 

がPS:NLog 4.3.11

関連する問題