Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog can't write to the log file in the install directory

Tags:

nlog

I have an application that logs, and I don't get any file-based logging when I deploy it to the default directory. It seems clear that the reason is that writing to "c:\program files (x86)\blah" requires elevated privelege levels which my app doesn't have. So I end up telling users "install to "c:\blah" and it will work fine, which it does. But that seems rubbish. Any alternative?

like image 203
Julian Gold Avatar asked Feb 18 '23 20:02

Julian Gold


2 Answers

You could configure NLog to log to a different folder. NLog has SpecialFolderLayoutRenderer that allows you to use .NET's special folders. You should be able to specify the log file name using the SpecialFolderLayoutRenderer. Maybe something like this (I have not tried this):

<target name="file" xsi:type="File" layout="${longdate} | ${logger} | "${level} | ${message}" fileName="${specialfolder:folder=MyDocuments:dir=LogFiles:file=${shortdate}.log}" />

That should (if it works) create log files in a LogFiles subfolder in the user's Documents folder, with the actual log file names corresponding to the date. I don't know, but I would guess that NLog will create the LogFiles folder the first time anything is logged.

Good luck!

like image 103
wageoghe Avatar answered Mar 06 '23 04:03

wageoghe


For me it worked like this:

fileName="${specialfolder:folder=ApplicationData}/newDMS ClientApplication/Logs/${shortdate}.log"
like image 42
tudor.iliescu Avatar answered Mar 06 '23 04:03

tudor.iliescu