2
Answers

Updating system.diagnostics section in config file

Ask a question
Andy Webster

Andy Webster

12y
4.2k
1
Hi

Can anyone help me with how to update the system.diagnostics section of an application configuration file?

Specifically I want to be able to update the trace switch levels held in the config file from code.

Here is the format of the app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.diagnostics>
  <trace autoflush="true" indentsize="2">
  <listeners>
  <!-- Optional field, trace listener to accept debug messages and detailed exception information -->
  <add name="XML" type="TraceListeners.XmlTraceListener, TraceListeners" />
  </listeners>
  </trace>
  <switches>
  <!-- This switch controls tracing, possible values are:
 0 = Off
 1 = Errors
 2 = Errors and warnings
 3 = Errors, warnings and information
 4 = Verbose (Errors, warnings, information & deep detail debug tracing.)
 -->
  <add name="TsMain" value="4" />
  <add name="TsInit" value="4" />
  </switches>
  </system.diagnostics>
</configuration>

I want to be able to update the TsMain and TsInit trace levels from code using the System.Configuration namespace rather than directly manipulating the xml through code.

I tried:Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSection systemDiagnostics = config.GetSection("system.diagnostics");
But I cannot access the key value pairs for the switches. What I want to do is something like:
systemDiagnostics.Switches.Key["TsMain"].Value = 2;

Any help with this greatfully received.





Answers (2)