1

I can't seem to find a definitive answer/example on how I can log to a file in appdata

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

Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
empo
  • 1,133
  • 5
  • 21
  • 41
  • possible duplicate of [Log4Net.How can I change the file location programmatically c#?](http://stackoverflow.com/questions/1535736/log4net-how-can-i-change-the-file-location-programmatically-c) – Anthony Mastrean Jun 16 '11 at 03:57

2 Answers2

1

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

Community
  • 1
  • 1
Peter Lillevold
  • 33,668
  • 7
  • 97
  • 131
-1

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
  • 28,883
  • 9
  • 61
  • 81
  • There are plenty of acceptable (idiomatic, even) ways to do this with log4net configuration. log4net is just not built for runtime modification. – Anthony Mastrean Jun 16 '11 at 03:58