0

I have a DLL project in my solution responsible for logging events in my other projects. Right now I'm facing the problem on where to save the log file. I would like to save it in user app data (c:\users\\AppData\Local\.....)

How can I access that location in log4net.config file?

I have this in my dll project:

 static Log()
        {
            log4net.GlobalContext.Properties["LogFileName"] = @"d:\fileRelease2"; //log file path
#if DEBUG
            log4net.GlobalContext.Properties["LogFileName"] = @"d:\fileDebug2"; //log file path
#endif

            FileInfo configFileInfo = new FileInfo("log4net.config");

            log4net.Config.XmlConfigurator.ConfigureAndWatch(configFileInfo);

            log = LogManager.GetLogger(typeof(Log));
        }

Thanks in advance.

Regards,

===========EDIT 2==========

I've managed to put my log4net.config like this:

<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="DEBUG" />
        <levelMax value="OFF" />
      </filter>

      <param name="AppendToFile" value="true"/>
      <file value="${AppData}\AppName\Logs\log" />
      <staticLogFileName value="false"/>
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <datePattern value=" yyyy-MM-dd&quot;.txt&quot;"/>

      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger: %message%newline"/>
      </layout>     
    </appender>

The problem I'm facing now is that in the IDE (VS2013 ultimate) the folder is created. When I install the application (using a visual studio project installer) the folder/file is not created.

If I start the application with admin privileges it creates the folder/file...

Any ideas why?

sexta13
  • 1,558
  • 1
  • 11
  • 19
  • Sounds like you may need a custom action in your installer to create the folder where the log files are to be saved? I haven't created installers with VS before so cannot help you there I'm afraid. – JT_ Oct 30 '14 at 19:14

2 Answers2

0

Try using

 log4net.GlobalContext.Properties["LogFileName"] = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "fileRelease2");
d.moncada
  • 16,900
  • 5
  • 53
  • 82
0

I think the log4net.Util.PatternString is what you need. An excellent answer explaining how to use this has already been posted on Stackoverflow: How can I change the file location programmatically?.

Community
  • 1
  • 1
JT_
  • 503
  • 5
  • 9