using log4net.Appender; using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace CVE.LogViewer { class DataGridViewAppender : AppenderSkeleton { private DataGridView _dataGridView; public DataGridView AppenderControl { get { return _dataGridView; } set { _dataGridView = value; } } public string FormName { get; set; } public string ListViewName { get; set; } /// /// 查找控件 /// /// /// /// private Control FindControlRecursive(Control root, string ListViewName) { if (root.Name == ListViewName) return root; foreach (Control c in root.Controls) { Control t = FindControlRecursive(c, ListViewName); if (t != null) return t; } return null; } /// /// 显示日志 /// /// protected override void Append(log4net.Core.LoggingEvent loggingEvent) { //验证控件是否存在 if (_dataGridView == null) { if (String.IsNullOrEmpty(FormName) || String.IsNullOrEmpty(ListViewName)) return; Form form = Application.OpenForms[FormName]; if (form == null) return; _dataGridView = (DataGridView)FindControlRecursive(form, ListViewName); _dataGridView.Invoke(new Action(() => { _dataGridView.Rows.Clear(); DataGridViewTextBoxColumn logInfo = new DataGridViewTextBoxColumn(); logInfo.Name = "Log Info"; logInfo.DataPropertyName = "Log Info"; logInfo.HeaderText = "Log Info"; _dataGridView.Columns.Add(logInfo); _dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; //设置自动换行 _dataGridView.DefaultCellStyle.WrapMode = DataGridViewTriState.True; //设置自动调整高度 _dataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; _dataGridView.RowHeadersVisible = false; _dataGridView.ColumnHeadersVisible = false; _dataGridView.AllowDrop = false; _dataGridView.AllowUserToAddRows = false; _dataGridView.AllowUserToDeleteRows = false; _dataGridView.AllowUserToOrderColumns = false; _dataGridView.AllowUserToResizeColumns = false; _dataGridView.AllowUserToResizeRows = false; _dataGridView.MultiSelect = false; _dataGridView.ReadOnly = true; _dataGridView.CellBorderStyle = DataGridViewCellBorderStyle.None; if (_dataGridView == null) return; form.FormClosing += (s, e) => _dataGridView = null; })); } if (!_dataGridView.IsHandleCreated) { return; } if (_dataGridView.IsDisposed) { return; } //显示日志 string str = String.Format("{0} {1}", loggingEvent.TimeStamp.ToString("yyyy/MM/dd HH:mm:ss.fff"), RenderLoggingEvent(loggingEvent)); string level = loggingEvent.Level.ToString(); _dataGridView.BeginInvoke((MethodInvoker)delegate { //日志行数控制 if (_dataGridView.Rows.Count > 500) { //大于200行清空显示 _dataGridView.Rows.Clear(); } DataGridViewRow row = new DataGridViewRow(); DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell(); cell.Value = str; //根据等级控制显示颜色 switch (level) { case "DEBUG": cell.Style.ForeColor = Color.Blue; //DarkBlue; break; case "INFO": cell.Style.ForeColor = Color.Black; //ForestGreen; break; case "WARN": cell.Style.ForeColor = Color.Orange; break; case "ERROR": cell.Style.ForeColor = Color.MediumVioletRed; break; case "FATAL": cell.Style.ForeColor = Color.Red; break; } row.Cells.Add(cell); //控制每次显示最新行 _dataGridView.Rows.Add(row); _dataGridView.FirstDisplayedScrollingRowIndex = _dataGridView.Rows.Count - 1; _dataGridView.ClearSelection(); }); } } }