Instrumenting and tracing apps

Tracing is a method that you track the implementation of your program when it’s running. It’s possible to add debugging and tracing instrumentation to a .NET Framework program when you create this, and you’ll be able to utilize that instrumentation both when you’re creating the program and as soon as you’ve deployed it. It is possible to use the System.Diagnostics.Trace,System.Diagnostics.Debug, and System.Diagnostics.TraceSource courses to capture information regarding errors and program implementation in logs, text documents, or other apparatus for later evaluation.

The expression instrumentation identifies an ability to track or assess the degree of an item’s functionality and to diagnose errors. In programming, this also implies that the ability of an application to integrate:

Debugging – Tracking and correcting programming errors in a program under development. To learn more, visit Debugging.

Performance counters – Components which enable you to monitor the performance of your program. To learn more, visit Performance Counters.

Event logs – Components which enable you get and monitor big events in the implementation of your program.

Instrumenting your program by putting trace statements in strategic places in your code is particularly helpful for distributed applications. By employing follow statements you are able to instrument a program not just to display info when things go wrong, but also to track how well the program is doing.

The TraceSource course provides improved tracing features and may be utilized instead of the static procedures of the elderly Trace and Debug tracing classes. The recognizable Trace and Debug courses continue to be widely utilized, but the TraceSource course is suggested for new tracing commands, for example TraceEvent and TraceData.

The Trace and Debug courses are indistinguishable, except that functions and procedures of this Trace course are compiled by default to release builds, but these of the Debug course aren’t.

The Trace and Debug classes offer the capacity to track and analyze program performance either through development or after installation. By way of instance, you may use the Trace course to monitor particular kinds of activities in a deployed application as they happen (by way of instance, production of new database relations ), and may therefore track the program’s efficiency.

During development, you may use the output techniques of this Debug course to show messages from the Output window of the Visual Studio integrated development environment (IDE). As an instance:

Every one of those examples will show”Hello World!” From the Output window once the program is conducted in the debugger.

This allows you to debug your software and optimize their functionality based on their behaviour on your test environment. It is possible to debug your program on your debug build together with all the Debug conditional attribute turned on so you get all debugging output. When your program is ready for launch, it is possible to compile your release build without turning to the Debug conditional attribute, so the compiler won’t contain your debugging code at the final executable. To learn more on different build configurations for your program, visit Compiling and Building.

You might even trace code implementation in an installed program, utilizing methods of this Trace course. By putting Trace Switches on your code, it is possible to control if tracing happens and how broad it is. This enables you to track the status of your program in a manufacturing environment. This is particularly vital in a business program which uses multiple elements running on multiple computers. It is possible to control the way the switches are utilized after installation through the setup file.

When you’re creating a program for which you plan to use tracing, you generally contain both debugging and tracing messages from the program code. Whenever you’re ready to set up the program, you are able to compile your release build without turning to the Debug conditional attribute. But, it is possible to turn on the Trace conditional attribute so the compiler contains your trace code from the executable.

There are 3 stages of code tracing:

Instrumentation — you include trace code into your program.

Tracing — the tracing code writes info to the specified goal.

Analysis — you assess the tracing information to recognize and identify issues in the program.

During evolution, all debug and input methods compose info to the Output window in Visual Studio by default. In a deployed application, the approaches write tracing information regarding the goals you define. To learn more about establishing an output for debugging or tracing, visit Trace Listeners.

The following is a general view of the significant steps typically involved with using tracing to examine and fix potential problems in deployed applications. To learn more about the best way to execute these measures, see the proper link.

To utilize tracing within an program

Contemplate which tracing output you’ll want to obtain onsite as soon as you’ve deployed the program.

Produce a set of buttons.

Insert the trace statements into the program code.

Determine where you want the tracing output to look and include the right listeners.

Test and debug your program as well as the tracing code it contains.

Compile the program into executable code with one of the following approaches:

Utilize the Build menu Together with the Debug page of this Property Pages dialogue box at Solution Explorer.

– or –

Utilize the Trace and Debug compiler directives to your command-line Way of compiling.

In case a problem occurs during run time, then flip on the proper trace switch. To learn more, visit Configuring Trace Switches.

The tracing code writes arranges messages into a specified goal, as an instance, a display, a text document, or a event log. The kind of listener you contained from the Trace.Listeners collection decides the goal.

Assess the tracing messages to recognize and understand the issue from the program.

