简体   繁体   中英

Should I see debug outputs In Visual Studio's output window when running in a release configuration?

I am wondering if I am meant to see anything in my Debug output when running in release mode?

The below image is a screenshot of my output window when I am running in Release configuration. 在此处输入图片说明

Seeing debug stuff makes me think that I am not actually running a release compile, Is there a way that I can be sure that I am definitely running a release mode?

Short answer

These appear to be Trace messages , not Debug messages , so they appear even in release builds.

Long answer

Let's see what's going on by investigating the various contextual meanings of the term "Debug" here.

The "Debug" Build Configuration

First, there's the context of the drop-down with the "Debug" and "Release" options in Visual Studio. Those options are build configurations . To understand the significance of this option, double-click the "Properties" node for your project and change to the "Build" page.

C#项目属性中的“生成”页面

This page (and some others) can have settings specific to a certain build configuration. You can see settings for each configuration with the drop-down on the page (it defaults to whichever build configuration you're currently using).

There's nothing special about the names "Debug" and "Release" - those are just default configurations, with default settings for those scenarios. You can change the settings for a configuration. You can also define your own build configurations, but doing so is out of the scope of this answer. Finally, these settings are also parameterized by a simliar target platform (eg, "AnyCPU", "x86", etc.), but for this answer I'll assume all builds are using the same platform setting.

The "DEBUG" Conditional Compilation Symbol

C# has conditional compilation - the ability to compile different sections of code depending on symbols that either are defined or undefined at compilation time. For instance, this constant has the value "A" if MY_SYMBOL_NAME is defined, or "B" otherwise.

        public const string MyConst =
#if MY_SYMBOL_NAME
      "A";
#else
      "B";
#endif

The top three settings on the project's "Build" page control these symbols. In particular are the "Define DEBUG constant" and "Define TRACE constant" - which really refer to compilation symbols. By checking these, the selected build setting compiles with the DEBUG and TRACE symbols defined, respectively.

By default, both "Debug" and "Release" configurations define TRACE , but fittingly only "Debug" defines DEBUG .

The Debug Class

Conditional compilation extends specifically to the members of the System.Diagnostics.Debug and the System.Diagnostics.Trace classes: calls to these classes' methods will only be compiled when the appropriate symbol is defined. So this:

Debug.WriteLine("!");

is treated like this:

#if DEBUG
    Debug.WriteLine("!");
#endif

These methods write to so-called "trace listeners", which can be configured either in code or in application configuration files .

So if the text you're seeing is written by calls to Trace , then you'll still see it, even in the Release configuration. Of course, this means that this logging will remain in shipped versions unless you explicitly disable the "Define TRACE constant" setting.

Start Debugging vs. Start Without Debugging

So why is Visual Studio showing this stuff, anyway? Because there's one more meaning of "Debug": whether you are starting the app with Visual Studio's debugger attached, or not. Both options for starting are listed in VS' Debug menu bar: "Start Debugging" and "Start Without Debugging". This is orthogonal to what build configuration you are using: you can run a Debug build without a debugger attached, and you can run a Release build with a debugger attached (though this may not be as helpful, because breakpoints may not be hit in some cases).

When you use a debugger, the Output window displays messages written to the default trace listener. This is labeled "Debug" but it includes anything written there - including calls to Trace , which are included (by default) in Release builds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM