private void InitMainBusinessLogger(string appenderName, ComonLog.LogLevel logLevel) { var logForSpecialAppender = (Logger)LogManager.GetLogger(appenderName).Logger; if (logForSpecialAppender.Appenders.Count > 0) { return; //避免重复生成日志文件 } var logFileName = "Log/" + GetLogFileName(appenderName); var appender = CreateAppender(appenderName, logFileName, logLevel); logForSpecialAppender.AddAppender(appender); }
//LoggerPoolName/AllLog__Level__Date.txt //LoggerPoolName/SellerAccount__Level__Date.txt private string GetAppenderName(string user, ComonLog.LogLevel logLevel) { var appenderName = fileAppender.GetAppenderName(user, logLevel); lock (lockObject) { var tuple = Tuple.Create(user, logLevel); if (!hashLogFile.Contains(tuple)) { InitMainBusinessLogger(appenderName, logLevel); hashLogFile.Add(tuple); } } return(appenderName); }
public ComonLog.ILog GetLogger(string user, ComonLog.LogLevel logLevel = ComonLog.LogLevel.Info) { var appenderName = GetAppenderName(user, logLevel); return(ComonLog.LogManager.GetLogger(appenderName)); }
private RollingFileAppender CreateAppender(string appenderName, string fileName, ComonLog.LogLevel logLevel) { PatternLayout layout = new PatternLayout(); layout.ConversionPattern = "%d %-5p %m%n"; layout.ActivateOptions(); RollingFileAppender appender = new RollingFileAppender(); appender.Layout = layout; appender.Name = appenderName; appender.File = fileName; appender.RollingStyle = RollingFileAppender.RollingMode.Composite; //appender.Encoding = Encoding.Unicode; appender.AppendToFile = true; appender.MaximumFileSize = "4MB"; appender.MaxSizeRollBackups = 100; appender.DatePattern = "yyyy-MM-dd"; appender.PreserveLogFileNameExtension = true; appender.StaticLogFileName = false; appender.Threshold = FileAppenderHelper.ConvertLogLevel(logLevel); log4net.Filter.LevelRangeFilter levfilter = new log4net.Filter.LevelRangeFilter(); levfilter.LevelMax = appender.Threshold; levfilter.LevelMin = appender.Threshold; levfilter.ActivateOptions(); appender.AddFilter(levfilter); appender.ActivateOptions(); return(appender); }