private static void HistoryMonitor() { while (true) { int interval = 60000; int time = interval; while (time <= HISTORY_TIME) { Thread.Sleep(interval); time += interval; } Thread.Sleep(1000); InitService.WriteLogToDB("Monitor", "WT", "开始保存:time=" + time.ToString()); InsertHistoryData(); } }
public static void RegisterService(TServiceStatus status, string baseURL, string msg) { InitService.ReadConfig(); }
private static void InsertRealTimeData() { string clientId = ""; string exCode = ""; lock (_realServiceCollection) { try { using (SqlConnection conn = new SqlConnection(CONNECTSTR)) { conn.Open(); string sqlCmd = "DELETE stb_Services_Real WHERE ServiceID=@ServiceID;"; SqlCommand command = new SqlCommand(sqlCmd, conn); command.Parameters.AddWithValue("@ServiceID", ServiceID); command.ExecuteNonQuery(); sqlCmd = "INSERT INTO stb_Services_Real " + "SELECT @ServiceID, @ClientID, @TrueExCode, @Success, " + "@Failure, @Reject, @Exception, @LastCallTime"; command = new SqlCommand(sqlCmd, conn); command.Parameters.Add("@ServiceID", SqlDbType.VarChar, 32); command.Parameters.Add("@ClientID", SqlDbType.VarChar, 50); command.Parameters.Add("@TrueExCode", SqlDbType.VarChar, 20); command.Parameters.Add("@Success", SqlDbType.Int); command.Parameters.Add("@Failure", SqlDbType.Int); command.Parameters.Add("@Reject", SqlDbType.Int); command.Parameters.Add("@Exception", SqlDbType.Int); command.Parameters.Add("@LastCallTime", SqlDbType.DateTime2); command.Parameters["@ServiceID"].Value = ServiceID; foreach (KeyValuePair <string, Dictionary <string, TEntityService> > clients in _realServiceCollection) { clientId = clients.Key; command.Parameters["@ClientID"].Value = clientId; foreach (KeyValuePair <string, TEntityService> items in clients.Value) { exCode = items.Key; command.Parameters["@TrueExCode"].Value = exCode; command.Parameters["@Success"].Value = items.Value.Success; command.Parameters["@Failure"].Value = items.Value.Failed; command.Parameters["@Reject"].Value = items.Value.Reject; command.Parameters["@Exception"].Value = items.Value.Except; command.Parameters["@LastCallTime"].Value = items.Value.LastCallDate; command.ExecuteNonQuery(); } } conn.Close(); _realServiceCollection.Clear(); } } catch (Exception error) { InitService.WriteLogToDB(clientId, exCode, "保存实时日志失败:" + error.Message); } } }
private static void InsertHistoryData() { string clientId = ""; string exCode = ""; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); DateTime now = DateTime.Now; lock (_historyServiceCollection) { try { using (SqlConnection conn = new SqlConnection(CONNECTSTR)) { conn.Open(); string insertSql = "INSERT INTO stb_Services_His VALUES(@ServiceID, " + "@ClientID, @TrueExCode, @YearID, @MonthOfYear, " + "@WhichDay, @WhichHour, @Success, @Failure, @Exception, @Reject)"; SqlCommand command = new SqlCommand(insertSql, conn); command.Parameters.Add("@ServiceID", SqlDbType.VarChar, 32); command.Parameters.Add("@ClientID", SqlDbType.VarChar, 50); command.Parameters.Add("@TrueExCode", SqlDbType.VarChar, 20); command.Parameters.Add("@YearID", SqlDbType.Int); command.Parameters.Add("@MonthOfYear", SqlDbType.Int); command.Parameters.Add("@WhichDay", SqlDbType.Int); command.Parameters.Add("@WhichHour", SqlDbType.Int); command.Parameters.Add("@Success", SqlDbType.Int); command.Parameters.Add("@Failure", SqlDbType.Int); command.Parameters.Add("@Reject", SqlDbType.Int); command.Parameters.Add("@Exception", SqlDbType.Int); command.Parameters["@ServiceID"].Value = ServiceID; foreach (KeyValuePair <string, Dictionary <string, TEntityService> > clients in _historyServiceCollection) { clientId = clients.Key; command.Parameters["@ClientID"].Value = clientId; foreach (var items in clients.Value) { exCode = items.Key; command.Parameters["@TrueExCode"].Value = exCode; command.Parameters["@YearID"].Value = now.Year; command.Parameters["@MonthOfYear"].Value = now.Month; command.Parameters["@WhichDay"].Value = now.Day; command.Parameters["@WhichHour"].Value = now.Hour; command.Parameters["@Success"].Value = items.Value.Success; command.Parameters["@Failure"].Value = items.Value.Failed; command.Parameters["@Reject"].Value = items.Value.Reject; command.Parameters["@Exception"].Value = items.Value.Except; command.ExecuteNonQuery(); } } conn.Close(); stopwatch.Stop(); TimeSpan timespan = stopwatch.Elapsed; InitService.WriteLogToDB(clientId, exCode, "间隔" + (HISTORY_TIME / 1000 / 60).ToString() + "分钟保存成功,耗时:" + timespan.TotalMilliseconds.ToString()); _historyServiceCollection.Clear(); } } catch (Exception ex) { stopwatch.Stop(); InitService.WriteLogToDB(clientId, exCode, "间隔" + (HISTORY_TIME / 1000 / 60).ToString() + "分钟保存日志失败:" + ex.Message); } } }