跳轉到內容

.NET 開發基金會/配置

來自 Wikibooks,開放世界中的開放書籍


配置、診斷、管理和安裝


配置、診斷、管理和安裝

[編輯 | 編輯原始碼]

考試目標: 將配置、診斷、管理和安裝功能嵌入 .NET Framework 應用程式

配置管理

[編輯 | 編輯原始碼]

配置管理在此處以受限的意義使用。它指的是將應用程式適配到特定的執行環境或使用者。這通常透過使用配置文 件來完成,這些檔案指定應用程式的執行時引數。這種配置資訊的典型示例是用於定位和連線到應用程式資料庫的連線字串。考試目標都與應用程式與其配置檔案的實際互動有關。

配置檔案本身的管理或設計是一個廣闊的主題,考試目標沒有涉及,因此我們不會在本學習指南中介紹它。

.NET 框架安裝程式

[編輯 | 編輯原始碼]

事件日誌

[編輯 | 編輯原始碼]

MSDN 的定義是“Windows 事件日誌允許您的應用程式和元件記錄有關重要事件的資訊。您可以使用這些記錄來審計對系統 的訪問,解決問題以及重新建立使用模式。”

有關一般性討論,請參見 MSDN

有關 EventLog 類的詳細資訊以及有關其使用的一些注意事項,請參見 MSDN

效能監視

[編輯 | 編輯原始碼]

除錯和跟蹤

[編輯 | 編輯原始碼]

管理資訊和事件

[編輯 | 編輯原始碼]

Windows 管理規範 - MSDN



Clipboard

待辦事項
描述 WMI 在 .NET 中的整合


類、介面和工具

[編輯 | 編輯原始碼]

嵌入配置管理

[編輯 | 編輯原始碼]

考試目標: 將配置管理功能嵌入 .NET Framework 應用程式。

(參考 System.Configuration 名稱空間)

Configuration 類和 ConfigurationManager 類

Configuration 類 - MSDN
ConfigurationManager 類 - MSDN

ConfigurationSettings 類、ConfigurationElement 類、ConfigurationElementCollection 類和 ConfigurationElementProperty 類

ConfigurationSettings 類 - MSDN
ConfigurationElement 類 - MSDN
ConfigurationElementCollection 類 - MSDN
ConfigurationElementProperty 類 - MSDN

實現 IConfigurationSectionHandler 介面 - MSDN

ConfigurationSection 類、ConfigurationSectionCollection 類、ConfigurationSectionGroup 類和 ConfigurationSectionGroupCollection 類

ConfigurationSection 類 - MSDN
ConfigurationSectionCollection 類 - MSDN
ConfigurationSectionGroup 類 - MSDN
ConfigurationSectionGroupCollection - MSDN

實現 ISettingsProviderService 介面 - MSDN

實現 IApplicationSettingsProvider 介面 - MSDN

ConfigurationValidationBase 類 - MSDN

在 MSDN 上沒有直接的結果 - 待核實

實現 IConfigurationSystem 介面 - MSDN

建立自定義安裝程式並配置應用程式

[編輯 | 編輯原始碼]

考試目標: 使用 System.Configuration.Install 名稱空間建立 .NET Framework 元件的自定義 Microsoft Windows 安裝程式,並使用配置檔案、環境變數和 .NET Framework 配置工具 (Mscorcfg.msc) 配置 .NET Framework 應用程式。

有關本節中討論的過程的“食譜”,請參見 MSDN 以及相應的“操作方法”部分。

Installer 類 - MSDN

配置 .NET Framework 應用程式應使用哪個執行時版本 - MSDN

配置執行時應在何處搜尋程式集 - MSDN

配置程式集的位置以及要使用的程式集版本 - MSDNMSDN

指示執行時在搜尋程式集時使用 DEVPATH 環境變數 - MSDN

AssemblyInstaller 類 - MSDN

ComponentInstaller 類 - MSDN

使用 .NET Framework 配置工具 (Mscorcfg.msc) 配置 .NET Framework 應用程式 - MSDN

ManagedInstaller 類 - MSDN

InstallContext 類 - MSDN

InstallerCollection 類 - MSDN

實現 IManagedInstaller 介面 - MSDN

InstallEventHandler 委託 - MSDN

配置併發垃圾回收 - MSDN

使用配置檔案註冊遠端物件 - MSDN

管理事件日誌

[edit | edit source]

考試目標: 使用 System.Diagnostics 名稱空間管理事件日誌

EventLog 類 - MSDN

EventSourceCreationData 類 - MSDN

寫入事件日誌
[edit | edit source]

MSDN

從事件日誌讀取
[edit | edit source]

MSDN

建立新的事件日誌
[edit | edit source]

透過建立第一個寫入該日誌的事件源來建立 EventLog。

最簡單的兩種方法是

  • 使用 EventLog.CreateEventSource 方法
  • 建立 EventLog 例項,指定源,然後寫入日誌。實際建立發生在第一次寫入執行時。

