System.Diagnostics.Trace

Tracing allows you to keep track of the application, how it is performing, what errors are produced etc
Being able to collect information about an application is extremely useful when trying to track down errors and improving performance.
The System.Diagnostics.DebuggerStepThrough attribute can be used on class properties and methods when you don't want the debugger to step into them


Check the Options

Define TRACE constant - Build tab
This is a compilation constant
For any of this to happen you must make sure the option is selected
SS


Release configuration

A release solution configuration includes all the Debug class methods


Trace Switches

Trace switches allow you to enable, disable and filter tracing output
They can be configured externally in the .config file


Trace Listeners

A trace listener is an object that receives output and sends it somewhere else.
All the trace listeners are derived from the abstract TraceListener class.
If you inherit from this class then you must at least implement the Write and WriteLine methods.
The System.Diagnostics namespace somes with several trace listeners built in.

  • DefaultTraceListener - created and enabled automatically when tracing or debugging is enabled

This is the default trace listener and is used when we want to see tracing output in the Visual Studio editor.
If we want to capture messages outside of Visual Studio then you must use a different trace listener.


  • TextWriterTraceListener - used to log to a file

This type of trace listener will redirect the tracing output to an instance of the TextWriter class or to any stream, for example a log file, console etc.
[copy code from Tracer_Start


  • EventLogTraceListener - used to log to windows event viewer

This type of trace listener will direct the tracing output to the windows event log
This class can even be used to direct the output to a remote computer.



Methods

Assert 
AutoFlush 
Closecloses the stream to receive no more output
CorrelationManager 
Failoutputs the text with the call stack
Flushflushes the output buffer
Indent 
Indentlevel 
Indentsize 
Listeners 
Refresh 
TraceError 
TraceInformation 
TraceWarning 
Unindent 
UseGlobalLock 
Writeoutputs the text
WriteIf 
WriteLineoutputs the text with a carriage return
WriteLineIf 


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext