ilans
ilans

Reputation: 2727

How to to load environment variable on System level and not on User level in Log4Net

I've struggled in this for a couple of hours with no avail.

I need to use an environment variable called TMP or TEMP, which hold the Windows Temp folder to which I want to log. Usually it's C:\Windows\Temp (of course this value might change from one system to another, that's why I need the variable value).

The problem is there are two values for each of those keys:

enter image description here

I use the following XML to instruct Log4Net (under C#) to load the temp folder (pasted the relevant part):

  <appender name="SomeFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="${TMP}\Logs\SomeLogFileName.log" />
  <appendToFile value="true" />

When I ran my program, it writes the log into the users temp folder: C:\Users\myuser\AppData\Local\Temp\Logs

The question is how do I force Log4Net to take the environment variable of TMP/TEMP from system level and not user level ?

Upvotes: 0

Views: 742

Answers (2)

Klitos Kyriacou
Klitos Kyriacou

Reputation: 11652

If your application is being run by a specific user, you would typically want to write only to user-specific directories. C:\Windows\TEMP is a directory shared among all the users of your PC. If you really want to write to that, typically you would want to run your application as a System user.

(This is just the typical scenario; granted that there may be individual circumstances where you want a specific user to write to a system-wide directory.)

Now if you run as SYSTEM, you would pick up the System-level definitions of TMP/TEMP into your environment.

Upvotes: 1

ilans
ilans

Reputation: 2727

If indeed @JeroenMostert is right, and it ain't possible (without turning to the registry), I used the WINDIR environment variable (which value is C:\Windows) as a work around as I'll show next.

Still, if someone comes up with a better answer I'll be glad.

  <appender name="SomeFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="${WINDIR}\temp\Logs\SomeLogFileName.log" />
  <appendToFile value="true" />

Upvotes: 0

Related Questions