請注意,System.Diagnostics 名稱空間中沒有“EventSource”類,即使在登錄檔中建立了表示該源的物件。

C# EventLog 建立示例

   using System;
   using System.Collections.Generic;
   using System.Text;
   using System.Diagnostics;
   namespace EventLogLab1
   {
       class Program
       {
           static void Main(string[] args)
           {
               try
               {
                   EventLog log1 = new EventLog("EvtLab2Log");
                   log1.Source = "EvtLab2Source";
                   // Actual creation happens next
                   log1.WriteEntry("Example message", EventLogEntryType.Information,
                       123, 1);
               }
               catch (Exception e)
               {
                   Console.WriteLine(e.Message);
               }
               Console.WriteLine("Press ENTER to finish");
               Console.ReadLine();
           }
       }
   }

推薦的方法,在培訓工具包中似乎沒有介紹(因此可能不在考試中),是在應用程式安裝期間使用 EventLogInstaller 類。有關參考,請參閱 MSDN

管理程序和監控效能

[edit | edit source]

考試目標: 使用 .NET Framework 2.0 的診斷功能管理系統程序並監控 .NET Framework 應用程式的效能。

(參考 System.Diagnostics 名稱空間)

獲取所有正在執行的程序的列表。

Process 類 - MSDN
有關 GetCurrentProcess()、GetProcessesByName()、GetProcesses() 和 GetProcessById() 的示例程式碼,請參閱 MSDN

檢索有關當前程序的資訊 - MSDN

獲取程序載入的所有模組的列表

Process.Modules 屬性返回 ProcessModule 物件的強型別集合,這些物件表示 Process 當前載入的模組。
有關 Process.Modules 屬性,請參閱 MSDN
有關 ProcessModule 類,請參閱 MSDN

PerformanceCounter 類、PerformanceCounterCategory 類和 CounterCreationData 類

PerformanceCounter 類 - MSDN
PerformanceCounterCategory - MSDN
CounterCreationData 類 - MSDN

使用和不使用命令列引數啟動程序

啟動程序概述
程序使用 Process.Start() 的一個過載方法啟動。當將敏感資料(如密碼)傳遞給 Process.Start() 時,請使用接受 SecureString 作為引數型別的兩個過載 Start() 方法之一。
外部連結

StackTrace 類 - MSDN

StackFrame 類 - MSDN

除錯和跟蹤

[edit | edit source]

考試目標: 使用 System.Diagnostics 名稱空間除錯和跟蹤 .NET Framework 應用程式。

Debug 類和 Debugger 類

Debug 類 - MSDN
Debug 類的四個靜態寫入方法 - WriteWriteLineWriteIfWriteLineIf - 允許您寫入除錯訊息以檢查程式的流程並捕獲錯誤。在程式的發行版本中,對這些方法的呼叫會被忽略(有關詳細資訊,請參閱下面的“ConditionalAttribute 特性”)。
在 Visual Studio 中,Debug 寫入方法的預設目標是輸出視窗。您可以使用 Debug 的 Listeners 屬性訪問關聯的 TraceListenerCollection。以下程式碼顯示瞭如何使用集合的 Remove 和 Add 方法來控制除錯訊息傳送到哪裡。如果您新增多個相同型別的偵聽器,關聯的目標會多次接收文字。
C# 程式碼示例

Debug 類示例

   using System;
   using System.Diagnostics;
   using System.IO;
   using System.Reflection;
   class Program
   {
       static void Main(string[] args)
       {
           Debug.WriteLine("This is (by default) printed in the Output window.");
           //remove default listener
           Debug.Listeners.RemoveAt(0);
           //add a listener that can write to the Console window
           Debug.Listeners.Add(new ConsoleTraceListener());
           Debug.WriteLine("This is printed in the console window.");
           //add a default listener again
           Debug.Listeners.Add(new DefaultTraceListener());
           Debug.WriteLine("This is printed in both the Output and the Console window.");
           //remove all listeners
           Debug.Listeners.Clear();
           //add a listener that writes to a file
           Debug.Listeners.Add(new TextWriterTraceListener(File.Create("C:\\test.txt")));
           Debug.WriteLine("This is only printed to the newly created file.");
           //here we need to flush the output buffer
           Debug.Flush();
           //keep console window open in debug mode
           Console.ReadLine();
       }
   }
Debugger 類 - MSDN

Trace 類 - MSDN

CorrelationManager 類 - MSDN

TraceListener 類 - MSDN

TraceSource 類 - MSDN

TraceSwitch 類 - MSDN

XmlWriterTraceListener 類 - MSDN

DelimitedListTraceListener 類 - MSDN

EventlogTraceListener 類 - MSDN

偵錯程式特性 - MSDN

DebuggerBrowsableAttribute 類 - MSDN
DebuggerDisplayAttribute 類 - MSDN
DebuggerHiddenAttribute 類 - MSDN
DebuggerNonUserCodeAttribute 類 - MSDN
DebuggerStepperBoundaryAttribute 類 - MSDN
DebuggerStepThroughAttribute 類 - MSDN
DebuggerTypeProxyAttribute 類 - MSDN
DebuggerVisualizerAttribute 類 - MSDN

