title: How to write to eventLog with NLog from a dynamically loaded dll in a windows service
body: <p>How to write to eventLog with NLog from a dynamically loaded dll in a windows service.</p>
  
  <p>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.</p>
  
  <p>Error:</p>
  
  <pre><code>    Service cannot be started. System.Reflection.TargetInvocationException:
  Exception has been thrown     by the target of an invocation. 
  ---&gt; System.TypeInitializationException: The type initializer for 'MyService.Worker' threw an exception. 
  ---&gt; NLog.NLogConfigurationException: Error during initialization of EventLog Target[eventLog_wrapped] 
  ---&gt; 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...
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>my NLog config/target is:</p>
  
  <pre><code>&lt;variable name="appName" value="MyApp" /&gt;
  &lt;variable name="source" value="MySource" /&gt;
  
      &lt;target xsi:type="EventLog"
          name="log"
          log="My Service"
          source="${source}"
          machineName="."
          layout="${callsite}${newline} ${message}${newline}${exception:format=ToString}"
              /&gt;
  </code></pre>
  
  <p>Any ideas on how to get this working would be appreciated.</p>