I want to detect and report bugs in specific cases, using the same behavior as Debug.Assert()
, but in a Release build. How can I do that?

- 18,291
- 25
- 109
- 191

- 45,157
- 15
- 111
- 168
-
No offense, but how does a "former member of C# team at Microsoft" (your profile description) not know about `Trace.Assert`? – Mr. Smith Jan 16 '14 at 05:01
-
The list of things I don't know is very, very long. – Jay Bazuzi Jan 16 '14 at 05:38
-
see: https://stackoverflow.com/questions/3021538/debug-assert-appears-in-release-mode While the intention is quite the opposite, the answers should give you the clue you need. – mbx Oct 20 '17 at 21:46
3 Answers
You should be able to use Trace.Assert()
.
From MSDN:
Use the Trace.Assert method if you want to do assertions in release builds. The Debug.Assert method works only in debug builds.

- 65,241
- 13
- 115
- 165
-
Even better than my answer, I did not know about `Trace.Assert` – Scott Chamberlain Jan 16 '14 at 04:21
You can manually add the DEBUG constant while still having Release optimizations enabled.
In the Build tab of your project settings just check the box that enables the DEBUG constant.
This allows all functions that have [ConditionalAttribute("DEBUG")]
(like Assert()
) to still function in your compiled program.
EDIT: Grant's answer is even better, if possible use Trace.Assert
instead. That function triggers on if the constant TRACE
is defined and it is defined by default in Release builds. That will make sure you don't get any unforeseen side effects of enabling any other code that uses #if DEBUG
or [ConditionalAttribute("DEBUG")]
in your code.

- 1
- 1

- 124,994
- 33
- 282
- 431
Can't you turn on Tracing and perform tracing? You can use conditional tracing in Release mode. Also, you can implement some conditionally logging with log4net

- 14,625
- 2
- 37
- 55