The right way to log to EventLog using NLog
Asked Answered
M

3

10

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?

Misreckon answered 11/8, 2013 at 17:46 Comment(4)
Have you seen this SO question: #17321433?Amblyoscope
@Amblyoscope Yes, but where do I set the ID then?Misreckon
It depends what do you want to have in your ID, you can use some custom values as descirbed in the linked question or you can use any of the built in renderers: github.com/nlog/NLog/wiki/Layout-RenderersAmblyoscope
@Amblyoscope Works but is still ugly. But thats not the fault of NLog so my questions is answered so far. Feel free to add an answer and I'll mark it.Misreckon
C
9

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

Cacao answered 20/10, 2014 at 15:6 Comment(2)
Thanks. This wasn't the problem. I just corrected my querstion above. It's all about logging to Windows EventLog. How can I tell NLog to use a certain event id. Dynamic ids would be even better.Misreckon
@AlexanderSchmidt You can specify the eventId-property for the target, and provide the EventId when logging.Bitumen
R
1

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>
Rorke answered 18/12, 2015 at 14:50 Comment(0)
C
0
<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);            
}
Colored answered 8/2, 2024 at 16:39 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.