Creating folder for log files for NLog with InnoSetup
Asked Answered
S

0

3

I'm having an issue where I need my users to be able to run my application without administrator rights, but at the same time, I need NLog to be able to create it's log files, which it needs administrator rights to create them in the same folder the application is installed in.

I'm trying to create a directory under the application directory, named Logs, and give everyone-modify permissions with inno setup. I'm going to set up my NLog config to write to this new Logs folder instead of the application directory, so even when the application is run by non-administrators, NLog has sufficient privileges to create the logs.

My question is, is this the proper way to do this? I'm not very experienced with NLog or InnoSetup, so I'm not sure if there is something I'm missing, or if this will possibly create security problems?

I have my NLog config section set up like so

NLog

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <target name="el" xsi:type="EventLog" log="ASI" layout="${message} ${exception:innerFormat=tostring:maxInnerExceptionLevel=10:format=tostring}" />
    <target name="fl" xsi:type="File" fileName="Logs\iDocClientLog.log" layout="${date}: ${message} ${exception:innerFormat=tostring:maxInnerExceptionLevel=10:format=tostring}" />
  </targets>
  <rules>
    <logger name="*" minlevel="Debug" writeTo="el,fl" />
  </rules>
</nlog>

And my InnoSetup installer script Dirs section is like this

InnoSetup

[Dirs]
Name: "{app}\Logs"; Permissions: everyone-modify
Sp answered 10/7, 2013 at 20:23 Comment(4)
I don't know how to configure NLog, but for the InnoSetup part I can tell you that {app} is not a good idea to use since it's by default targeted to the Program Files for which you need to have admin right for writing to. I would suggest you to use e.g. {userappdata}.Snapdragon
@Snapdragon Thanks for your advice. I was just trying to look up whether creating the Logs folder would require the installer to be run as administrator, and it seems like you are saying it would since I'm trying to create a folder in the Program Files directory. I was thinking of changing the fileName attribute on my target to use the C:\Temp folder, but I'm not sure if I would run into the same problems I'm having now, since it doesn't seem like NLog would have permissions to create files in that directory.Sp
That should be fine (if it's possible to make such NLog configuration; I have absolutely no experience with it), although the Application Data folder sounds to me better fitting for your needs.Snapdragon
Seconded on Application Data. One of the things you always need to consider with writable data is: what if two users are running your application at the same time? It should be obvious that there will be problems if the logs get stored in the same folder. Anything that an application writes should always be kept in the proper per-user folders only.Hooper

© 2022 - 2024 — McMap. All rights reserved.