Question about effective logging in C#

Posted by MartyIX on Stack Overflow See other posts from Stack Overflow or by MartyIX
Published on 2010-04-14T18:49:00Z Indexed on 2010/04/14 18:53 UTC
Read the original article Hit count: 340

Filed under:
|
|

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 (http://msdn.microsoft.com/en-us/library/system.diagnostics.trace.aspx) left out if compiling in release mode? If it is so is it possible that my methods would behave similarly?

  • http://msdn.microsoft.com/en-us/library/fht0f5be.aspx

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

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about debugging