14

I can't write to the event log with NLog. I've been able to write to the console and to a file. I've turned on exceptions in NLog and am receiving no feedback from NLog.

Here is my 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"
      throwExceptions="true">
    <targets>
        <target name="console" xsi:type="Console" layout="${message}" />
        <target xsi:type="EventLog" name="eventlog" layout="${message}" log="Application" source="aaaTest"/>
        <target xsi:type="File" fileName="log.txt" name="file"/>
    </targets>

    <rules>
        <logger name="*" minlevel="Debug" writeTo="eventlog,console,file" />
    </rules>
</nlog>

In Event Viewer, I am looking at "Event Viewer (Local)" > "Windows Logs" > "Application". However, I see no instances of "aaaTest" (my defined source) in the log.

CRABOLO
  • 8,605
  • 39
  • 41
  • 68
sparks
  • 1,264
  • 1
  • 15
  • 29
  • If I run my application as administer, the logs messages will correctly show up in the event log. I am using Windows 7. Is there no other way to write to the event log unless I run my application as administer? (I stumbled upon this article that gave me the idea to try running as administer: http://webcache.googleusercontent.com/search?q=cache:8pYpa9wBFTEJ:connect.microsoft.com/VisualStudio/feedback/details/509224/security-exception-trying-to-access-application-event-log+my.application.log+securityexception&cd=1&hl=en&ct=clnk&gl=us&client=firefox-a) – sparks Jul 01 '10 at 21:34
  • See also https://github.com/NLog/NLog/wiki/Eventlog-target#notes (About needing to register EventLog-source) – Rolf Kristensen Nov 02 '22 at 18:49

1 Answers1

18

From nlog forum post

To be able to write to the EventLog an application must be registered as an event source. If you run VS as admin this happens automatically. If you create an installer and install your application it gets registered.

To register an app manually as event source I use the following script:

Set Args = WScript.Arguments
If Args.Count < 1 then
    WScript.Echo "USAGE: CreateEventSource.vbs <EventSourceName>"
    WScript.Quit
End If
EventSourceName = Args(0)

Set WshShell = WScript.CreateObject("WScript.Shell")
 
'Create event source
KeyName = "HKLM\SYSTEM\CurrentControlSet\Services\Eventlog\Application\" & EventSourceName & "\EventMessageFile"
'Change path to .NET Framework version used
WshShell.RegWrite KeyName,"%windir%\Microsoft.NET\Framework64\v2.0.50727\EventLogMessages.dll", "REG_EXPAND_SZ" 

See also: https://github.com/NLog/NLog/wiki/Eventlog-target#notes

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
Fabio Bonfante
  • 5,128
  • 1
  • 32
  • 37
  • 2
    Instead of using VBS, using this command line instead. Much easier. http://stackoverflow.com/questions/446691/how-to-create-windows-eventlog-source-from-command-line/1036133#1036133 "eventcreate /ID 1 /L APPLICATION /T INFORMATION /SO MYEVENTSOURCE /D "My first log" – Niko Gunadi Jan 15 '14 at 00:11
  • I discovered that you only have to run VS as admin once and create an Event Log whilst doing so. After that you can run as normal and it will still work (unless you change the `source` name). – SharpC Feb 05 '18 at 17:09