Modernize remaining System.Diagnostics C# snippets - #13034
Conversation
Update the final 95 System.Diagnostics C# samples to current syntax and consolidate their net10.0 snippet projects while preserving documented APIs and runtime behavior. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 81f0fa5e-3bcf-4d3b-b652-f7dc76e7ae03
There was a problem hiding this comment.
Pull request overview
This PR modernizes the remaining C# snippet sources under snippets/csharp/System.Diagnostics to current C# syntax while keeping the documented APIs and observable behavior intact, and updates snippet projects to ensure the samples compile as runnable or consolidated entry points where needed.
Changes:
- Modernizes snippet source code (target-typed
new, built-in aliases, object/collection initializers, improvedusingpatterns, minor formatting cleanups). - Updates/creates snippet project files (many
Library→Exe/WinExe) and adds dispatcherProgram.csentry points for directories with multiple samples. - Removes unused
usingdirectives and updates sample scaffolding to keep projects building.
Reviewed changes
Copilot reviewed 159 out of 161 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| snippets/csharp/System.Diagnostics/XmlWriterTraceListener/Overview/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/XmlWriterTraceListener/Overview/program.cs | Modernize snippet code (target-typed new, using order). |
| snippets/csharp/System.Diagnostics/TraceSwitch/TraceWarning/source.cs | Modernize snippet; static field naming and brace usage. |
| snippets/csharp/System.Diagnostics/TraceSwitch/TraceWarning/Project.csproj | Formatting cleanup. |
| snippets/csharp/System.Diagnostics/TraceSwitch/TraceInfo/source.cs | Modernize snippet; static field naming and brace usage. |
| snippets/csharp/System.Diagnostics/TraceSwitch/TraceInfo/Project.csproj | Formatting cleanup. |
| snippets/csharp/System.Diagnostics/TraceSwitch/Overview/source.cs | Modernize snippet; convert entrypoint to Run. |
| snippets/csharp/System.Diagnostics/TraceSwitch/Overview/remarks.cs | Modernize snippet; convert entrypoint to Run. |
| snippets/csharp/System.Diagnostics/TraceSwitch/Overview/Project.csproj | Remove StartupObject for consolidated entrypoint. |
| snippets/csharp/System.Diagnostics/TraceSwitch/Overview/Program.cs | Add dispatcher entrypoint for multiple samples. |
| snippets/csharp/System.Diagnostics/TraceSwitch/Level/source.cs | Modernize snippet; static field naming and braces. |
| snippets/csharp/System.Diagnostics/TraceSwitch/Level/Project.csproj | Formatting cleanup. |
| snippets/csharp/System.Diagnostics/TraceListenerCollection/Add/source.cs | Use object initializer + explicit type. |
| snippets/csharp/System.Diagnostics/TraceListenerCollection/Add/Project.csproj | Formatting cleanup. |
| snippets/csharp/System.Diagnostics/TraceListener/GetSupportedAttributes/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/TraceListener/GetSupportedAttributes/program.cs | Modernize snippet; collection expression and target-typed new. |
| snippets/csharp/System.Diagnostics/TraceFilter/ShouldTrace/source.cs | Use object initializer + target-typed new. |
| snippets/csharp/System.Diagnostics/TraceFilter/ShouldTrace/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/Trace/WriteLineIf/source.cs | Modernize sample and rename type to avoid WinForms dependency. |
| snippets/csharp/System.Diagnostics/Trace/WriteLineIf/source1.cs | Modernize sample and rename type to avoid WinForms dependency. |
| snippets/csharp/System.Diagnostics/Trace/WriteLineIf/source2.cs | Modernize sample and rename type to avoid WinForms dependency. |
| snippets/csharp/System.Diagnostics/Trace/WriteLineIf/Project.csproj | Add snippet project for compilation. |
| snippets/csharp/System.Diagnostics/Trace/WriteLine/source.cs | Modernize sample and rename type to avoid WinForms dependency. |
| snippets/csharp/System.Diagnostics/Trace/WriteLine/source1.cs | Modernize sample and rename type to avoid WinForms dependency. |
| snippets/csharp/System.Diagnostics/Trace/WriteLine/source2.cs | Modernize sample and rename type to avoid WinForms dependency. |
| snippets/csharp/System.Diagnostics/Trace/WriteLine/Project.csproj | Add snippet project for compilation. |
| snippets/csharp/System.Diagnostics/Trace/WriteIf/source.cs | Modernize sample and rename type to avoid WinForms dependency. |
| snippets/csharp/System.Diagnostics/Trace/WriteIf/source1.cs | Modernize sample and rename type to avoid WinForms dependency. |
| snippets/csharp/System.Diagnostics/Trace/WriteIf/source2.cs | Modernize sample; string interpolation. |
| snippets/csharp/System.Diagnostics/Trace/WriteIf/source3.cs | Modernize sample and rename type to avoid WinForms dependency. |
| snippets/csharp/System.Diagnostics/Trace/WriteIf/Project.csproj | Add snippet project for compilation. |
| snippets/csharp/System.Diagnostics/Trace/Write/source.cs | Modernize sample and rename type to avoid WinForms dependency. |
| snippets/csharp/System.Diagnostics/Trace/Write/source1.cs | Modernize sample and rename type to avoid WinForms dependency. |
| snippets/csharp/System.Diagnostics/Trace/Write/source2.cs | Modernize sample; string interpolation. |
| snippets/csharp/System.Diagnostics/Trace/Write/source3.cs | Modernize sample and rename type to avoid WinForms dependency. |
| snippets/csharp/System.Diagnostics/Trace/Write/Project.csproj | Add snippet project for compilation. |
| snippets/csharp/System.Diagnostics/Trace/Overview/source.cs | Formatting/indentation normalization. |
| snippets/csharp/System.Diagnostics/Trace/Overview/Project.csproj | Formatting cleanup. |
| snippets/csharp/System.Diagnostics/Trace/Listeners/source.cs | Remove unused usings; explicit type + target-typed new. |
| snippets/csharp/System.Diagnostics/Trace/Listeners/Project.csproj | Formatting cleanup. |
| snippets/csharp/System.Diagnostics/Trace/Indent/source.cs | Remove unused usings. |
| snippets/csharp/System.Diagnostics/Trace/Indent/Project.csproj | Formatting cleanup. |
| snippets/csharp/System.Diagnostics/Trace/Fail/source1.cs | Modernize enum formatting and string interpolation. |
| snippets/csharp/System.Diagnostics/Trace/Fail/source.cs | Modernize enum formatting and string interpolation; comment punctuation. |
| snippets/csharp/System.Diagnostics/Trace/Fail/Project.csproj | Formatting cleanup. |
| snippets/csharp/System.Diagnostics/Trace/Close/source.cs | Modernize using and target-typed new; using order. |
| snippets/csharp/System.Diagnostics/Trace/Close/Project.csproj | Formatting cleanup. |
| snippets/csharp/System.Diagnostics/Trace/Assert/source2.cs | Modernize null-check to pattern matching. |
| snippets/csharp/System.Diagnostics/Trace/Assert/source1.cs | Modernize null-check to pattern matching. |
| snippets/csharp/System.Diagnostics/Trace/Assert/source.cs | Modernize snippet and add scaffolding for compilation (but BOM lost). |
| snippets/csharp/System.Diagnostics/Trace/Assert/Project.csproj | Formatting cleanup. |
| snippets/csharp/System.Diagnostics/TextWriterTraceListener/Writer/source.cs | Modernize indentation and object initializer usage. |
| snippets/csharp/System.Diagnostics/TextWriterTraceListener/Writer/Project.csproj | Formatting cleanup. |
| snippets/csharp/System.Diagnostics/TextWriterTraceListener/Write/source.cs | Modernize indentation and object initializer usage. |
| snippets/csharp/System.Diagnostics/TextWriterTraceListener/Write/Project.csproj | Formatting cleanup. |
| snippets/csharp/System.Diagnostics/TextWriterTraceListener/Overview/source.cs | Modernize using + ensure listener removed before disposal. |
| snippets/csharp/System.Diagnostics/TextWriterTraceListener/Overview/Project.csproj | Formatting cleanup. |
| snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconwritername.cs | Modernize, rename const, change Main→Run. |
| snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconstringname.cs | Modernize, rename const, change Main→Run. |
| snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconstring.cs | Modernize, change Main→Run. |
| snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconstreamname.cs | Modernize, rename const, change Main→Run. |
| snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconstream.cs | Modernize, rename const, change Main→Run. |
| snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/source1.cs | Modernize object initializer; change Main→Run. |
| snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/source.cs | Modernize using/disposal and string interpolation; change Main→Run. |
| snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/Project.csproj | Remove StartupObject for consolidated entrypoint. |
| snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/Program.cs | Add dispatcher entrypoint for multiple samples. |
| snippets/csharp/System.Diagnostics/SwitchAttribute/Overview/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/SwitchAttribute/Overview/program.cs | Modernize code (target-typed new, collection expression, interpolation). |
| snippets/csharp/System.Diagnostics/Switch/Overview/source.cs | Modernize; change Main→Run. |
| snippets/csharp/System.Diagnostics/Switch/Overview/remarks.cs | Modernize; change Main→Run. |
| snippets/csharp/System.Diagnostics/Switch/Overview/Project.csproj | Remove StartupObject for consolidated entrypoint. |
| snippets/csharp/System.Diagnostics/Switch/Overview/Program.cs | Add dispatcher entrypoint for multiple samples. |
| snippets/csharp/System.Diagnostics/Stopwatch/Overview/source1.cs | Modernize; change entrypoint to Run. |
| snippets/csharp/System.Diagnostics/Stopwatch/Overview/source.cs | Modernize aliases, spacing, and collections. |
| snippets/csharp/System.Diagnostics/Stopwatch/Overview/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/Stopwatch/Overview/Program.cs | Add dispatcher entrypoint for multiple samples. |
| snippets/csharp/System.Diagnostics/StackTrace/Overview/stacktracesample1.cs | Modernize constructors, spacing, and exception rethrow. |
| snippets/csharp/System.Diagnostics/StackTrace/Overview/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/StackTrace/GetFrames/stacktracesample2.cs | Modernize formatting and array syntax. |
| snippets/csharp/System.Diagnostics/StackTrace/GetFrames/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/StackFrame/Overview/source.cs | Modernize formatting and exception rethrow patterns. |
| snippets/csharp/System.Diagnostics/StackFrame/Overview/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/StackFrame/Overview/program.cs | Modernize and add dispatcher entrypoint. |
| snippets/csharp/System.Diagnostics/StackFrame/GetMethod/stacktracesample3.cs | Modernize constructors/spacing; keep EventLog behavior. |
| snippets/csharp/System.Diagnostics/StackFrame/GetMethod/Project.csproj | Switch project to Exe and align package reference. |
| snippets/csharp/System.Diagnostics/SourceFilter/.ctor/Project.csproj | Switch project to Exe and simplify TF. |
| snippets/csharp/System.Diagnostics/SourceFilter/.ctor/program.cs | Modernize code (aliases, collection expressions, interpolation). |
| snippets/csharp/System.Diagnostics/ProcessThread/IdealProcessor/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/ProcessThread/IdealProcessor/program.cs | Modernize; add braces and nint cast. |
| snippets/csharp/System.Diagnostics/ProcessStartInfo/Verb/source.cs | Modernize object initializers, loops, and using pattern. |
| snippets/csharp/System.Diagnostics/ProcessStartInfo/Verb/Project.csproj | Switch to WinExe and remove unused package reference. |
| snippets/csharp/System.Diagnostics/ProcessStartInfo/RedirectStandardOutput/stdstr.cs | Modernize using pattern (now has redundant block). |
| snippets/csharp/System.Diagnostics/ProcessStartInfo/RedirectStandardOutput/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/ProcessModule/ToString/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/ProcessModule/ToString/processmodule_tostring.cs | Modernize using/new usage. |
| snippets/csharp/System.Diagnostics/ProcessModule/Overview/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/ProcessModule/Overview/processmodule.cs | Modernize using/new usage. |
| snippets/csharp/System.Diagnostics/ProcessModule/ModuleName/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/ProcessModule/ModuleName/processmodule_modulename.cs | Modernize using/new usage. |
| snippets/csharp/System.Diagnostics/ProcessModule/ModuleMemorySize/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/ProcessModule/ModuleMemorySize/processmodule_modulememorysize.cs | Modernize using/new usage. |
| snippets/csharp/System.Diagnostics/ProcessModule/FileVersionInfo/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/ProcessModule/FileVersionInfo/processmodule_fileversioninfo.cs | Modernize using/new usage. |
| snippets/csharp/System.Diagnostics/ProcessModule/FileName/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/ProcessModule/FileName/processmodule_filename.cs | Modernize using/new usage. |
| snippets/csharp/System.Diagnostics/ProcessModule/EntryPointAddress/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/ProcessModule/EntryPointAddress/processmodule_entrypoint.cs | Modernize using/new usage. |
| snippets/csharp/System.Diagnostics/ProcessModule/BaseAddress/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/ProcessModule/BaseAddress/processmodule_baseaddress.cs | Modernize using/new usage. |
| snippets/csharp/System.Diagnostics/Process/SynchronizingObject/remarks.cs | Modernize; change Main→Run for consolidation. |
| snippets/csharp/System.Diagnostics/Process/SynchronizingObject/Project.csproj | Add WinForms snippet project file. |
| snippets/csharp/System.Diagnostics/Process/SynchronizingObject/Program.cs | Add dispatcher entrypoint for multiple samples. |
| snippets/csharp/System.Diagnostics/Process/SynchronizingObject/process_synchronizingobject.cs | Modernize WinForms sample; change Main→Run. |
| snippets/csharp/System.Diagnostics/Process/Start/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/Process/Start/program.cs | Modernize usings and object creation. |
| snippets/csharp/System.Diagnostics/Process/StandardOutput/write500lines.cs | Change Main→Run and add braces for clarity. |
| snippets/csharp/System.Diagnostics/Process/StandardOutput/stdoutput-sync.cs | Use SampleProcess helper to launch consolidated samples. |
| snippets/csharp/System.Diagnostics/Process/StandardOutput/stdoutput-async.cs | Use SampleProcess helper to launch consolidated samples. |
| snippets/csharp/System.Diagnostics/Process/StandardOutput/Project.csproj | Remove StartupObject for consolidated entrypoint. |
| snippets/csharp/System.Diagnostics/Process/StandardOutput/Program.cs | Add dispatcher + SampleProcess helper. |
| snippets/csharp/System.Diagnostics/Process/StandardOutput/process_standardoutput.cs | Change Main→Run and modernize using. |
| snippets/csharp/System.Diagnostics/Process/StandardInput/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/Process/StandardInput/process_standardinput.cs | Modernize using and built-in aliases. |
| snippets/csharp/System.Diagnostics/Process/StandardError/stderror-sync.cs | Change Main→Run and use consolidated helper. |
| snippets/csharp/System.Diagnostics/Process/StandardError/source.cs | Change Main→Run and modernize ProcessStartInfo. |
| snippets/csharp/System.Diagnostics/Process/StandardError/Project.csproj | Remove StartupObject for consolidated entrypoint. |
| snippets/csharp/System.Diagnostics/Process/StandardError/Program.cs | Add dispatcher + helper implementation. |
| snippets/csharp/System.Diagnostics/Process/Overview/Project.csproj | Add snippet project file for compilation. |
| snippets/csharp/System.Diagnostics/Process/Overview/Program.cs | Add dispatcher + SampleProcess helper (BOM issue present in some related files). |
| snippets/csharp/System.Diagnostics/Process/Overview/processstartstatic3.cs | Update helper sample; change Main→Run (BOM lost). |
| snippets/csharp/System.Diagnostics/Process/Overview/processstartstatic2.cs | Update sample to use consolidated helper (BOM lost). |
| snippets/csharp/System.Diagnostics/Process/Overview/processstartstatic.cs | Rename sample type and change Main→Run (BOM lost). |
| snippets/csharp/System.Diagnostics/Process/Overview/processstart.cs | Rename sample type and change Main→Run. |
| snippets/csharp/System.Diagnostics/Process/OnExited/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/Process/OnExited/program.cs | Modernize constructor usage and this. removal. |
| snippets/csharp/System.Diagnostics/Process/MainWindowTitle/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/Process/MainWindowTitle/process_mainwindowtitle.cs | Modernize ProcessStartInfo initialization. |
| snippets/csharp/System.Diagnostics/Process/MachineName/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/Process/MachineName/process_getprocessesbyname2_2.cs | Add braces and fix quoting in message. |
| snippets/csharp/System.Diagnostics/Process/Id/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/Process/Id/program.cs | Modernize parsing/disposal and simplify exception catch. |
| snippets/csharp/System.Diagnostics/Process/GetCurrentProcess/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/Process/GetCurrentProcess/processstaticget.cs | Modernize object creation. |
| snippets/csharp/System.Diagnostics/Process/EnableRaisingEvents/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/Process/EnableRaisingEvents/processexitedevent.cs | Modernize using/new patterns (now includes redundant block). |
| snippets/csharp/System.Diagnostics/Process/Close/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/Process/Close/process_refresh.cs | Modernize using and improve loop clarity. |
| snippets/csharp/System.Diagnostics/Process/BasePriority/source.cs | Modernize using and spacing. |
| snippets/csharp/System.Diagnostics/Process/BasePriority/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/PresentationTraceLevel/Overview/SimpleBinding.csproj | Formatting cleanup. |
| snippets/csharp/System.Diagnostics/PresentationTraceLevel/Overview/Person.cs | Formatting normalization. |
| snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/rawfraction.cs | Modernize collections/initializers and aliases. |
| snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/rateofcountspersecond64.cs | Modernize collections/initializers and aliases. |
| snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/rateofcountspersecond32.cs | Modernize collections/initializers and aliases. |
| snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/Project.csproj | Switch project to Exe for runnable sample. |
| snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/program.cs | Rename entry type and add dispatcher Main. |
| snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/numberofitems64.cs | Modernize collections/initializers and aliases. |
| snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/numberofitems32.cs | Modernize formatting and alias usage. |
| snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/averagetimer32.cs | Modernize formatting and alias usage; Main→Run. |
| snippets/csharp/System.Diagnostics/PerformanceCounterCategory/Overview/Project.csproj | Simplify TF/output and keep package reference. |
| snippets/csharp/System.Diagnostics/PerformanceCounterCategory/Overview/perfcountercatcreateexist.cs | Modernize spacing and exception handling formatting. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 159 out of 161 changed files in this pull request and generated 8 comments.
Suppressed comments (4)
Previously missed (4) — in code that hasn't changed since the last review.
snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/program.cs:139
- Rename the misspelled local variable to
denominator.
float denomenator = (float)s1.BaseValue - (float)s0.BaseValue;
snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/rawfraction.cs:155
- Rename the misspelled local variable to
denominator.
float denomenator = (float)rfSample.BaseValue;
snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/rateofcountspersecond32.cs:141
- Rename the misspelled local variable to
denominator.
float denomenator = (float)(s1.TimeStamp - s0.TimeStamp) / (float)s1.SystemFrequency;
snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/rateofcountspersecond64.cs:142
- Rename the misspelled local variable to
denominator.
float denomenator = (float)(s1.TimeStamp - s0.TimeStamp) / (float)s1.SystemFrequency;
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
@copilot Please respond to remaining code review comments. |
Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
Addressed in the latest commit:
All affected projects build cleanly with 0 warnings/errors. |
Contributes to #12713.
Summary
Modernizes the final 95 C# snippets in the
System.Diagnosticsnamespace so they use current C# syntax while preserving the documented APIs and runtime behavior.Updates 58 snippet projects and adds eight consolidation entry points for directories containing multiple samples. All affected projects build successfully; remaining warnings are expected platform and obsolete-API annotations for the demonstrated APIs.
Internal previews
Toggle expand/collapse
Note
This table shows the first 50 preview links (sorted alphabetically by file path) found in the OPS build report. For the full list, select OpenPublishing.Build Details within checks.
Build report