using System; using System.Collections.Generic; using System.Text; using log4net; using System.IO; using System.ComponentModel; using System.Drawing; using System.Diagnostics; using System.Threading; using System.Windows.Forms; using log4net.Repository; using log4net.Appender; namespace CommonLib.LOG { public class LogView : DataGridView { public LogView() { this.InitializeComponent(); //if (!this.IsInDesignMode()) if (!this.DesignMode) { base.Columns.Clear(); base.Columns.Add("No", "No"); base.Columns.Add("Thread", "Thread"); base.Columns.Add("Time", "Time"); base.Columns.Add("Message", "Message"); base.Columns[0].Width = 75; base.Columns[1].Width = 75; base.Columns[2].Width = 100; base.Columns[3].Width = 1000; base.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; base.HorizontalScrollBar.Visible = true; base.VerticalScrollBar.Visible = true; this.Interval = 500; } base.ReadOnly = true; base.SelectionMode = DataGridViewSelectionMode.FullRowSelect; } public LogCollection Logs { get { return this.m_Logs; } set { this.m_Logs = value; if (this.m_Logs != null) { this.PreviousElementCount = this.m_Logs.ElementCount; this.viewTimer.Start(); return; } this.viewTimer.Stop(); } } public int Level { get { return this.m_Level; } set { this.m_Level = value; } } public bool AutoScroll { get { return this.m_AutoScroll; } set { this.m_AutoScroll = value; } } public int Interval { get { return this.viewTimer.Interval; } set { this.viewTimer.Interval = value; } } private bool IsInDesignMode() { bool result; using (Process process = Process.GetCurrentProcess()) { if (LicenseManager.UsageMode == LicenseUsageMode.Designtime) { result = true; } else if (process.ProcessName.ToUpper().Equals("DEVENV")) { result = true; } else { result = false; } } return result; } public void EnsureVisible(bool redraw) { try { int count = base.DisplayedRowCount(false); int index = base.RowCount - count; if (index < 0) { index = 0; } if (base.FirstDisplayedScrollingRowIndex >= 0) { base.FirstDisplayedScrollingRowIndex = index; } if (redraw) { base.Invalidate(); } } catch (Exception ex) { LogStorage.Logs.Add(LogLevel.Error, "LogView: {0}", ex.Message); } } private void viewTimer_Tick(object sender, EventArgs e) { if (this.m_Logs == null) { return; } base.RowCount = this.m_Logs.Count((LogLevel)this.Level); if (this.AutoScroll) { uint element_count = this.m_Logs.ElementCount; if (this.PreviousElementCount != element_count) { this.EnsureVisible(true); this.PreviousElementCount = element_count; } } } private void LogView_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e) { LogElement log = this.m_Logs.Fetch(this.Level, e.RowIndex); if (log == null) { return; } switch (e.ColumnIndex) { case 0: e.Value = log.Number.ToString(); break; case 1: e.Value = log.ThreadID.ToString("X8"); break; case 2: e.Value = log.TimeStamp.ToString("HH:mm:ss.fff"); break; case 3: e.Value = log.Message; break; } if (this.Level != 0 && log.Level == 0) {//´íÎó base[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Red; return; } if (this.Level != 1 && log.Level ==(LogLevel) 1) {//¾¯¸æ base[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Pink; return; } if (this.Level != (int)LogLevel.Error1 && log.Level == LogLevel.Error1) { base[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Yellow; return; } if (this.Level != (int) LogLevel.Error2 && log.Level == LogLevel.Error2) { base[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Orange; return; } base[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.White; } protected override void Dispose(bool disposing) { if (disposing && this.components != null) { this.components.Dispose(); } base.Dispose(disposing); } private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.viewTimer = new System.Windows.Forms.Timer(this.components); ((System.ComponentModel.ISupportInitialize)(this)).BeginInit(); this.SuspendLayout(); // // viewTimer // this.viewTimer.Enabled = true; this.viewTimer.Interval = 200; this.viewTimer.Tick += new System.EventHandler(this.viewTimer_Tick); // // LogView // //this.DoubleBuffered = true; this.AllowUserToAddRows = false; this.AllowUserToDeleteRows = false; this.AllowUserToResizeRows = false; this.ReadOnly = true; this.RowHeadersVisible = false; this.RowTemplate.Height = 21; this.Size = new System.Drawing.Size(499, 351); this.VirtualMode = true; this.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.LogView_CellValueNeeded); ((System.ComponentModel.ISupportInitialize)(this)).EndInit(); this.ResumeLayout(false); } private LogCollection m_Logs; // Token: 0x040000E8 RID: 232 private uint PreviousElementCount; private int m_Level; private bool m_AutoScroll; private IContainer components =null; private System.Windows.Forms.Timer viewTimer; } }