|
64 | 64 |
|
65 | 65 | You can create a <xref:System.Diagnostics.BooleanSwitch> in your code and set the <xref:System.Diagnostics.BooleanSwitch.Enabled> property directly to instrument a specific section of code. |
66 | 66 |
|
67 | | -For .NET Framework apps only, you can also enable or disable a <xref:System.Diagnostics.BooleanSwitch> through the application configuration file and then use the configured <xref:System.Diagnostics.BooleanSwitch> value in your application. To configure a <xref:System.Diagnostics.BooleanSwitch>, edit the configuration file that corresponds to the name of your application. Within this file, you can add or remove a switch, set a switch's value, or clear all the switches previously set by the application. The configuration file should be formatted like the following example. |
| 67 | +The <xref:System.Diagnostics.BooleanSwitch.Enabled> property of the new switch is set to `false` by default. |
68 | 68 |
|
69 | | -```xml |
70 | | -<configuration> |
71 | | - <system.diagnostics> |
72 | | - <switches> |
73 | | - <add name="mySwitch" value="1"/> |
74 | | - </switches> |
75 | | - </system.diagnostics> |
76 | | -</configuration> |
77 | | -``` |
78 | | -
|
79 | | - This example configuration section defines a <xref:System.Diagnostics.BooleanSwitch> with the <xref:System.Diagnostics.Switch.DisplayName> property set to `mySwitch` and the <xref:System.Diagnostics.BooleanSwitch.Enabled*> value set to `true`. Within your .NET Framework application, you can use the configured switch value by creating a <xref:System.Diagnostics.BooleanSwitch> with the same name, as shown in the following code example. |
80 | | -
|
81 | | - :::code language="csharp" source="~/snippets/csharp/System.Diagnostics/BooleanSwitch/Overview/remarks.cs" id="Snippet2"::: |
82 | | - :::code language="vb" source="~/snippets/visualbasic/System.Diagnostics/BooleanSwitch/Overview/remarks.vb" id="Snippet2"::: |
83 | | -
|
84 | | -For .NET Core and .NET 5+ apps, the <xref:System.Diagnostics.BooleanSwitch.Enabled> property of the new switch is set to `false` by default. |
85 | | -
|
86 | | -For .NET Framework apps, the <xref:System.Diagnostics.BooleanSwitch.Enabled> property is set using the value specified in the configuration file. Configure the switch with a value of 0 to set the <xref:System.Diagnostics.BooleanSwitch.Enabled> property to `false`; configure the switch with a nonzero value to set the <xref:System.Diagnostics.BooleanSwitch.Enabled> property to `true`. If the <xref:System.Diagnostics.BooleanSwitch> constructor cannot find initial switch settings in the configuration file, the <xref:System.Diagnostics.BooleanSwitch.Enabled> property of the new switch is set to `false`. |
87 | | -
|
88 | | - You must enable tracing or debugging to use a switch. The following syntax is compiler specific. If you use compilers other than C# or Visual Basic, refer to the documentation for your compiler. |
89 | | -
|
90 | | -- To enable debugging in C#, add the `/d:DEBUG` flag to the compiler command line when you compile your code, or you can add `#define DEBUG` to the top of your file. In Visual Basic, add the `/d:DEBUG=True` flag to the compiler command line. |
91 | | -
|
92 | | -- To enable tracing in C#, add the `/d:TRACE` flag to the compiler command line when you compile your code, or add `#define TRACE` to the top of your file. In Visual Basic, add the `/d:TRACE=True` flag to the compiler command line. |
| 69 | + You must enable tracing or debugging to use a switch. |
93 | 70 |
|
94 | 71 | > [!NOTE] |
95 | | -> These debug and trace compiler switches are not required when using the <xref:System.Diagnostics.BooleanSwitch> class in isolation. They are only required in conjunction with <xref:System.Diagnostics.Trace> or <xref:System.Diagnostics.Debug> methods that are conditionally compiled. |
| 72 | +> These debug and trace compiler switches are not required when using the <xref:System.Diagnostics.BooleanSwitch> class in isolation. They are only required in conjunction with <xref:System.Diagnostics.Trace> or <xref:System.Diagnostics.Debug> methods that are conditionally compiled. |
96 | 73 |
|
97 | | - For more information on instrumenting your application, see <xref:System.Diagnostics.Debug> and <xref:System.Diagnostics.Trace>. For more information about configuring and using trace switches, see [Trace Switches](/dotnet/framework/debug-trace-profile/trace-switches). |
| 74 | + For more information on instrumenting your application, see <xref:System.Diagnostics.Debug> and <xref:System.Diagnostics.Trace> |
98 | 75 |
|
99 | 76 | > [!NOTE] |
100 | | -> To improve performance, you can make <xref:System.Diagnostics.BooleanSwitch> members `static` in your class. |
| 77 | +> To improve performance, you can make <xref:System.Diagnostics.BooleanSwitch> members `static` in your class. |
101 | 78 |
|
102 | 79 | ## Examples |
103 | 80 | The following example creates a <xref:System.Diagnostics.BooleanSwitch> and uses the switch to determine whether to print an error message. You create the switch at the class level. The `Main` method passes its location to `MyMethod`, which prints an error message and where the error occurred. |
@@ -177,25 +154,7 @@ For .NET Framework apps, the <xref:System.Diagnostics.BooleanSwitch.Enabled> pro |
177 | 154 | <format type="text/markdown"><![CDATA[ |
178 | 155 |
|
179 | 156 | ## Remarks |
180 | | - When you create a <xref:System.Diagnostics.BooleanSwitch>, the `displayName` parameter is used to find the initial switch settings for .NET Framework apps in the application configuration file. If the constructor cannot find initial settings, or for .NET Core and .NET 5+ apps, the <xref:System.Diagnostics.BooleanSwitch.Enabled> property is set to `false` (disabled). |
181 | | -
|
182 | | - To set the level of your <xref:System.Diagnostics.BooleanSwitch> in a .NET Framework app, edit the configuration file corresponding to the name of your application. Within this file, you can add a switch and set its value, remove a switch, or clear all switches previously set by the application. The configuration file should be formatted like the following example: |
183 | | -
|
184 | | -```xml |
185 | | -<configuration> |
186 | | - <system.diagnostics> |
187 | | - <switches> |
188 | | - <add name="mySwitch" value="10" /> |
189 | | - <add name="myNewSwitch" value="20" /> |
190 | | - <remove name="mySwitch" /> |
191 | | - <clear/> |
192 | | - </switches> |
193 | | - </system.diagnostics> |
194 | | - </configuration> |
195 | | -``` |
196 | | -
|
197 | | -> [!NOTE] |
198 | | -> The switches you created should be `static`. |
| 157 | + When you create a <xref:System.Diagnostics.BooleanSwitch>, the <xref:System.Diagnostics.BooleanSwitch.Enabled> property is set to `false` (disabled). |
199 | 158 |
|
200 | 159 | ## Examples |
201 | 160 | The following example creates a <xref:System.Diagnostics.BooleanSwitch> and uses the switch to determine whether to print an error message. The switch is created at the class level. The `Main` method passes its location to `MyMethod`, which prints an error message and where the error occurred. |
|
0 commit comments