using switches & .config files to control diagnostic output, app.config
Place configuration file contents in a file called app.config in your main project folder.
When the project is built, a copy of the file is automatically generated an named to match the .exe with a “.config” suffix.
In the designer properties view setting a DynamicProperties creates an entry in app.config, in the appSettings section. It also adds class field “configurationAppSettings” which is initialized as follows:
System.Configuration.AppSettingsReader configurationAppSettings = new System.Configuration.AppSettingsReader();
A “dynamic property” enabled sqlConnection.ConnectionString initializations looks like:
sqlConnection.ConnectionString
= ((string)(configurationAppSettings.GetValue("sqlConnection.ConnectionString", typeof(string))));
An alternative all-in-one initialization (not efficient for many, great for just one):
= (string)(new System.Configuration.AppSettingsReader()).GetValue("JobSearchConnectionString", typeof(string));
And another way to get the job done that works great for strings:
= System.Configuration.ConfigurationSettings.AppSettings["JobSearchConnectionString"];
For diagnostic switches:
<configuration>
<system.diagnostics>
<switches>
<add name="DataGridDebugSwitch" value="4"/>
</switches>
</system.diagnostics>
</configuration>
Declare a TraceSwitch bound by name to the config file.
static TraceSwitch ts = new TraceSwitch("DataGridDebugSwitch", "Control debug output in DataGridColumnStyles");
Use the “If” flavor Trace or Debug methods:
Debug.WriteLineIf(ts.TraceInfo, String.Format("LinkLabel constructor id={0}",this.GetHashCode()));
The output will only appear if the switch setting is at least as high as the Boolean property chosen for the *If method's predicate argument:
1 == TraceError
2 == TraceWarning
3 == TraceInfo
4 == TraceVerbose
>4, user defined, no bool property.