title: Topshelf, NLog and File Permissions
body: <p>I have a Windows service application that uses the TopShelf library and I'm installing it in AWS during the cfn-init using the handy command line features that you get with topshelf.</p>
  
  <pre><code>C:\handy_service\&gt; HandyService.exe install start
  </code></pre>
  
  <p>This basically installs the service in the registry and then calls <code>sc start</code>, but it's quite useful because it checks the service name matches what you expect and it allows you to configure the user that service will run as using the nice fluent API.</p>
  
  <p>The installer code also writes some diagnostic logs to NLog if the service is configured to use NLog in general.</p>
  
  <p>The problem is this: the installer runs as the default local administrator account that the AMI starts with and the NLog file gets created by this user. When the service starts up as the Network Service user, it doesn't have permission to write to the NLog log file.</p>
  
  <p>How can I get my service to write to the log file? I've thought about setting the permissions programmatically but it looks nasty and I'd have to determine the log file name as this is generated dynamically based on the ec2 instance id. Also, it's not entirely obvious at what point the log file is first created. The easiest hack that I might go with is having two NLog.configs and switching one out at the end of the install after the logger is flushed. But because there is some overlap in time between the service starting and the installer exiting, I expect I'll lose a few lines of logging here.</p>
  
  <p>Any clean suggestions would be greatly appreciated!</p>