private static void InitLog4net(string LogName, LogConfigBase config) { Hierarchy hier = LogManager.GetRepository() as Hierarchy; RollingFileAppender fileAppender = new RollingFileAppender(); fileAppender.Name = LogName; fileAppender.File = Path.Combine(config.FilePath, LogName) + @"\"; fileAppender.AppendToFile = true; fileAppender.DatePattern = config.FileName; fileAppender.StaticLogFileName = false; fileAppender.LockingModel = new FileAppender.MinimalLock(); PatternLayout patternLayout = new PatternLayout(); patternLayout.ConversionPattern = config.Pattern; patternLayout.ActivateOptions(); fileAppender.Layout = patternLayout; //选择UTF8编码,确保中文不乱码。 fileAppender.Encoding = Encoding.Default; fileAppender.ActivateOptions(); hier.Root.RemoveAllAppenders(); BasicConfigurator.Configure(fileAppender); }
internal static void Log(LogConfigBase config, string LogName, LogLevel loglevel, string title, object[] obj) { InitLog4net(LogName, config); ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); string msg = FixLog(config, title, obj); _log(msg, log, loglevel); }
/// <summary> /// 处理日志格式 /// </summary> /// <param name="config"></param> /// <param name="title"></param> /// <param name="obj"></param> /// <returns></returns> public static string FixLog(LogConfigBase config, string title, object[] obj) { if (obj == null || obj.Count() == 0) { return(title); } else { return(title + "\r\n" + obj.ToJson()); } }
internal ILogDistination GetDistination(LogConfigBase config) { lock (logDistinationMap) { ILogDistination distination = null; if (!logDistinationMap.ContainsKey(config.Type)) { distination = config.CreateDistination(); logDistinationMap.Add(config.Type, distination); } else { distination = logDistinationMap[config.Type]; } return distination; } }
/// <summary> /// create LogConfig instance /// </summary> /// <param name="fileName">the xml config file name</param> /// <param name="configId">the config id</param> /// <returns>the logconfig instance</returns> public LogConfig(string fileName, string configId) { LogManagement.Instance().Initialize(fileName); LogConfigBase config = LogManagement.Instance().Site.Config.LogConfigs[configId]; if (config == null) { throw new StackException(string.Format(CultureInfo.InvariantCulture, "the config can not be found, id={0}", configId)); } this.realLogConfig = config; }