Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define TRACE Constant in .NET / Visual Studio

In Visual Studio 2010, if you go to a project's properties and go to the Build Tab, there is a checkbox for "Define TRACE Constant." Which is the equivalent of doing a #define TRACE.

All of the methods of System.Diagnostics.Trace have a [Conditional("TRACE")] around them.

My question is why would you ever turn this off? I mean, if you don't have any trace listeners defined, then it isn't as if you're going to fill up a log or something. It just feels weird to me. If you are going through the effort to put in calls to Trace, why would you want to not control it through the App/Web.config, but instead control it via a compiler switch, which rules out the possibility of turning it back on without a recompile.

Am I missing something? Surely, it can't be THAT bad for performance, right?

like image 710
aquinas Avatar asked Nov 28 '11 22:11

aquinas


People also ask

What is trace in C#?

Tracing helps to see the information of issues at the runtime of the application. By default Tracing is disabled. Tracing has the following important features: We can see the execution path of the page and application using the debug statement. We can access and manipulate trace messages programmatically.

What is trace in VB net?

Trace produces messages about program conditions even after application is compiled and released without interrupting application execution.

Which of the following is the use of trace class?

Tracing helps you isolate problems and fix them without disturbing a running system. This class provides methods to display an Assert dialog box, and to emit an assertion that will always Fail. This class provides write methods in the following variations: Write.

Which namespace does the trace and debug function belong?

NET Framework Class Library namespace System.


1 Answers

Presumably this checkbox is equivalent to the /define:TRACE compiler option. You might want to turn this option off for a release build either because you don't want end users to see the trace output for some reason (e.g. security), or to improve performance. Of course, the performance increase will depend on how much work is being done when it's on, but the Conditional attribute will cause the compiler to completely remove the function call (including any string formatting, etc.) from the generated IL, so it could make a significant difference.

like image 96
Matthew Strawbridge Avatar answered Oct 16 '22 18:10

Matthew Strawbridge