When you make a distributed program, you might find it tough to check the program in the way it’ll be used. Few improvement teams have the capacity to check all probable combinations of operating systems or Web browsers (such as most of the localized language choices ), or even to simulate the large number of users who will access the program at precisely the exact same moment. Under those conditions, you can’t examine how a distributed application will react to large volumes, different setups, and particular end-user behaviours. Additionally, many areas of a distributed application don’t have any user interface with which you are able to interact directly or see the action of these components.

But you are able to compensate for it by allowing distributed applications to spell out specific events of interest to system administrators, particularly things go wrong, by instrumenting the program — which is, by putting trace statements in strategic places on your code. Then if something unexpected happens at run time (as an instance, overly slow reaction time), you are able to establish the probable cause.

With trace statements you’re able to stay away from the challenging job of analyzing the initial source code, changing it, recompiling, and trying to create the run-time mistake inside the debugging environment. Bear in mind that you can instrument a program not just to exhibit errors, but also to track performance.

You have to exercise particular care when setting your trace statements to be used during run time. You have to contemplate what tracing data is very likely to be required in a deployed application, so that likely tracing situations are satisfactorily covered. Because applications that utilize tracing vary broadly, but there are no general guidelines for tactical positioning of tracing. To learn more on putting trace statements, visit How toAdd Trace Statements to Application Code.

Output from Tracing

Trace output is accumulated by objects known as listeners. When a trace listener is made, it’s generally added to the Trace.Listeners set, permitting the listener to get all input.

Tracing data is always composed at the least on the default option Trace output goal, the DefaultTraceListener. If for any reason you’ve deleted the DefaultTraceListener without including some other listeners into the Listeners set, you won’t obtain any messages that are unread. To learn more, visit Trace Listeners.

Both Debug associates and Trace techniques that write distributing information are recorded in the next table.

The outcome is written only if the condition defined as an argument from the Assert announcement is untrue . WriteIfThe text that is specified, in case the condition defined as an argument from the WriteIf announcement is fulfilled. WriteLineIfThe given text and a carriage return, if the condition defined as an argument from the WriteLineIf announcement is fulfilled.

All listeners at the Listeners collection get the messages described in the above mentioned table, but the action taken may change based on the type of listener gets the message. By way of instance, the DefaultTraceListener shows an assertion dialogue box once it receives a Fail or neglected Assert telling, however a TextWriterTraceListener just writes the output signal to its own flow.

You’re able to create customized outcomes by implementing your listener. A custom trace listener might, by way of instance, exhibit the messages into a message box, or link to a database to incorporate messages into a desk. All custom listeners must support the six approaches mentioned previously. To learn more on producing developer-defined listeners, visit TraceListener from the .NET Framework reference.

The Write and WriteLine approaches always compose the text that you define. Assert, WriteIf, and WriteLineIf call for a Boolean argument that controls whether they write the specified text; they compose the specified text only if the saying is authentic (such as WriteIf and WriteLineIf), or even fictitious (such as Assert). The Fail process always writes the text. To Learn More, see How toAdd Trace Statements to Application Code along with also the .NET Framework reference.

The outcome is written only if the condition defined as an argument from the Assert announcement is untrue . WriteIfThe text that is specified, in case the condition defined as an argument from the WriteIf announcement is fulfilled. WriteLineIfThe given text and a carriage return, if the condition defined as an argument from the WriteLineIf announcement is fulfilled.

All listeners at the Listeners collection get the messages described in the above mentioned table, but the action taken may change based on the type of listener gets the message. By way of instance, the DefaultTraceListener shows an assertion dialogue box once it receives a Fail or neglected Assert telling, however a TextWriterTraceListener just writes the output signal to its own flow.

You’re able to create customized outcomes by implementing your listener. A custom trace listener might, by way of instance, exhibit the messages into a message box, or link to a database to incorporate messages into a desk. All custom listeners must support the six approaches mentioned previously. To learn more on producing developer-defined listeners, visit TraceListener from the .NET Framework reference.

The Write and WriteLine approaches always compose the text that you define. Assert, WriteIf, and WriteLineIf call for a Boolean argument that controls whether they write the specified text; they compose the specified text only if the saying is authentic (such as WriteIf and WriteLineIf), or even fictitious (such as Assert). The Fail process always writes the text. To Learn More, see How toAdd Trace Statements to Application Code along with also the .NET Framework reference.

Security Concerns

If you don’t disable tracing and debugging prior to deploying an ASP.NET application, your program may disclose details about itself which may be manipulated by a malicious application.