trace配置文件

来源:互联网 发布:nginx 代理静态资源 编辑:程序博客网 时间:2024/06/11 05:06

XML Config Files

XML config files are new to.NET, and many .NET developers still haven't yet learned of their existence. Have you ever seen an application with an.INI file? Applications often need to save the user's settings from one run of the program to the next, and for years, the method of choice was to write those settings to a text.INI file (INI for INItialization). But after Windows 95 came out, Microsoft encouraged developers to store those user settings in the Windows registry rather than in.INI files. Now, though, we've come full circle. Each application in .NET automatically looks for an XML file called <yourExecutableName>.exe.config. If no such file exists, then the application will still run anyway; but if the file does exist, then .NET will read it to determine some configuration options.

You could store arbitrary information about your program in the config file, and your application can use the APIs in the System.Configuration namespace to read (but not write) those values. But the main thing that interests us when debugging is the ability to specify our TraceSwitches and TraceListeners in the config file. Modifying the config file allows us to change the logging behavior without recompiling our code. Let's look at a typical.config file:

<configuration>   <runtime>      ...   </runtime>   <system.diagnostics>     <trace autoflush="true" indentsize="2">        <listeners>           <add name="myListener"             type="System.Diagnostics.TextWriterTraceListener"             initializeData="c://MyListener.log"/>        </listeners>     </trace>     <switches>        <add name="MySwitch" value="4" />        <add name="YourSwitch" value="3" />     </switches>   </system.diagnostics></configuration>

Don't worry about writing your own config file. Visual Studio .NET will automatically output one for you when you create a new project, so you just need to add the extra details. Advanced developers may be interested in learning about the values inside the <runtime> area, but for now, we're only concerned about the values in <system.diagnostics>, because that's how we configure our switches and listeners. These values won't be created by VS .NET; we'll have to manually add them.

The <listeners> section is straightforward. Remember several pages ago when we set a listener on the Trace class with Trace.Listeners.Add? Now that we've specified a listener in the config file, there's no need to do it in the code anymore. Instead, we can use the config file to set the name of the listener (which doesn't really matter in most cases) and the type (for instance, TextWriterTraceListener or EventLogTraceListener). We can also do any necessary initialization of the listener (in the preceding example, we set the name of the file where we want to write our log data). And that's it! Now any messages written with Trace.Write will be sent to the file we specified. Want your log data to go to the Windows event logs instead? Just change this one line in your config file and you're done.

The <switches> section is only slightly more complicated. In the preceding example, I've added two switches—one for me, and one for you—and the value attribute corresponds to the TraceSwitch level we discussed before. There's one small tricky bit: Instead of referring to the level by the symbolic names (TraceLevel.Off, TraceLevel.Verbose, etc.), we have to use the numbers 0 through 4. This is mildly annoying, but it's not too great of a hardship.

In order to use a switch defined in the config file, we need to first define a switch with the same name in the code. But we still set the level in the config file. So the code looks like this:

using System.Diagnostics;class SwitchExample {    public static void Main() {        TraceSwitch ts = new TraceSwitch("switchName", "description");        //// ts.Level = TraceLevel.Verbose; //DON'T set this line in the code!       Trace.WriteIf(ts.TraceInfo, "Whatever");    }}

Meanwhile the config file would contain these lines:

<switches>        <add name=" switchName" value="3" /></switches>
 
原创粉丝点击