/// <summary> /// Log4Net记录日志 /// </summary> /// <param name="logEntity">日志实体信息</param> public void Log(LogEntity logEntity) { if (logEntity == null) return; var logger = log4net.LogManager.GetLogger("index"); switch (logEntity.Level) { case LogLevel.Debug: logger.Debug(logEntity); break; case LogLevel.Warn: logger.Warn(logEntity); break; case LogLevel.Error: logger.Error(logEntity); break; case LogLevel.Fatal: logger.Fatal(logEntity); break; case LogLevel.Info: logger.Info(logEntity); break; default: logger.Error(logEntity); break; } }
/// <summary> /// MongoDB记录日志 /// </summary> /// <param name="logEntity">日志信息实体</param> public void Log(LogEntity logEntity) { if (logEntity == null) return; string collectionName = string.Empty; switch (logEntity.Level) { case LogLevel.Debug: collectionName = ConfigurationManager.AppSettings[TableNameKey_Debug]; break; case LogLevel.Warn: collectionName = ConfigurationManager.AppSettings[TableNameKey_Warn]; break; case LogLevel.Fatal: collectionName = ConfigurationManager.AppSettings[TableNameKey_Fatal]; break; case LogLevel.Info: collectionName = ConfigurationManager.AppSettings[TableNameKey_Info]; break; default: collectionName = ConfigurationManager.AppSettings[TableNameKey_Error]; break; } //mongodb数据库连接字符串 //mongodb://username:[email protected]:27017 string connStr = ConfigurationManager.ConnectionStrings[ConnectionStringKey].ConnectionString; MongoClient client = new MongoClient(connStr); MongoServer servers = client.GetServer(); MongoDatabase db = servers.GetDatabase(ConfigurationManager.AppSettings[DataBaseKey]); MongoCollection<LogEntity> collection = db.GetCollection<LogEntity>(collectionName); collection.Insert(logEntity); }
/// <summary> /// Log4Net记录日志 /// </summary> /// <param name="logEntity">日志实体信息</param> public void Log(LogEntity logEntity) { if (logEntity == null) { return; } var logger = log4net.LogManager.GetLogger("index"); switch (logEntity.Level) { case LogLevel.Debug: logger.Debug(logEntity); break; case LogLevel.Warn: logger.Warn(logEntity); break; case LogLevel.Error: logger.Error(logEntity); break; case LogLevel.Fatal: logger.Fatal(logEntity); break; case LogLevel.Info: logger.Info(logEntity); break; default: logger.Error(logEntity); break; } }
/// <summary> /// 记录程序异常日志 /// </summary> /// <param name="title">日志标题</param> /// <param name="logMsg">日志内容</param> /// <param name="level">日志级别,默认错误信息,LogLevel.Error</param> public static void Log(string title, string logMsg, LogLevel level = LogLevel.Error) { LogEntity logEntity = new LogEntity() { AppName = appName, LogMsg = logMsg, LogTime = DateTime.Now, ProcessName = processName, ServerIp = serverIP, Title = title, Level = level }; if (HttpContext.Current != null) { try { logEntity.ClientIp = Misc.IPAddr; logEntity.FullClientIp = Misc.GetIPAddr(true); logEntity.UrlReferrer = StringUtils.ToString(HttpContext.Current.Request.UrlReferrer); logEntity.RawUrl = HttpContext.Current.Request.RawUrl; } catch { } } logInstance.Log(logEntity); }