views:

135

answers:

6

I've written a simple class for debugging and I call the method Debugger.WriteLine(...) in my code like this:

Debugger.WriteLine("[Draw]", "InProgress",
    "[x,y] = " + x.ToString("0.00") + 
    ", " + y.ToString("0.00") + 
    "; pos = " + lastPosX.ToString() + "x" + 
    lastPosY.ToString() + " -> " + posX.ToString() + 
    "x" + posY.ToString() + "; SS = " + 
    squareSize.ToString() + "; MST = " + 
    startTime.ToString("0.000") + "; Time = " + time.ToString() +
    phase.ToString(".0000") + "; progress = " + 
    progress.ToString("0.000") + "; step = " + 
    step.ToString() + "; TimeMovementEnd = " + 
    UI.MovementEndTime.ToString()
);

The body of the procedure Debugger.WriteLine is compiled only in Debug mode (directives #if, #endif). What makes me worry is that I often need ToString() in Debugger.WriteLine call which is costly because it creates still new strings (for changing number for example). How to solve this problem?

A few points/questions about debugging/tracing:

  • I don't want to wrap every Debugger.WriteLine in an IF statement or to use preprocessor directives in order to leave out debugging methods because it would inevitable lead to a not very readable code and it requires too much typing.

  • I don't want to use any framework for tracing/debugging. I want to try to program it myself.

  • Are Trace methods left out if compiling in release mode? If it is so is it possible that my methods would behave similarly?

  • With the static String.Format method I can do this:

    output = String.Format("You are now {0} years old.", years);

Which seems nice. Is it a solution for my problem with ToString()?

+3  A: 

Use StringBuilder to create your output strings instead of concatenating each and every value.

And you can create your own custom debugger (MyDbg) class that contains a WriteLine member the contents of which you can surround with compile directives. It wouldn't entirely compile out the debug code but would turn you MyDbg.WriteLine calls into no-ops.

Here's a quick sketch of the class:

using System;
using System.Text ;

public static class MyDbg
{
    public static void WriteLine(string str) // and some other params
    {
        #if DEBUG

        StringBuilder sb = new StringBuilder();
        sb.Append(str);
        // etc. appending other params as you see fit
        #endif
    }
}

OR

[Conditional("DEBUG")]
public static class MyDbg
{
    public static void WriteLine(string str) // and some other params
    {
        StringBuilder sb = new StringBuilder();
        sb.Append(str);
        // etc. appending other params as you see fit
    }
}

You'd modify it to suit your own needs of course. And instead of creating a separate class, you could create a member method if #if DEBUG/#endif built-in for displaying its own state when in the debugger.

Paul Sasik
I don't believe that StringBuilder will make any difference in this case. Since the strings are assembled in a single statement, the compiler will not create intermediate strings for each concatenation. (AFAIK)
harpo
@harpo: That's a compiler optimization i've never heard of. As far as i know, string operations are immutable and any concatenation or augmentation creates a new one regardless of where it's used.
Paul Sasik
harpo is right: the compiler replaces a + b + c with string.Concat(a,b,c)
Catalin DICU
I have contents of methods surrounded with compile directives. But how to use StringBuilder and still have just one line call of debugging method?
MartyIX
@Catalin and harpo: i won't doubt two opinions but i'd love to see and study the idea further. Can you provide a link? Please.
Paul Sasik
This question seems to be two pronged: 1) performance considerations in logging which seem to be optimized by the compiler and 2) How to do you keep your code clean and manageable and still compile out debugger cruft in release? Seems like a wrapper class/method of some sort is the only way to go.
Paul Sasik
@Paul Sasik: use Reflector and see for yourself
Catalin DICU
@Paul Sasik: Thank you! I'll change my implementation to use StringBuilder. It still doesn't prevent excessive allocation of strings in debug mode. I guess I can implement often types of parameters like: WriteLine(string, number) in order to use the full advantage of StringBuilder. But it's not general solution of problem. The excessive allocating of strings in debug mode may still happens.
MartyIX
A: 
  1. You do not need to wrap every line. Just write helper method containing Debug.WriteLine and turn it on/off by flag (usually bool).

  2. Tracing will be in your code even in release mode. You can configure it, see http://msdn.microsoft.com/en-us/library/system.diagnostics.tracelistener.aspx

  3. string.Format calls ToString() on arguments internally, so no benefit there.

Tomas Voracek
1. I've done that. 2., 3. thanks for answer!
MartyIX
Concerning (3), I think this is not relevant here, as performance considerations are usually a non-issue in Debug configuration, anyway. `string.Format` still has the benefit of improving code legibility (which, I'd say, sort of goes hand in hand with debugging.)
stakx
stakx: I think it is relevant, because you can have tracing enabled in release code, thus affecting application performance.
Tomas Voracek
+2  A: 

For Logging have a look at frameworks such as Log4net or the Enterprise library. They make logging configurable in many ways. Even if you want to log at all.

HTH

Sascha
+1  A: 

Using string.Format(), the expected output will actually become more easily recognizable just from looking at the code (IMHO):

Debug.WriteLine(string.Format(
    "[Draw]InProgress[x,y] = {0:0.00}, {1:0.00}; pos = {2}x{3} -> {4}x{5}; ...",
    x, y,
    lastPosX, lastPosY,
    posX, posY,
    ...));
stakx
Thanks, it looks good!
MartyIX
+4  A: 

Using Reflector I found out that Debug.Writeline is declared this way :

[Conditional("DEBUG")]
public static void WriteLine(string message)

That means that in Release mode all calls to this method are eliminated from code.

For example this code :

public static void Test(int a)
{
    string b = Console.ReadLine();
    Debug.WriteLine(a + " " + b);
    Console.WriteLine(a + " " + b);
}

compiles in release mode to :

public static void Test(int a)
{
    Console.WriteLine(a + " " + Console.ReadLine());
}
Catalin DICU
+1; http://msdn.microsoft.com/en-us/library/aa288458(VS.71).aspx - the link that confirms what you've said.
MartyIX
+1  A: 

The pattern I use is to define a logging interface (ILogger) that gets dependency injected into all the classes

public class MyClassThatLogs
{
    public MyClassThatLogs(ILogger logger)
    {
        try
        {
            logger.Write("bla bla bla");
        }
        catch(Exception ex)
        {
            logger.Write(ex); //overload of Write() that takes in an Exception
        }
    }
}

So when you new it up you can pass in a 'real' logger or a mock/fake one

var mc = new MyClassThatLogs(new FakeLogger());

Now you have abstracted away the need to know in the MyClassThatLogs class what's going on with logging. Basic SOLID stuff. So the 'top' (say main() method of a console) would have the IF DEBUG directive then pass in the correct implementation of the logger.

Kevin Won
Thank you for suggestion! I was thinking about [Conditional("FakeLogger")], [Conditional("MyLogger")],... for classes. Maybe it would be also flexible as your solution. The change of logger would be matter of changing mode in Visual Studio.
MartyIX
Cool! The important take away I think is to get the dependency out of your class... your class shouldn't care how logging is implemented, it should just declare that it needs to log. Again, this just comes from the SOLID principles--Inversion of control and Dependency Injection.
Kevin Won