tags:

views:

251

answers:

2

I can't seem to find a definitive answer/ example on how I can log to a file in C:\Documents and Settings\All Users\Application Data\CompanyName\ApplicationName\Logs\app.log

I don't want to "hardcode" the path in the app.config and would rather use Environment.SpecialFolder.CommonApplicationData

Can anyone help me with this?

Thanks.

A: 

One way:

log4net.Repository.Hierarchy.Hierarchy hierarchy = (log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetRepository(); 
var appenders = hierarchy.GetAppenders();
  foreach (var appender in appenders)
  {
    FileAppender fileAppender = appender as FileAppender;
    if (fileAppender != null)
    {
      fileAppender.File = Path.Combine(Environment.SpecialFolder.CommonApplicationData, "myLogFile.log");
      fileAppender.ActivateOptions();
    }
  }
RichardOD
+1  A: 

Yes, take a look at my answer to this question. It explains in detail how to configure this path setup in log4net configuration.

Peter Lillevold
Interesting approach.
RichardOD