先贴上代码,参考网上资料的,忘记是谁的了,先谢谢他。
不过尝试看下Log4net的命名空间就会发现,太棒了,跟MyMetal一样,用起来非常方便。
修改文件路径的代码:
Code
public static void ChangeRollingFileName(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++)
{ //这里我只对RollingFileAppender类型做修改
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);
}
//更新Writer属性
rfa.Writer = new System.IO.StreamWriter(rfa.File, rfa.AppendToFile, rfa.Encoding);
}
}
}
} 修改配置文件的代码:
log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(Server .MapPath ( "~/Config/log4net.config")));