Exemple #1
0
        private void btnGenerateSysEvent_Click(object sender, EventArgs e)
        {
            try
            {
                // 模拟产生一个系统日志;系统将显示此日志并进行持久化。
                var log = new SysEventLog(EventType.CommRecovery, EventLevel.First, "节点 A 与节点 B 通信恢复。");
                GlobalMessageBus.PublishNewSystemEventGenerated(new NewSystemEventArgs(log));

                // 模拟产生一个系统日志;系统将显示此日志并进行持久化。
                log = new SysEventLog(EventType.CommInterruption, EventLevel.Third, "节点 A 与节点 B 通信中断。");
                GlobalMessageBus.PublishNewSystemEventGenerated(new NewSystemEventArgs(log));

                // 模拟产生一个通信中断消息;系统将显示此消息并自动产生一个系统日志;然后持久化。
                var args = new CommStateChangedEventArgs(false, NodeType.Node1, 16, NodeType.Node2, 18);
                GlobalMessageBus.PublishCommStateChanged(args);

                // 查询
                log = GlobalServices.Repository.Where <SysEventLog>(p => p.Code == log.Code).FirstOrDefault();
                LogUtility.Info("{0}", log);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void OnCommStateChanged(object sender, CommStateChangedEventArgs args)
        {
            try
            {
                var remoteNode = GlobalServices.Repository.Where <SystemNode>(p => p.Code == args.RemoteNodeCode).FirstOrDefault();

                //  产生事件。
                var eventLog = new SysEventLog();
                eventLog.TypeCode = args.CommState.GetValueOrDefault() ? EventType.CommRecovery : EventType.CommInterruption;
                eventLog.Level    = GlobalServices.SEM.GetEventLevel(eventLog.TypeCode);

                var nodeName = remoteNode == null?string.Format("{0}", args.RemoteNodeCode) : string.Format("{0}_{1}", args.RemoteNodeCode, remoteNode.Name);

                if (args.CommState.GetValueOrDefault())
                {
                    eventLog.Description = string.Format("本节点与远程节点({0})通信恢复。", nodeName);
                }
                else
                {
                    eventLog.Description = string.Format("本节点与远程节点({0})通信中断。", nodeName);
                }

                // 发布事件
                GlobalMessageBus.PublishNewSystemEventGenerated(new NewSystemEventArgs(eventLog));
            }
            catch (System.Exception ex)
            {
                LogUtility.Error(ex);
            }
        }
        private void ShowSysEventLog(SysEventLog log)
        {
            try
            {
                // 清除无效记录
                if (dataGridView.Rows.Count > MaxCount)
                {
                    dataGridView.Rows.RemoveAt(dataGridView.Rows.Count - 1);
                }

                // 查找对应的行。
                var theRow = this.FindGridRow(log.Code);
                if (theRow == null)
                {
                    // 创建新行。
                    theRow = new DataGridViewRow()
                    {
                        Tag = log
                    };
                    theRow.CreateCells(this.dataGridView, new object[] { "" });

                    // 插入到第一行
                    dataGridView.Rows.Insert(0, theRow);
                }

                // 更新内容。
                int index = 0;
                if (log.Level == EventLevel.First)
                {
                    theRow.Cells[index++].Value = Resources.Info;
                    //newRow.DefaultCellStyle.ForeColor = Color.Green;
                }
                else if (log.Level == EventLevel.Second)
                {
                    theRow.Cells[index++].Value = Resources.Warning;
                }
                else
                {
                    theRow.Cells[index++].Value = Resources.Error;
                }
                theRow.Cells[index++].Value = log.Timestamp.ToString("yyyy-MM-dd HH:mm:ss.fff");
                theRow.Cells[index++].Value = EnumUtility.GetDescription(log.TypeCode);
                theRow.Cells[index++].Value = log.Description;

                var confirmEnabled = GlobalServices.SEM.GetConfirmEnabled(log.TypeCode);
                if (confirmEnabled)
                {
                    theRow.Cells[index++].Value = log.ConfirmTime != DateTime.MinValue ? log.ConfirmTime.ToString("yyyy-MM-dd HH:mm:ss.fff") : "";
                }
                else
                {
                    theRow.Cells[index++].Value = "无需确认";
                }
                theRow.Cells[ConfirmTimeIndex].Tag = confirmEnabled;
            }
            catch (Exception ex)
            {
                LogUtility.Error(ex);
            }
        }
        public void AddSysEventLog(SysEventLog log)
        {
            if (log.Code == Entity.InvalidCode)
            {
                log.Code = GlobalServices.Repository.NextSequence <SysEventLog>();
            }

            GlobalServices.Repository.Insert(log);
        }
 public void UpdateSysEventLog(SysEventLog log)
 {
     GlobalServices.Repository.Update <SysEventLog>(log, p => p.Code == log.Code);
 }
 /// <summary>
 /// 构造函数。
 /// </summary>
 /// <param name="value">表示系统事件对象。</param>
 public NewSystemEventArgs(SysEventLog value)
 {
     this.Value = value;
 }