用户活动记录,遥测(以及全局exception处理程序中的变量)

背景:

我正在处理一个非常古老的应用程序,很少和非常间歇地生成exception。

现行做法:

通常,我们程序员使用Global Exception处理程序处理罕见的未知数,并将这样的事情连接起来:

[STAThread] [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)] private static void Main() { Application.ThreadException += new ThreadExceptionEventHandler(UIThreadException); Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledException); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new OldAppWithLotsOfWierdExceptionsThatUsersAlwaysIgnore()); } private static void UIThreadException(object sender, ThreadExceptionEventArgs t) { //------------------------------- ReportToDevelopers("All the steps & variables you need to repro the problem are: " + ShowMeStepsToReproduceAndDiagnoseProblem(t)); //------------------------------- MessageToUser.Show("It's not you, it's us. This is our fault.\r\n Detailed information about this error has automatically been recorded and we have been notified.Yes, we do look at every error. We even try to fix some of them.") } private static void UnhandledException(object sender, UnhandledExceptionEventArgs e) { //... } 

问题域:

很难从用户那里获得repro步骤,并且由于报告的问题数量不同,我不想再下载(第二次机会例外)WinDBG或CDB故障排除路径。 我想要一些指标,希望最近有一些System.Diagnostic爱。

研究/谅解:

很久以前我读了一本书Debugging Microsoft .NET 2.0 Applications ,它讨论了一个很酷的工具,John Robbins(又名BugSlayer)写的SuperAssert.Net

这个工具唯一的缺点是(为了解决问题)内存转储的大小是巨大的,当然调试它们几乎是一门艺术,因为它是一门科学。

题:

我希望有人可以告诉我一种方法,我可以在这个程序中转储变量,至少是应用程序Exception.StackTrace的最后一步中的变量。

这几天这有可能吗? 它很容易让我将StackTrace映射回用户操作以计算出步骤.. 我只需要变量!

更新

原来是一个有故障的路由器。

开源项目现在在GitHub上: https : //github.com/MeaningOfLights/UserActionLog

我对这个*进行了大量的研究。 最后,我刚刚创建了一个用户所做内容的日志,它只是内存转储大小的一小部分,可靠地为我提供了重现问题的步骤。 它还有另一个好处,了解用户如何使用该应用程序。

*我真的无法在网上找到任何进行此基本用户活动记录的内容。 我发现的一切都是关于AOP,自动UI测试框架或1/2 Gig内存转储。

为了您的方便,这里是善良!


ActionLogger类:

 public class ActionLogger { private Type _frmType; private Form _frm; ///  /// Ctor Lazy way of hooking up all form control events to listen for user actions. ///  /// /// The WinForm, WPF, Xamarin, etc Form. public ActionLogger(Control frm) { _frmType = ((Form)frm).GetType(); _frm = (Form)frm; ActionLoggerSetUp(frm); } ///  /// Ctor Optimal way of hooking up control events to listen for user actions. ///  public ActionLogger(Control[] ctrls) { ActionLoggerSetUp(ctrls); } ///  /// Lazy way of hooking up all form control events to listen for user actions. ///  /// /// The WinForm, WPF, Xamarin, etc Form. public void ActionLoggerSetUp(Control frm) { HookUpEvents(frm); //First hook up this controls' events, then traversely Hook Up its children's foreach (Control ctrl in frm.Controls) { ActionLoggerSetUp(ctrl); //Recursively hook up control events via the *Form's* child->child->etc controls } } ///  /// Optimal way of hooking up control events to listen for user actions. ///  /// The controls on the WinForm, WPF, Xamarin, etc Form. public void ActionLoggerSetUp(Control[] ctrls) { foreach (var ctrl in ctrls) { HookUpEvents(ctrl); } } ///  /// Releases the hooked up events (avoiding memory leaks). ///  public void ActionLoggerTierDown(Control frm) { ReleaseEvents(frm); } ///  /// Hooks up the event(s) needed to debug problems. Feel free to add more Controls like ListView for example subscribe LogAction() to more events. ///  /// The control whose events we're suspicious of causing problems. private void HookUpEvents(Control ctrl) { if (ctrl is Form) { Form frm = ((Form)ctrl); frm.Load += LogAction; frm.FormClosed += LogAction; frm.ResizeBegin += LogAction; frm.ResizeEnd += LogAction; } else if (ctrl is TextBoxBase) { TextBoxBase txt = ((TextBoxBase)ctrl); txt.Enter += LogAction; } else if (ctrl is ListControl) { //ListControl stands for ComboBoxes and ListBoxes. ListControl lst = ((ListControl)ctrl); lst.SelectedValueChanged += LogAction; } else if (ctrl is ButtonBase) { //ButtonBase stands for Buttons, CheckBoxes and RadioButtons. ButtonBase btn = ((ButtonBase)ctrl); btn.Click += LogAction; } else if (ctrl is DateTimePicker) { DateTimePicker dtp = ((DateTimePicker)ctrl); dtp.Enter += LogAction; dtp.ValueChanged += LogAction; } else if (ctrl is DataGridView) { DataGridView dgv = ((DataGridView)ctrl); dgv.RowEnter += LogAction; dgv.CellBeginEdit += LogAction; dgv.CellEndEdit += LogAction; } } ///  /// Releases the hooked up events (avoiding memory leaks). ///  ///  private void ReleaseEvents(Control ctrl) { if (ctrl is Form) { Form frm = ((Form)ctrl); frm.Load -= LogAction; frm.FormClosed -= LogAction; frm.ResizeBegin -= LogAction; frm.ResizeEnd -= LogAction; } else if (ctrl is TextBoxBase) { TextBoxBase txt = ((TextBoxBase)ctrl); txt.Enter -= LogAction; } else if (ctrl is ListControl) { ListControl lst = ((ListControl)ctrl); lst.SelectedValueChanged -= LogAction; } else if (ctrl is DateTimePicker) { DateTimePicker dtp = ((DateTimePicker)ctrl); dtp.Enter -= LogAction; dtp.ValueChanged -= LogAction; } else if (ctrl is ButtonBase) { ButtonBase btn = ((ButtonBase)ctrl); btn.Click -= LogAction; } else if (ctrl is DataGridView) { DataGridView dgv = ((DataGridView)ctrl); dgv.RowEnter -= LogAction; dgv.CellBeginEdit -= LogAction; dgv.CellEndEdit -= LogAction; } } ///  /// Log the Control that made the call and its value ///  ///  ///  public void LogAction(object sender, EventArgs e) { if (!(sender is Form || sender is ButtonBase || sender is DataGridView)) //Tailor this line to suit your needs { //dont log control events if its a Maintenance Form and its not in Edit mode if (_frmType.BaseType.ToString().Contains("frmMaint")) {//This is strictly specific to my project - you will need to rewrite this line and possible the line above too. That's all though... PropertyInfo pi = _frmType.GetProperty("IsEditing"); bool isEditing = (bool)pi.GetValue(_frm, null); if (!isEditing) return; } } StackTrace stackTrace = new StackTrace(); StackFrame[] stackFrames = stackTrace.GetFrames(); var eventType = stackFrames[2].GetMethod().Name;//This depends usually its the 1st Frame but in this particular framework (CSLA) its 2 ActionLog.LogAction(_frm.Name, ((Control)sender).Name, eventType, GetSendingCtrlValue(((Control)sender), eventType)); } private string GetSendingCtrlValue(Control ctrl, string eventType) { if (ctrl is TextBoxBase) { return ((TextBoxBase)ctrl).Text; } //else if (ctrl is CheckBox || ctrl is RadioButton) { // return ((ButtonBase)ctrl).Text; //} else if (ctrl is ListControl) { return ((ListControl)ctrl).Text.ToString(); } else if (ctrl is DateTimePicker) { return ((DateTimePicker)ctrl).Text; } else if (ctrl is DataGridView && eventType == "OnRowEnter") { if (((DataGridView)ctrl).SelectedRows.Count > 0) { return ((DataGridView)ctrl).SelectedRows[0].Cells[0].Value.ToString(); } else { return string.Empty; } } else if (ctrl is DataGridView) { DataGridViewCell cell = (((DataGridView)ctrl).CurrentCell); if (cell == null) return string.Empty; if (cell.Value == null) return string.Empty; return cell.Value.ToString(); } return string.Empty; } } 

ActionLog类:

 public static class ActionLog { const string ACTIONLOGFILEIDENTIFIER = "ActionLog_"; private static int _numberOfDaily = 0; private static int _maxNumerOfLogsInMemory = 512; private static List _TheUserActions = new List(); private static string _actionLoggerDirectory = string.Empty; public static void LogActionSetUp(int maxNumerOfLogsInMemory = 512,string actionLoggerDirectory = "") { if (string.IsNullOrEmpty(actionLoggerDirectory)) actionLoggerDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\Documents\\ProjectNameMgtFolder\\"; if (!Directory.Exists(actionLoggerDirectory)) Directory.CreateDirectory(actionLoggerDirectory); _actionLoggerDirectory = actionLoggerDirectory; LogAction("MDI_Form", "APPLICATION", "STARTUP", string.Empty); } public static void LogAction(string frmName, string ctrlName, string eventName, string value) { if (value.Length > 10) value = value.Substring(0, 10); LogAction(DateTime.Now, frmName,ctrlName, eventName, value); } public static void LogAction(DateTime timeStamp, string frmName, string ctrlName, string eventName, string value) { _TheUserActions.Add(string.Format("{0}\t{1}\t{2}\t{3}\t{4}", timeStamp.ToShortTimeString(), frmName, ctrlName, eventName, value)); if (_TheUserActions.Count > _maxNumerOfLogsInMemory) WriteLogActionsToFile(); } public static string GetLogFileName() { //Check if the current file is > 1 MB and create another string[] existingFileList = System.IO.Directory.GetFiles(_actionLoggerDirectory, ACTIONLOGFILEIDENTIFIER + DateTime.Now.ToString("yyyyMMdd") + "*.log"); string filePath = _actionLoggerDirectory + ACTIONLOGFILEIDENTIFIER + DateTime.Now.ToString("yyyyMMdd") + "-0.log"; if (existingFileList.Count() > 0) { filePath = _actionLoggerDirectory + ACTIONLOGFILEIDENTIFIER + DateTime.Now.ToString("yyyyMMdd") + "-" + (existingFileList.Count() - 1).ToString() + ".log"; FileInfo fi = new FileInfo(filePath); if (fi.Length / 1024 > 1000) //Over a MB (ie > 1000 KBs) { filePath = _actionLoggerDirectory + ACTIONLOGFILEIDENTIFIER + DateTime.Now.ToString("yyyyMMdd") + "-" + existingFileList.Count().ToString() + ".log"; } } return filePath; } public static string[] GetTodaysLogFileNames() { string[] existingFileList = System.IO.Directory.GetFiles(_actionLoggerDirectory, ACTIONLOGFILEIDENTIFIER + DateTime.Now.ToString("yyyyMMdd") + "*.log"); return existingFileList; } public static void WriteLogActionsToFile() { string logFilePath = GetLogFileName(); if (File.Exists(logFilePath)) { File.AppendAllLines(logFilePath,_TheUserActions); } else { File.WriteAllLines(logFilePath,_TheUserActions); } _TheUserActions = new List(); } } 

注意:LogAction方法很可能会触发第二个(例如,对于Button单击,它将在调用Button_Click事件后调用)。 因此,虽然您可能认为需要首先插入这些LogAction事件,例如通过反转事件调用顺序 ,这不是一个好的做法而且不是必需的。 诀窍是在堆栈跟踪中,堆栈中的最后一个调用将告诉您最后一个用户操作 。 “操作日志”告诉您如何在未处理的exception发生之前使程序处于该状态。 一旦达到这一点,您需要遵循StackTrace来使应用程序出错。

将其付诸实践 – 例如MDI表单加载事件:

 UserActionLog.ActionLog.LogActionSetUp(); 

在MDI Forms Close事件中:

 UserActionLog.ActionLog.WriteLogActionsToFile(); 

在子窗体构造函数中:

 _logger = New UserActionLog.ActionLogger(this); 

在儿童表格封闭活动中:

 _logger.ActionLoggerTierDown(this); 

UIThreadExceptionCurrentDomain_UnhandledException事件中调用WriteLogActionsToFile(); 然后将日志附加到通过屏幕截图发送给支持的电子邮件中…


以下是如何通过电子邮件发送支持日志文件的快速示例:

 string _errMsg = new System.Text.StringBuilder(); string _caseNumber = IO.Path.GetRandomFileName.Substring(0, 5).ToUpper(); string _errorType; string _screenshotPath; List _emailAttachments = new List(); string _userName; private static void UIThreadException(object sender, ThreadExceptionEventArgs t) { _errorType = "UI Thread Exception" .... //HTML table containing the Exception details for the body of the support email _errMsg.Append(""); _errMsg.Append(""); _errMsg.Append(""); if (exception != null) { _errMsg.Append(""); if (exception.InnerException != null) _errMsg.Append(""); _errMsg.Append("
User:" & _userName & "
Time:" & _errorDateTime.ToShortTimeString & "
Exception Type:" & _errorType.ToString & "
Message:" & exception.Message.Replace(" at ", " at
") & "
Inner Exception:" & exception.InnerException.Message & "
Stacktrace:" & exception.StackTrace & "
"); } .... //Write out the logs in memory to file UserActionLog.ActionLog.WriteLogActionsToFile(); //Get list of today's log files _emailAttachments.AddRange(UserActionLog.ActionLog.GetTodaysLogFileNames()); //Adding a screenshot of the broken window for support is a good touch //https://stackoverflow.com/a/1163770/495455 _emailAttachments.Add(_screenshotPath); .... Email emailSystem = New Email(); //(using Microsoft.Exchange.WebServices.Data) emailSystem.SendEmail(ConfigMgr.AppSettings.GetSetting("EmailSupport"), "PROJECT_NAME - PROBLEM CASE ID: " & _caseNumber, _errMsg.ToString(), _emailAttachments.ToArray());

发送电子邮件后,向用户显示一个窗口,解释发生的问题,图片很好… StackExchange网站有很好的例子,这是我最喜欢的: https : //serverfault.com/error