Question about efficient login in C #

I wrote a simple class for debugging, and I call the Debugger.WriteLine (...) method in my code as follows:

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 Debugger.WriteLine procedure is compiled only in debug mode (directives #if, #endif). My concern is that I often need ToString () in a call to Debugger.WriteLine, which is expensive because it still creates new lines (for example, to change the number). How to solve this problem?

A few questions / debugging / tracing questions:

  • I don’t want to wrap every Debugger.WriteLine in an IF statement or use preprocessor directives to exclude debugging methods, because this will inevitably lead to not-very-readable code, and this takes too much input.

  • I do not want to use the framework for tracing / debugging. I want to try to program it myself.

  • Are trace methods if compiled in release mode? If possible, will my methods behave the same?

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

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

Which seems nice. Is this a solution to my problem with ToString ()?

+4
source share
6 answers

Use StringBuilder to create your own output strings, rather than concatenating each value.

And you can create your own custom debugger class (MyDbg) that contains the WriteLine element, the contents of which you can surround with compilation commands. It will not fully compile the debugging code, but will turn you into calls to MyDbg.WriteLine 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 would change it to, of course, satisfy your own needs. And instead of creating a separate class, you can create a member method if #if DEBUG / # endif is built-in to display your own state in the debugger.

+3
source

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

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

This means that in Release mode all calls to this method are excluded from the 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:

 public static void Test(int a) { Console.WriteLine(a + " " + Console.ReadLine()); } 
+4
source

For logging, take a look at structures such as Log4net or the corporate library. They allow you to configure logging in various ways. Even if you want to log in.

NTN

+2
source

Using string.Format() , the expected result will become more easily recognizable by simply 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, ...)); 
+1
source

I use a template to define a registration interface (ILogger) that gets the dependency injected into all 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 are a beginner, you can go to the β€œreal” registrar or layout / fake

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

Now you are distracted from the need to know in the MyClassThatLogs class what happens with logging. Base SOLID material. Thus, the "top" method (for example, the main () console) must have the IF DEBUG directive, and then pass the correct registrar implementation.

+1
source
0
source

Source: https://habr.com/ru/post/1306964/


All Articles