Question

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.

Was it helpful?

Solution

Well, embarrassing, but perhaps it will save someone some time.

Short answer, nlog.config file is in the wrong directory.

The solution has two projects, the service host/installer project and a project where the logic/work is done. Service installs from the debug folder for the service host/installer (still in early testing). The logic dll, is loaded from the logic project debug folder by the service hsot. The logic folder contains the nlog.config. So when the service starts the nlog.config file can't be found where the service host is running.

The service host has no reference to the service.dll, not a problem as the dll is loaded at run time, but the nlog.config file was an issue.

The simple solution for creating the service is to change the logic/work project output to be the same as the service host output directory.

OTHER TIPS

I had a System.TypeInitializationException when trying to create EventLog and set MaximumKilobytes if the source didn't exist.

Solved by moving EventLog.CreateEventSource() before I create the EventLog object.

if (!EventLog.SourceExists(SOURCE)) {
EventLog.CreateEventSource(SOURCE, LOG);
}
eventLog = new EventLog();
eventLog.Source = SOURCE;
eventLog.Log = LOG;
eventLog.MachineName = System.Environment.MachineName;
eventLog.MaximumKilobytes = 1024;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top