public void HandleOnLogEvent(Object sender, LogEventArgs args) { if (m_writer == null) return; String logText = args.Timestamp.ToString("") + " | " + args.Severity + " | " + args.Caller + "::line " + args.Line + " | " + args.Message + Environment.NewLine; m_writer.Write(logText); // Bad for perfomance but necessary in case of a crash. m_writer.Flush(); }
public void HandleOnLogEvent(object _sender, LogEventArgs args) { if (InvokeRequired) { BeginInvoke(new EventHandler<LogEventArgs>(HandleOnLogEvent), new object[] { _sender, args }); return; } // We might be called after the UI has been disposed. Let the // event go. if (this.IsDisposed || this.Disposing) return; // Determine whether we want to scroll. bool scrollFlag = (this.WindowState != FormWindowState.Minimized && lvMessages.Items.Count != 0 && lvMessages.ClientRectangle.Contains( lvMessages.Items[lvMessages.Items.Count - 1].SubItems[1].Bounds)); // Add the event. AddEvent(args, scrollFlag); }
/// <summary> /// Handle a request to log an event. This method is private to /// standardize the layout of the stack frames. /// </summary> private static void privLog(int severity, String msg, bool isLoggingException) { // Return early if we are not logging. if (m_loggingLevel == LoggingLevel.None) return; // Generate the log event. String callerStr = "Unknown"; String lineStr = "?"; String callStackStr = ""; if (m_loggingLevel == LoggingLevel.Debug) { FormatStackFrame(new StackFrame(2, true), out callerStr, out lineStr); StackTrace st = new StackTrace(true); for (int i = 2; i < st.FrameCount; i++) { String name, line; FormatStackFrame(st.GetFrame(i), out name, out line); callStackStr += name + " at line " + line + Environment.NewLine; } } LogEventArgs evt = new LogEventArgs(severity, callerStr, callStackStr, lineStr, msg, DateTime.Now.ToLocalTime()); // For ordering and consistency purpose the following has to be // done in mutual exclusion. lock (Mutex) { // Overflow, remove some events. if (m_evtList.Count >= m_maxNbEvt) m_evtList.RemoveRange(0, m_evtList.Count / 2); // Add the event to the list. m_evtList.Add(evt); #if DEBUG Debug.WriteLine(evt.Timestamp.ToString("") + " | " + evt.Severity + " | " + evt.Caller + "::line " + evt.Line + " | " + evt.Message); #endif // Dispatch the event to the listeners. if (m_onLogEvent != null && !m_firingFlag) { try { m_firingFlag = true; m_onLogEvent(null, evt); m_firingFlag = false; } catch (Exception ex) { Base.HandleError(ex.Message, true); } } } }
/// <summary> /// Adds a message to the listview. If scrollToBotton /// is set to true, calls EnsureVisible so that the last /// item is scrolled to. /// </summary> private void AddEvent(LogEventArgs evt, bool scrollToBottom) { // Add this message to listview ListViewItem itm = new ListViewItem(evt.Severity.ToString()); itm.Name = lvMessages.Columns[0].Name; itm.Tag = evt.CallStack; switch (evt.Severity) { case 1: itm.BackColor = Color.Yellow; break; case 2: itm.BackColor = Color.Red; break; default: itm.BackColor = Color.White; break; } ListViewItem.ListViewSubItem sub; sub = new ListViewItem.ListViewSubItem(); sub.Name = lvMessages.Columns[1].Name; sub.Text = evt.Timestamp.ToString("hh:mm:ss.fff"); itm.SubItems.Add(sub); sub = new ListViewItem.ListViewSubItem(); sub.Name = lvMessages.Columns[2].Name; sub.Text = evt.Caller; itm.SubItems.Add(sub); sub = new ListViewItem.ListViewSubItem(); sub.Name = lvMessages.Columns[3].Name; sub.Text = evt.Line; itm.SubItems.Add(sub); sub = new ListViewItem.ListViewSubItem(); sub.Name = lvMessages.Columns[4].Name; sub.Text = evt.Message; itm.SubItems.Add(sub); lvMessages.Items.Add(itm); if (scrollToBottom) CorrectUI(false); }