using log4net; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LogServer { /// /// Logger 工厂 /// class LoggerFactory { private static Dictionary dicLog = new Dictionary(); /// /// 创建实例 ,默认读取App_Data 下的log4net.config文件 /// /// /// public static ILog CreateInstance(string loggerName) { return CreateInstance(loggerName, null); } /// /// 创建实例 /// /// /// log4net.config文件路径 /// public static ILog CreateInstance(string loggerName, string configFilename) { if (!dicLog.ContainsKey(loggerName)) { if (string.IsNullOrWhiteSpace(configFilename)) { configFilename = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).FullName + "\\App_Data\\configs\\log4net.config"; } log4net.Config.XmlConfigurator.Configure(new FileInfo(configFilename)); //从配置文件中读取Logger对象 //WebLogger 里面的配置信息是用来将日志录入到数据库的 //做为扩展 做判断来确定日志的记录形式,数据库也好,txt文档也好,控制台程序也好。 var log = log4net.LogManager.GetLogger(loggerName); //防止并发出错 if (!dicLog.ContainsKey(loggerName)) { dicLog.Add(loggerName, log); } return log; } else { return dicLog[loggerName]; } } } }