3

Using

this.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);

in the ctor of my data context class only works (prints queries to the output window) in Debug build configuration.

What do I need to do to get it to print to the output window in Visual Studio for a custom build configuration (not Debug)?

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
reggaeguitar
  • 1,795
  • 1
  • 27
  • 48
  • 1
    The `ConditionalAttribute` attribute is applied to the methods of Debug. Compilers that support `ConditionalAttribute` ignore calls to these methods unless "DEBUG" is defined as a conditional compilation symbol. Means it will only work when "DEBUG" symbol will be defined. You better use `Trace` class methods which will work in all configurations. FYI Debug and Trace both targets to same TraceListeners. – vendettamit Mar 10 '16 at 19:59
  • 3
    @reggaeguitar all methods in the `Debug` class have [`[Conditional("DEBUG")]`](https://msdn.microsoft.com/en-us/library/system.diagnostics.conditionalattribute(v=vs.110).aspx) on them, that means if the DEBUG compiler symbol is not set from the calling code then the code does not run. If you want your custom build configuration to run those methods you need to turn on the `DEBUG` compile symbol. Before you try that you may want to switch to the [`Trace`](https://msdn.microsoft.com/en-us/library/system.diagnostics.trace(v=vs.110).aspx) class, that is enabled by default in both debug and release – Scott Chamberlain Mar 10 '16 at 19:59
  • Go to the Properties of your project with your custom build configuration active, then go to the Build tab and check "Define DEBUG constant", which is mentioned in one of the duplicate's answers in addition to what @Scott said. :) But alright, reopened, because that isn't quite clear from [that question](http://stackoverflow.com/questions/1370449/debug-writeline-not-working). See also http://stackoverflow.com/questions/1159755/where-does-system-diagnostics-debug-write-output-appear, http://stackoverflow.com/questions/7477309/debug-writeline-is-not-printing-anything and so on. – CodeCaster Mar 10 '16 at 20:08
  • Thanks @vendettamit that will probably work for what I'm trying to accomplish. I don't want to enable the DEBUG constant because I have #if DEBUG blocks in the code that I don't want to hit – reggaeguitar Mar 10 '16 at 20:13

1 Answers1

4

All methods in the System.Diagnostics.Debug class have [Conditional("DEBUG")] on them, that means if the DEBUG compiler symbol is not set from the calling code then the code does not run.

If you want your custom build configuration to run those methods you need to turn on the DEBUG compile symbol. Before you try that you may want to switch to the System.Diagnostics.Trace class, that is enabled by default in both debug and release and relies on a [Conditional("TRACE")] on each method.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431