多个程序使用同一个 log4net 配置,
AssemblyInfo.cs文件加上:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]初始化log4net:
log4net.GlobalContext.Properties["program"] = "#your program name#"; log4net.Config.XmlConfigurator.Configure();调用
protected static readonly ILog SystemLogger = LogManager.GetLogger("SystemLog"); protected static readonly ILog TransferLogger = LogManager.GetLogger("TransferLog");
配置文件 log4net.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <!--log4net config--> <log4net> <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file value=".\Logs\" /> <!--日志路径--> <rollingStyle value="Date" /><!--日志滚动模式--> <datePattern value="_yyyy-MM-dd".log"" /> <!--日志文件名格式--> <staticLogFileName value="false" /> <!--日志文件名是否固定不变的--> <appendToFile value="true" /> <!--是否向文件中追加日志--> <maximumFileSize value="64MB" /> <!--日志文件最大大小--> <maxSizeRollBackups value="10" /> <!--日志保留天数--> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="[