开发者

How can I log to a file in Common Application Data (appdata) using log4net?

开发者 https://www.devze.com 2022-12-09 08:09 出处:网络
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 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 E开发者_运维问答nvironment.SpecialFolder.CommonApplicationData


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


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();
    }
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消