4

私はそのような行をたくさん使います

Console.WriteLine("Test");

VS2010でアプリケーションをデバッグします。

私の質問は、アプリケーションを作成するときに、これらすべての行にコメントを付ける必要がありますか?

ありがとう!

4

4 に答える 4

14

Yes. In fact, if your app was a console application, you'd really want those lines executed. Have a look at System.Diagnostics.Debug methods (e.g. Debug.WriteLine) which may be what you need. Their output is in Visual Studio's Output window, and they do nothing in Release code.

More generally, you can have code that's only compiled in a Debug build by doing:

#if DEBUG
// Debug-only code here.
#endif

You can also put this attribute before your method definition to write a method that's not called at all when you do a Release build:

    [System.Diagnostics.Conditional("DEBUG")]

All these methods have the advantage that they shouldn't affect the performance of production code.

To check I'm giving you accurate advice, I compiled the following in Release mode:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello world!");

#if DEBUG
        Console.WriteLine("Inside #if block.");
#endif

        WriteLine("With ConditionalAttribute.");

        Debug.WriteLine("Debug.WriteLine.");
    }

    [Conditional("DEBUG")]
    public static void WriteLine(string line)
    {
        Console.WriteLine(line);
    }
}

I then used the IL Dissasembler tool to see what will actually run:

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       11 (0xb)
  .maxstack  8
  IL_0000:  ldstr      "Hello world!"
  IL_0005:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_000a:  ret
} // end of method Program::Main

As you can see, only the Console.WriteLine method is called. The other three alternatives are, as we had hoped, 'compiled out' of the debug code.

The Debug version looks like this:

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       46 (0x2e)
  .maxstack  8
  IL_0000:  nop
  IL_0001:  ldstr      "Hello world!"
  IL_0006:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_000b:  nop
  IL_000c:  ldstr      "Inside #if block."
  IL_0011:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_0016:  nop
  IL_0017:  ldstr      "With ConditionalAttribute."
  IL_001c:  call       void ConditionalCompileTest.Program::WriteLine(string)
  IL_0021:  nop
  IL_0022:  ldstr      "Debug.WriteLine."
  IL_0027:  call       void [System]System.Diagnostics.Debug::WriteLine(string)
  IL_002c:  nop
  IL_002d:  ret
} // end of method Program::Main
于 2012-07-04T17:44:22.687 に答える
3

Yes, all your Console.WriteLine() will be compiled into IL code and thus embedded into your executable.

Instead of Console.WriteLine() use some logging framework, like log4net or NLog. These are much easier to configure and reuse.

Benefits of logging frameworks over Console.WriteLine

  • A change from debug to production logging will require a one-line update in a config file
  • You can log into file, console, database, xml, you can also send emails and do much more
  • Also you can combine several loggers, like save to file and output to console at the same time
  • How about fancy colour console output - a few lines in a config file
  • Easy to disable (one line change in a config file)
  • Easy to configure and it looks nicer than #if DEBUG .. #endif
  • You can do async logging
  • You don't need to change your logging code if you will split your app into reusable library and UI (for example to use with WPF and not only with Console)
于 2012-07-04T17:45:03.670 に答える
2
#define DEBUG
// ...
#if DEBUG
    Console.WriteLine("Debug version");
#endif

http://msdn.microsoft.com/en-us/library/4y6tbswk.aspx

于 2012-07-04T17:45:01.370 に答える
1

What type of application is it? In console apps, that's the way to output to the screen. In any case, the answer is yes - it will still be executed. Whether or not there's anything attached to the console is another question.

You may want to look at Debug.WriteLine() but there are may better methods (not to mention Console is very slow in VS)

于 2012-07-04T17:45:35.400 に答える