4

I have an assembly that uses log4net for logging.
I have this in the Assemblynfo.cs file:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

The log4net.config file looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
    <layout type="log4net.Layout.PatternLayout">
      <!--          <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" /> -->
      <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
    </layout>
  </appender>

  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="${APPDATA}\\DC Lasersystem\\DCMark\\DCMarkLog.txt" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <datePattern value=" yyyy-MM-dd" />
    <maxSizeRollBackups value="10" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
    </layout>
  </appender>

  <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
    </layout>
  </appender>

  <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
    <layout type="log4net.Layout.PatternLayout">
      <!--          <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" /> -->
      <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
    </layout>
  </appender>

  <root>
    <level value="DEBUG" />
    <appender-ref ref="RollingLogFileAppender" />
  </root>
</log4net>

When I run my application from the IDE it works. The DCMarkLog.txt file is created in %APPDATA%\DC Lasersystem\DCMark directory.

BUT When I install the application and run it from Program Files directory then I don't get a log file!

I have the same files in the install directory as in the Release directory.

I'm guessing it's some kind of permission that isn't right...

Anyone that has a clue?

// Anders

Philipp M
  • 1,877
  • 7
  • 27
  • 38
Andis59
  • 559
  • 7
  • 25

1 Answers1

4

This is a permission issue. In Windows 7 you require administrator rights to write to the Program Files directory. You will have to either:

  • Run your application as Administrator
  • Or the preferred way write your log file to a folder where user has write access to.

To find the correct location to write to refer to: Where to store Application Data in Windows 7 and Vista

Community
  • 1
  • 1
Philip Fourie
  • 111,587
  • 10
  • 63
  • 83
  • But I thought that I did this! I have specified in log4net.config that the logfile is "${APPDATA}\DC Lasersystem\DCMark\DCMarkLog.txt". and in this directory the program should have permission to write, or? – Andis59 Feb 21 '12 at 12:03
  • Ok, I change from APPDATA to ALLUSERSPROFILE and then it worked! Don't know why, but ... – Andis59 Feb 21 '12 at 14:23