How to write to eventLog with NLog from a dynamically loaded dll in a windows service.
Using NLog 2.0.1 I have a windows service that dynamically loads a dll, from within that dll I'm using(trying to) NLog to log to the EventLog. The eventLog is custom and is created by the service installer.
Error:
Service cannot be started. System.Reflection.TargetInvocationException:
Exception has been thrown by the target of an invocation.
---> System.TypeInitializationException: The type initializer for 'MyService.Worker' threw an exception.
---> NLog.NLogConfigurationException: Error during initialization of EventLog Target[eventLog_wrapped]
---> System.IO.IOException: The network path was not found.
at Microsoft.Win32.RegistryKey.Win32ErrorStatic(Int32 errorCode, String str)
at Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(RegistryHive hKey, String machineName, RegistryView view)
at System.Diagnostics.EventLog.GetEventLogRegKey(String machine, Boolean writable)
at System.Diagnostics.EventLog.FindSourceRegistration(String source, String machineName, Boolean readOnly, Boolean wantToCreate)
at System.Diagnostics.EventLog._InternalLogNameFromSourceName(String source, String machineName)
at System.Diagnostics.EventLog.LogNameFromSourceName(String source, String machineName)
at NLog.Targets.EventLogTarg...
I created a winForm app to test the logging and the logging works as expected, but when I try to do the same thing in my service it does not work.
I tried running the service under "Local System" and "Network Service", I get the same error. As for the "Network path...." there is no network path being accessed so I'm not sure what this trying to tell me.
my NLog config/target is:
<variable name="appName" value="MyApp" />
<variable name="source" value="MySource" />
<target xsi:type="EventLog"
name="log"
log="My Service"
source="${source}"
machineName="."
layout="${callsite}${newline} ${message}${newline}${exception:format=ToString}"
/>
Any ideas on how to get this working would be appreciated.