How can I log to a file in Common Application Data (appdata) using log4net?
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();
}
}
精彩评论