摘要:[IADP]再探IADP的例外處理–自訂Crash Report
上次跟大家提到了IADP的例外處理 有一種稱之為Crash Report. 透過自訂Crash Report, 開發人員更容易除錯。
首先,我們看一下以下的程式 (底下以C# Windows Form Application 為例)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using com.intel.adp;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
AdpApplication _app;
TestCrashReport testCrashReport;
public Form1()
{
InitializeComponent();
_app = new AdpApplication(AdpConstants.ADP_DEBUG_APPLICATIONID);
testCrashReport = new TestCrashReport();
_app.SetCrashReport(testCrashReport);
}
private void button1_Click(object sender, EventArgs e)
{
testCrashReport.QuantityBeforeUpdate = 2;
testCrashReport.ExpectedQuantity = 1;
testCrashReport.QuantityAfterUpdate = 3;
throw new System.FormatException("test");
}
}
internal class TestCrashReport : DefaultCrashReport
{
private int _quantityBeforeUpdate, _expectedQuantity, _quantityAfterUpdate;
internal int QuantityBeforeUpdate
{
get { return _quantityBeforeUpdate; }
set {_quantityBeforeUpdate = value;}
}
internal int ExpectedQuantity
{
get { return _expectedQuantity; }
set { _expectedQuantity = value; }
}
internal int QuantityAfterUpdate
{
get { return _quantityAfterUpdate; }
set { _quantityAfterUpdate = value; }
}
ADP_CrashReportField field;
public override void PopulateCrashReportFields()
{
this._customField.Clear();
field = new ADP_CrashReportField();
field.name = "Quantity Before Update";
field.value = Convert.ToString(_quantityBeforeUpdate);
this._customField.Add(QuantityFields);
field= new ADP_CrashReportField();
field.name = "Expected Quantity";
field.value = Convert.ToString(_expectedQuantity);
this._customField.Add(QuantityFields);
field = new ADP_CrashReportField();
field.name = "Quantity After Update";
field.value = Convert.ToString(_quantityAfterUpdate);
this._customField.Add(QuantityFields);
}
}
}
在這個程式中有一個TestCrashReport 類別, 它繼承了DefaultCrashReport。
在這個類別中有三個private member、三個對應的屬性以及覆寫PopulateCrashField這個函式。
當我們的程式擲出任何非繼承自AdpException的例外時,便會產生將相關資料丟到我們所指定的Crash Report中。
(在這個例子中,就是TestCrashReport)
PopulateCrashReportFields這個函式,就是讓開發人員放入自訂欄位。
(在這個例子中,就是 Quantity Before Update , Expected Quantity, Quantity After Update)
接下來我們來看一下產出的XML檔案:
<CrashReport>
<WindowsFormsApplication1>
<ApplicationID>11111111-1111-1111-1111-111111111111</ApplicationID>
<ModuleName>Form1.cs</ModuleName>
<LineNumber>29</LineNumber>
<Error>test</Error>
<Category>System.FormatException</Category>
<Runtime>Microsoft .NET Framework</Runtime>
<RuntimeVersion>2.0.50727.5444</RuntimeVersion>
<OS>Win32NT</OS>
<OSVersion>6.1.7601.65536</OSVersion>
<SDKVersion>1.1.1</SDKVersion>
<Timestamp>2011-05-12T01:01:43.000</Timestamp>
<ErrorData>
於 WindowsFormsApplication1.Form1.button1_Click(Object sender, EventArgs e) 於 D:\CSharpProjects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs: 行 29
於 System.Windows.Forms.Control.OnClick(EventArgs e)
於 System.Windows.Forms.Button.OnClick(EventArgs e)
於 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
於 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
於 System.Windows.Forms.Control.WndProc(Message& m)
於 System.Windows.Forms.ButtonBase.WndProc(Message& m)
於 System.Windows.Forms.Button.WndProc(Message& m)
於 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
於 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
於 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
於 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
於 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
於 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
於 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext conte
於 System.Windows.Forms.Application.Run(Form mainForm)
於 WindowsFormsApplication1.Program.Main() 於 D:\CSharpProjects\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs: 行 18
於 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
於 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
於 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
於 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
於 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
於 System.Threading.ThreadHelper.ThreadStart()
</ErrorData>
<CustomFields>
<CustomField name="Quantity Before Update">2</CustomField>
<CustomField name="Expected Quantity">1</CustomField>
<CustomField name="Quantity After Update">3</CustomField>
</CustomFields>
</WindowsFormsApplication1>
</CrashReport>
可以看到 CustomFields 這個XML標籤 底下有三筆資料,而這三筆正是我們在程式中所設定的三個屬性。
當然,未來程式上傳到IADP官方網站,就可以從網頁上看到相關的Crash Report內容。