NLog does not write to a target file with filename containig dots
Asked Answered
S

2

7

When starting my windows service with "NT AUTHORITY\NETWORK SERVICE" credentials I experience a strange issue with NLog: it simply does not log anything to the file target if the filename contains dots.

I'm running windows service on my WinServer 2008 R2 Standard with .NET Framework 3.5 SP1 feature enabled, NLog.config is as follows:

<targets>
  <target xsi:type="File"
    name="f" 
    fileName="${basedir}/logs/${shortdate}.txt"
    encoding="utf-8"
    concurrentWrites="true"
    keepFileOpen="false"
    layout="${longdate} ${uppercase:${level}} ${message}"/>
</targets>
<rules>
  <logger name="*" minlevel="Trace" writeTo="f" />
</rules>

After some googling and experimenting with config I came up with a workaround by not including file extension in fileName parameter and it worked just fine, which solves the problem but does not look like a decent solution.

And what makes the issue look more like a some weird magic to me is the fact that I managed to solve the problem with log file extension in config of my second windows service (which is running on the same machine with the same credenials) simply by changing assembly information in project options.

Any ideas?

Snapback answered 17/4, 2013 at 4:53 Comment(0)
S
16

After enabling NLog's internal log file

<nlog 
  internalLogFile="c:\temp\nlogproblems.txt"  
  throwExceptions="true"  
  xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

i managed to catch UnathorizedAccessException

2013-04-17 11:06:14.0445 Error Exception in asynchronous handler 
  NLog.NLogRuntimeException: Exception occurred in NLog --->
  System.UnauthorizedAccessException: Access is denied. 
  (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

which led to a conclusion that I should fix the logs folder permissions.

Finally no more weird magic, I just had to allow NETWORK SERVICE writing into the logs folder.

Snapback answered 17/4, 2013 at 5:16 Comment(0)
C
0

in my case it was the user running the application pool.

it seems that with some cases you need specific users, my case was running an IHttpHandler and had other mehtods i was calling from ProcessRequest and for some reason from ProcessRequest itself it worked fine yet from the submethods i got the

Exception in asynchronous handler 
  NLog.NLogRuntimeException: Exception occurred in NLog --->
  System.UnauthorizedAccessException: Access is denied. 
  (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

and after reading 7kun answer i gave Everyone full control and it worked, and then changed to find the real missing user

Crossquestion answered 10/2, 2015 at 13:32 Comment(2)
Hello, can you please tell me how you gave everyone full control?Alaska
see this tutorial, youtube.com/watch?v=ytmYVbMEsGg, and instead of a specific user type "Everyone"Crossquestion

© 2022 - 2024 — McMap. All rights reserved.