.NET 开发基金会/配置
| .NET 开发基金会 | |
|---|---|
考试目标: 将配置、诊断、管理和安装功能嵌入 .NET Framework 应用程序
配置管理在此处以受限的意义使用。它指的是将应用程序适配到特定的执行环境或用户。这通常通过使用配置文 件来完成,这些文件指定应用程序的运行时参数。这种配置信息的典型示例是用于定位和连接到应用程序数据库的连接字符串。考试目标都与应用程序与其配置文件的实际交互有关。
配置文件本身的管理或设计是一个广阔的主题,考试目标没有涉及,因此我们不会在本学习指南中介绍它。
MSDN 的定义是“Windows 事件日志允许您的应用程序和组件记录有关重要事件的信息。您可以使用这些记录来审计对系统 的访问,解决问题以及重新创建使用模式。”
有关一般性讨论,请参见 MSDN
有关 EventLog 类的详细信息以及有关其使用的一些注意事项,请参见 MSDN
Windows 管理规范 - MSDN
考试目标: 将配置管理功能嵌入 .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
配置程序集的位置以及要使用的程序集版本 - MSDN 和 MSDN
指示运行时在搜索程序集时使用 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]从事件日志读取
[edit | edit source]创建新的事件日志
[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 类的四个静态写入方法 - Write、WriteLine、WriteIf 和 WriteLineIf - 允许您写入调试消息以检查程序的流程并捕获错误。在程序的发行版本中,对这些方法的调用会被忽略(有关详细信息,请参阅下面的“ConditionalAttribute 特性”)。
 
- 在 Visual Studio 中,Debug 写入方法的默认目标是输出窗口。您可以使用 Debug 的 Listeners 属性访问关联的 TraceListenerCollection。以下代码显示了如何使用集合的 Remove 和 Add 方法来控制调试消息发送到哪里。如果您添加多个相同类型的侦听器,关联的目标会多次接收文本。
 
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 类中所有管理信息对象的属性。然后使用它打印有关当前计算机系统的信息。
 
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 构造函数 的一个重载来列出系统上的所有物理和逻辑磁盘驱动器、网络适配器和正在运行的进程。
 
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 对象检索所有正在运行的服务,然后显示其名称。
 
枚举服务示例
   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

