Alexander Schmidt
Alexander Schmidt

Reputation: 5743

The right way to log to EventLog using NLog

How can I avoid windows-complaining about missing descriptions for event ids when logging using NLog. When I use:

<target xsi:type="EventLog" 
        name="eventLog" 
        layout="${message}" 
        machineName="."     
        source="MyApp" 
        log="Application" />

and

<rules>
    <logger name="*" minlevel="Debug" writeTo="eventLog" />
</rules>

the entry will appear in the log. But Windows complains about missing description for the event id "0" which is right.

Do I have to do things like pointed out here to get a clean logging?

Upvotes: 11

Views: 23539

Answers (3)

VPP
VPP

Reputation: 789

<variable name="EventID" value="10010" />
<target xsi:type="EventLog" 
        name="eventLog" 
        layout="${message}"
        machineName="."     
        source="MyApp"
        log="Application"
        eventId="${event-properties:EventID:whenEmpty=0}" />
    
<rules>
    <logger name="*" minlevel="Debug" writeTo="eventLog" />
</rules>

And when you get the logger, it should be

public ILogger _logger { get; set; }
public int _eventID { get; set; }
public LogManager()
{
    _eventID = Convert.ToInt32(NLog.LogManager.Configuration.Variables["EventID"].ToString());
    this._logger = NLog.LogManager.GetCurrentClassLogger().WithProperty("EventID", _eventID);            
}

Upvotes: 0

Ra&#250;l Diego
Ra&#250;l Diego

Reputation: 429

I know it's an old post, but the configuration should be

<target xsi:type="EventLog" 
        name="eventLog" 
        layout="${message}"
        machineName="."     
        source="MyApp"
        log="Application"
        eventId="${event-properties:EventID:whenEmpty=0}" />

and

<rules>
    <logger name="*" minlevel="Debug" writeTo="eventLog" />
</rules>

See also: https://github.com/NLog/NLog/wiki/EventLog-target

Upvotes: 10

Simon The Cat
Simon The Cat

Reputation: 654

According to the NLog documentation there is an eventId tag that can be set. https://github.com/nlog/NLog/wiki/EventLog-target

<targets>
  <target xsi:type="EventLog"
          name="String"
          layout="Layout"
          machineName="String"
          source="Layout" 
          category="Layout"
          eventId="Layout"
          log="String" />
<!-- note: source is a string in NLog before 4.0 -->

</targets>

Upvotes: 1

Related Questions