/// <summary> /// 记录异常日志 /// </summary> /// <param name="log">异常日志</param> /// <returns>日志Id</returns> public Guid Write(ExceptionLog log) { //01.构造sql语句 StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.Append( "INSERT INTO ExceptionLogs (\"Id\", \"Namespace\", \"ClassName\", \"MethodName\", \"MethodType\", \"ArgsJson\", \"ExceptionType\", \"ExceptionMessage\", \"ExceptionInfo\", \"InnerException\", \"OccurredTime\", \"IPAddress\") VALUES (uuid_generate_v4(), @Namespace, @ClassName, @MethodName, @MethodType, @ArgsJson, @ExceptionType, @ExceptionMessage, @ExceptionInfo, @InnerException, @OccurredTime, @IPAddress) "); sqlBuilder.Append("RETURNING \"Id\"; "); //02.初始化参数 IDbDataParameter[] parameters = { new NpgsqlParameter("@Namespace", log.Namespace.ToDbValue()), new NpgsqlParameter("@ClassName", log.ClassName.ToDbValue()), new NpgsqlParameter("@MethodName", log.MethodName.ToDbValue()), new NpgsqlParameter("@MethodType", log.MethodType.ToDbValue()), new NpgsqlParameter("@ArgsJson", log.ArgsJson.ToDbValue()), new NpgsqlParameter("@ExceptionType", log.ExceptionType.ToDbValue()), new NpgsqlParameter("@ExceptionMessage", log.ExceptionMessage.ToDbValue()), new NpgsqlParameter("@ExceptionInfo", log.ExceptionInfo.ToDbValue()), new NpgsqlParameter("@InnerException", log.InnerException.ToDbValue()), new NpgsqlParameter("@OccurredTime", log.OccurredTime.ToDbValue()), new NpgsqlParameter("@IPAddress", log.IPAddress.ToDbValue()) }; //03.执行sql object result = _SqlHelper.ExecuteScalar(sqlBuilder.ToString(), parameters); Guid newId = Guid.Parse(result.ToString()); return(newId); }
/// <summary> /// 创建序列种子 /// </summary> /// <param name="serialSeed">序列种子</param> public void Create(SerialSeed serialSeed) { StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.Append("INSERT INTO SerialSeeds "); sqlBuilder.Append(" (\"Id\", \"Name\", \"Prefix\", \"Timestamp\", \"SerialLength\", \"TodayCount\", \"Description\") "); sqlBuilder.Append("VALUES "); sqlBuilder.Append(" (uuid_generate_v4(), @Name, @Prefix, @Timestamp, @SerialLength, @TodayCount, @Description) "); sqlBuilder.Append("RETURNING \"Id\"; "); IDbDataParameter[] parameters = { new NpgsqlParameter("@Name", serialSeed.Name.ToDbValue()), new NpgsqlParameter("@Prefix", serialSeed.Prefix.ToDbValue()), new NpgsqlParameter("@Timestamp", serialSeed.Timestamp.ToDbValue()), new NpgsqlParameter("@SerialLength", serialSeed.SerialLength.ToDbValue()), new NpgsqlParameter("@TodayCount", serialSeed.TodayCount.ToDbValue()), new NpgsqlParameter("@Description", serialSeed.Description.ToDbValue()) }; object result = _SqlHelper.ExecuteScalar(sqlBuilder.ToString(), parameters); Guid serialSeedId = Guid.Parse(result.ToString()); serialSeed.Id = serialSeedId; }
/// <summary> /// 获取异常日志记录条数 /// </summary> /// <param name="logId">日志Id</param> /// <param name="startTime">开始时间</param> /// <param name="endTime">结束时间</param> /// <returns>异常日志记录条数</returns> public int Count(Guid?logId, DateTime?startTime, DateTime?endTime) { string sql = "SELECT COUNT(*) FROM ExceptionLogs WHERE 0 = 0"; #region # 条件过滤 if (logId.HasValue) { sql = $"{sql} AND \"Id\" = '{logId.Value}'"; } if (startTime.HasValue) { sql = $"{sql} AND \"OccurredTime\" >= '{startTime.Value}'"; } if (endTime.HasValue) { sql = $"{sql} AND \"OccurredTime\" <= '{endTime.Value}'"; } #endregion object result = _SqlHelper.ExecuteScalar(sql); int count = Convert.ToInt32(result); return(count); }