WMIThere has been some confusion with regard to how the Log4Net project relates to Windows Management Instrumentation (WMI). The short answer is that Log4Net and WMI have very little in common. Log4Net is a .NET application logging framework. WMI is a COM-based view into the internals of the Windows platform. WMI is used to monitor Windows system specifics like network card information, as shown in Listing 8.11. Listing 8.11. A WMI QuerySELECT * FROM Win32_NetworkAdapterConfiguration Microsoft even uses WMI in some of its own monitoring utilities. Microsoft married a nice COM interface to a familiar SQL-like query language so that all languages could use WMI. The first thing to note is that WMI is, of course, Windows-specific. You can still get useful logging information from System.NET and System.Environment. Different operating systems have different ways of obtaining this information. As far as I can tell, there is no CLI implementation other than Microsoft's .NET that has a System.Management managed portal into operating system-specific information. The reason this chapter addresses WMI is to clear up some confusion about Log4Net and WMI and also to demonstrate a useful way to use MDC logging.Listing 8.12 is an example of using System.Management to obtain very specific information about the system that may be used in logging. Listing 8.12. Using WMI with MDCmanageClass = new ManagementClass("Win32_Processor"); manageCollection = manageClass.GetInstances(); foreach(ManagementObject mo in manageCollection) { foreach(PropertyData s in mo.Properties) { if(s.Name == "MaxClockSpeed") { log4net.MDC.Set("clock", mo [s.Name].ToString() + " MHz"); } } } Next, add the following to your config file to use the custom variable:<layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c [MaxClock=%X{clock}] ![]() </layout> This can add very useful information to your log if you happen to be running on a CLI implementation that supports it. |