#眉標=Enterprise Library #副標=設計模型套件系列(6) #大標=日誌記錄專家─Logging Application Block (上) #作者=文/王寧疆 ==程式1 =========== LogEntry logEntry = new LogEntry(); //建立LogEntry類別的物件 logEntry.EventId = 100; //設定事件資訊的ID logEntry.Priority = 2; //設定事件資訊的優先順序 logEntry.Message = "Informational message"; //設定事件的詳細資訊 logEntry.Categories.Add("General"); //設定事件資訊的種類 Logger.Write(logEntry); //將準備好的事件資訊寫入到指定的媒體 ================ ==程式2 =========== Dictionary dictionary = new Dictionary();//建立存放額外資訊的Dictionary集合 ManagedSecurityContextInformationProvider informationHelper = new ManagedSecurityContextInformationProvider();//建立ManagedSecurityContextInformationProvider //類別的物件 informationHelper.PopulateDictionary(dictionary); //透過ManagedSecurityContextInformationProvider將額外 //的資訊填入dictionary集合 ================ ==程式3 =========== LogEntry logEntry = new LogEntry(); //建立LogEntry類別的物件 logEntry.EventId = 100; //設定事件資訊的ID logEntry.Priority = 2; //設定事件資訊的優先順序 logEntry.Message = "Informational message"; //設定事件的詳細資訊 logEntry.Categories.Add("General"); //設定事件資訊的種類 int width = Screen.PrimaryScreen.Bounds.Width; //取得螢幕的寬度 int height = Screen.PrimaryScreen.Bounds.Height; //取得螢幕的高度 string resolution = String.Format("{0}x{1}", width, height); //將螢幕的高度和寬度資訊準備成字串 dictionary.Add("Screen resolution", resolution); //將準備好的字串填入到dictionary集合中 logEntry.ExtendedProperties = dictionary;//將dictionary集合指定給LogEntry物件的ExtendedProperties屬性 Logger.Write(logEntry); //將準備好的事件資訊寫入到指定的媒體 ================ ==程式4 =========== using (new Tracer("UI Events")) { //執行欲計算效能的工作 } ================    ==程式5 =========== LogEntry logEntry = new LogEntry(); //建立LogEntry類別的物件 logEntry.Priority = 2; //設定LogEntry類別的物件的優先順序 logEntry.Message = "Some Message"; //設定欲記錄的資訊內容 logEntry.Categories.Add("General"); //設定事件資訊的種類 if (Logger.ShouldLog(logEntry)) //判斷是否要執行記錄資訊的動作 { Logger.Write(logEntry); //執行記錄資訊的動作 } ================ ==程式6 =========== protected void btnDataSource_Click(object sender, EventArgs e) //利用SqlDataSource控制項查詢資料庫記錄 { using (new Tracer("Trace")) //利用名稱為Trace的設定記錄程式執行的效能 { GridView1.DataSource = SqlDataSource1; //令GridView控制項繫結到SqlDataSource控制項 GridView1.DataBind(); //命令GridView控制項顯示SqlDataSource控制項查詢得到的記錄 } } protected void btnADONET_Click(object sender, EventArgs e) //利用ADO.NET提供的類別查詢資料庫記錄 { using (new Tracer("Trace")) //利用名稱為Trace的設定記錄程式執行的效能 { //取得設定檔中記載的資料庫連線資訊 string strConn = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString; SqlDataAdapter da = new SqlDataAdapter("Select * from Orders", strConn); //建立SqlDataAdapter類別的物件 DataSet ds = new DataSet(); //建立DataSet類別的物件 da.Fill(ds); /利用SqlDataAdapter查詢資料庫,並將查詢的結果填入到DataSet類別的物件中 GridView1.DataSource = ds.Tables[0]; //將DataSet中的第一個資料表當做GridView控制項的資料來源 GridView1.DataBind(); //命令GridView控制項顯示ADO.NET類別查詢得到的記錄 } } protected void btnDAAB_Click(object sender, EventArgs e) //利用Microsoft Data Access Application Block控制項查詢資料庫記錄 { using (new Tracer("Trace")) //利用名稱為Trace的設定記錄程式執行的效能 { //取得設定檔中記載的資料庫連線資訊 string strConn = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString; string strSQL = "Select * from Orders"; //負責查詢資料庫記錄的SQL敘述 SqlDatabase db = new SqlDatabase(strConn); //建立SqlDatabase類別的物件 DbCommand cmd = db.GetSqlStringCommand(strSQL); //建立DbCommand類別的物件 DataSet ds = db.ExecuteDataSet(cmd);//執行查詢,並將查詢得到的記錄存放在DataSet類別的物件中 GridView1.DataSource= ds.Tables[0]; //將DataSet中的第一個資料表當做GridView控制項的資料來源 GridView1.DataBind(); //命令GridView控制項顯示ADO.NET查詢得到的記錄 } } ================ ==Trace.log =========== ---------------------------------------- Timestamp: 2006/11/5 上午 06:58:08 Message: Start Trace: Activity 'c0edfc9c-faec-4178-8f83-10927f1679cc' in method 'btnDAAB_Click' at 49761144810 ticks Category: Trace Priority: 5 EventId: 1 Severity: Start Title:TracerEnter Machine: CRAIGNB Application Domain: d668ff7-1-128071834717991312 Process Id: 1456 Process Name: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\WebDev.WebServer.EXE Win32 Thread Id: 328 Thread Name: Extended Properties: ---------------------------------------- ---------------------------------------- Timestamp: 2006/11/5 上午 06:58:14 Message: End Trace: Activity 'c0edfc9c-faec-4178-8f83-10927f1679cc' in method 'btnDAAB_Click' at 49784126565 ticks (elapsed time: 6.421 seconds) Category: Trace Priority: 5 EventId: 1 Severity: Stop Title:TracerExit Machine: CRAIGNB Application Domain: d668ff7-1-128071834717991312 Process Id: 1456 Process Name: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\WebDev.WebServer.EXE Win32 Thread Id: 328 Thread Name: Extended Properties: ================