/// <summary> /// 添加报警信息 /// </summary> /// <param name="info"></param> public void AddInfo(string info) { try { g_Num++; string time = DateTime.Now.ToLongTimeString();//时间 AlarmInfo alarmInfo = new AlarmInfo(); alarmInfo.No = g_Num; alarmInfo.Alarm = info; alarmInfo.Time = time; g_AlarmInfo_L.Add(alarmInfo); if (g_AlarmInfo_L.Count > 50) { g_AlarmInfo_L.RemoveRange(0, g_AlarmInfo_L.Count - 50); } //记录日志 new Task(new Action(() => { WriteTxt(alarmInfo); })).Start(); } catch (Exception ex) { Log.L_I.WriteError(NameClass, ex); } }
//写入文本 public void WriteTxt(AlarmInfo alarmInfo) { try { TxtFile t_I = new TxtFile(); //文件 string strFileLog = PathAlarmHour + "Alarm.txt"; string value = "Alarm:" + alarmInfo.Alarm + " " + "Time:" + alarmInfo.Time; t_I.WriteText(strFileLog, value); } catch (Exception ex) { Log.L_I.WriteError(NameClass, ex); } }
//刷新产品参数 void RefreshDgAlarm() { try { //重新建立list防止主界面闪退 List <AlarmInfo> alarmInfo_L = new List <AlarmInfo>(); for (int i = 0; i < FunLogAlarm.F_I.g_AlarmInfo_L.Count; i++) { AlarmInfo alarmInfo = new AlarmInfo(); alarmInfo.No = FunLogAlarm.F_I.g_AlarmInfo_L[i].No; if (FunLogAlarm.F_I.g_AlarmInfo_L[i].Alarm != null) { alarmInfo.Alarm = FunLogAlarm.F_I.g_AlarmInfo_L[i].Alarm; } else { alarmInfo.Alarm = "Null"; } if (FunLogAlarm.F_I.g_AlarmInfo_L[i].Time != null) { alarmInfo.Time = FunLogAlarm.F_I.g_AlarmInfo_L[i].Time; } else { alarmInfo.Time = "Null"; } if (alarmInfo != null) { alarmInfo_L.Add(alarmInfo); } } dgAlarm.ItemsSource = alarmInfo_L; dgAlarm.Items.Refresh(); if (FunLogAlarm.F_I.g_AlarmInfo_L.Count > 0) { dgAlarm.ScrollIntoView(alarmInfo_L[alarmInfo_L.Count - 1], dgAlarm.Columns[0]); } } catch (Exception ex) { Log.L_I.WriteError(NameClass, ex); } }
/// <summary> /// 写入本地日志 /// </summary> public void WriteLog(AlarmInfo alarmInfo) { try { //文件 string strFileLog = PathAlarmHour + "Alarm.xml"; XmlDocument xDoc = DealXml.D_I.LoadXmlStream(strFileLog, PathTemplate); XmlElement xeRoot = DealXml.D_I.ReadNode(xDoc, "Alarm"); XmlElement xeAlarm = xDoc.CreateElement("Alarm"); DealXml.D_I.WriteAttribute(xeAlarm, "Value", alarmInfo.Alarm); DealXml.D_I.WriteAttribute(xeAlarm, "Time", alarmInfo.Time); xeRoot.AppendChild(xeAlarm); xDoc.Save(strFileLog); } catch (Exception ex) { Log.L_I.WriteError(NameClass, ex); } }