C# - Changing log4Net logging path at runtime

less than 1 minute read

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
        private static void InitLoggingPath()
        {
            string progDataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
            string dataPath = progDataPath + Resource.DATA_FILES_PATH;
            if (!Directory.Exists(dataPath))
                Directory.CreateDirectory(dataPath);
            if (Directory.Exists(dataPath))
            {
                string configPath = Path.Combine(dataPath, "Test.txt");
                ILoggerRepository repository = LogManager.GetRepository();
                IAppender[] appenders = repository.GetAppenders();
                var rollingAppender = appenders.Where(p => p is RollingFileAppender).ToList();
                foreach (IAppender appender in rollingAppender)
                {
                    RollingFileAppender fileAppender = appender as RollingFileAppender;
                    fileAppender.File = configPath;
                    fileAppender.ActivateOptions();
                }
            }
        }