嵌入管理資訊

[編輯 | 編輯原始碼]

考試目標:將管理資訊和事件嵌入到 .NET Framework 應用程式中。

(參考 System.Management 名稱空間 - MSDN)

使用 ManagementObjectSearcher 類及其派生類檢索管理物件集合

System.Management 名稱空間中的類允許您使用 Windows 管理規範 (WMI) 來管理計算機系統。該名稱空間中最值得注意的類是 ManagementObjectSearcher,您可以使用它根據 WMI 查詢檢索管理物件。
可以透過這種方式檢索的資訊範圍從計算機機箱型別(筆記型電腦、桌上型電腦...)到處理器和硬碟詳細資訊,以及有關正在執行的服務和程序的資訊。有關所有 WMI 類及其屬性的概述,請參閱 MSDN 上的 WMI 參考
在下面的示例中,定義了一個粗略且不乾淨的方法,該方法列印給定 WMI 類中所有管理資訊物件的屬性。然後使用它列印有關當前計算機系統的資訊。
C# 示例程式碼

WMI 基本示例

   class Program
   {
       static void Main(string[] args)
       {
           //Print all management info in the WMI class Win32_ComputerSystem
           PrintManagementInfo("Win32_ComputerSystem");
           //wait for user input to keep console window up in debug mode
           Console.ReadLine();
       }
       static void PrintManagementInfo(string WMIClassName)
       {
           ManagementObjectSearcher mos;
           //Get all managementobjects of the specified class
           mos = new ManagementObjectSearcher("SELECT * FROM " + WMIClassName);
           foreach (ManagementObject MOCollection in mos.Get())
           {
               foreach (PropertyData p in MOCollection.Properties)
               {
                   //Some properties are arrays,
                   //in which case the code below only prints
                   //the type of the array.
                   //Add a check with IsArray() and a loop
                   //to display the individual array values.
                   Console.WriteLine("{0}: {1}", p.Name, p.Value);
               }
           }
       }
   }
ManagementObjectSearcher 類 - MSDN
列舉計算機上的所有磁碟驅動器、網路介面卡和程序
以下程式碼使用 ManagementObjectSearcher 建構函式 的一個過載來列出系統上的所有物理和邏輯磁碟驅動器、網路介面卡和正在執行的程序。
C# 示例程式碼

ManagementObjectSearcher 示例

   class Program
   {
       static void Main(string[] args)
       {
           //
           Console.WriteLine("Physical disks: ");
           PrintManagementInfoProperty("Win32_DiskDrive", "Name");
           Console.WriteLine("*****************************");
           //
           Console.WriteLine("Logical disks: ");
           PrintManagementInfoProperty("Win32_LogicalDisk", "Name");
           Console.WriteLine("*****************************");
           //
           Console.WriteLine("Network adapters: ");
           PrintManagementInfoProperty("Win32_NetworkAdapter", "Name");
           Console.WriteLine("*****************************");
           //  
           Console.WriteLine("Processes: ");
           PrintManagementInfoProperty("Win32_Process", "Name");
           Console.WriteLine("*****************************");
           //  
           //wait for user input to keep console window up in debug mode
           Console.ReadLine();
       }
       static void PrintManagementInfoProperty(string WMIClassName, string WMIPropertyName)
       {
           ManagementObjectSearcher mos;
           //Get the specified property for all objects of the specified class
           mos = new ManagementObjectSearcher("SELECT " + WMIPropertyName + " FROM " + WMIClassName);
           foreach (ManagementObject mo in mos.Get())
           {
               PropertyData p = mo.Properties[WMIPropertyName]; 
               Console.WriteLine("{0}", p.Value);
           }
       }
   }
檢索有關所有網路連線的資訊
將提供示例
檢索有關所有暫停的服務的資訊
以下程式碼使用 ManagementObjectSearcher 物件檢索所有正在執行的服務,然後顯示其名稱。
C# 示例程式碼

列舉服務示例

   class Program
   {
       static void Main(string[] args)
       {
           Console.WriteLine("Running services: ");
           //form the query, providing a WMI class name and a condition
           SelectQuery query = new SelectQuery("Win32_Service", "State='Running'");
           //find matching management objects
           ManagementObjectSearcher mos = new ManagementObjectSearcher(query);
           //
           foreach (ManagementObject mo in mos.Get())
           {
               Console.WriteLine(mo.Properties["Name"].Value);
           }
           //                        
           //wait for user input to keep console window up in debug mode
           Console.ReadLine();
       }
   }

ManagementQuery 類 - MSDN

EventQuery 類 - MSDN

ObjectQuery 類 - MSDN

使用 ManagementEventWatcher 類訂閱管理事件 - MSDN


上一個 / 下一個

華夏公益教科書