Search code examples
c#system.diagnostics

What are benefits of using System. Diagnostics.Switch?


I am trying to get into system. Diagnostics namespace and learning about it, but the first class I try to learn is BooleanSwitch.

I read about it in MSDN and I can't understand what the benefits of using this library really are. I tried to search on the internet but I am confused by what I saw in words and sentences that I do not understand like (trace, control debugging).

And there is a base class named Switch which BooleanSwitch inherits from and other class in this namespace.

Is there any website I can learn from about the Diagnostics namespace?


Solution

  • I read about it in MSDN and I can't understand what the benefits of using this library really are.

    The benefits of using this library are easily available diagnostic tools. It is definitely a library worth using if you're looking to do some debugging with .NET.

    System.Diagnostics.EventLog

    This component provides functionality to write to event logs, read event log entries, and create and delete event logs and event sources on the network.

    System.Diagnostics.Trace

    Instrumentation allows you to monitor the health of your application running in real-life settings.

    System.Diagnostics.StackTrace & System.Diagnostics.StackFrame

    These classes will expose the stack information about the routines. Following example will return the name of the calling method.

    System.Diagnostics.Debug

    Provides a set of methods and properties that help debug your code. This class cannot be inherited. We all are accustomed to this class. Following are the important methods of this class.

    System.Diagnostics.Debug.WriteLine <<< this one is big

    You can write to diagnostic logs, do traces.. these are extremely important for complex projects. I'd say for simpler projects it might be a bit overkill, but it's definitely something you want to familiarize yourself with anyway.

    Almost every library is going to be helpful in some context. Libraries are pre-done work and the more relevant ones that you understand, the less work you will need to do. MSFT libraries are usually very well documented and designed.

    If you're looking for a good beginner guide, this would be a good place to start.

    This is VB.NET but the general overview still applies: https://www.codeproject.com/articles/5358/beginning-system-diagnostics

    I do agree that often the MSFT docs are hard to understand and implement.

    EDIT: I just noticed that is a VB.NET (yuck) link so it would only be useful for theory and learning what the methods do.

    The boolean switch just chooses whether or not to write an error message or not. This could be useful in some situations where an exception is not thrown but your code has noticed some error in the data or some other code path has tripped a situation where you want to write to the debug log.

    I don't think it's as useful as the general namespace is. It might be useful in some niche situations but regardless of the switch; you'll want to learn this namespace anyway.