private static void ChangeLog4netLogFileName(string fileName) { try { log4net.Core.LogImpl logImpl = ext_log as log4net.Core.LogImpl; if (logImpl != null) { log4net.Appender.AppenderCollection ac = ((log4net.Repository.Hierarchy.Logger)logImpl.Logger).Appenders; for (int i = 0; i < ac.Count; i++) { // 这里我只对RollingFileAppender类型做修改 log4net.Appender.RollingFileAppender rfa = ac[i] as log4net.Appender.RollingFileAppender; if (rfa != null) { fileName = AppDomain.CurrentDomain.BaseDirectory + "\\log\\" + DateTime.Now.ToString("yyyyMM\\\\dd") + "\\" + fileName + ".log"; if (rfa.File != null) { if (rfa.File == fileName) { continue; } } rfa.File = fileName; // 更新Writer属性 rfa.Writer = new System.IO.StreamWriter(rfa.File, rfa.AppendToFile, rfa.Encoding); } } } } catch { } ext_log = log4net.LogManager.GetLogger("ext"); }
private static void ChangeLog4netLogFileName(log4net.ILog iLog, string fileName) { log4net.Core.LogImpl logImpl = iLog as log4net.Core.LogImpl; if (logImpl != null) { log4net.Appender.AppenderCollection ac = ((log4net.Repository.Hierarchy.Logger)logImpl.Logger).Appenders; for (int i = 0; i < ac.Count; i++) { log4net.Appender.RollingFileAppender rfa = ac[i] as log4net.Appender.RollingFileAppender; if (rfa != null) { rfa.File = fileName; if (!System.IO.File.Exists(fileName)) { System.IO.File.Create(fileName); } rfa.Writer = new System.IO.StreamWriter(rfa.File, rfa.AppendToFile, rfa.Encoding); } } } }