T
T
TheTalion2018-07-23 01:43:40
Debugging
TheTalion, 2018-07-23 01:43:40

How do you write debug? What are the established methods?

( I put the average difficulty, because I'm interested in the opinion of experienced colleagues )
What do you think is the best way to write an enable / disable debug in case the fixes break everything?
The simplest thought:

public class Container {

private List<Cell> Cells = new List<Cell>();

public bool AddCell(Item item)
    {
      Cells.Add (new Cell(item));

      Debug("Add new Cell in {0}, with item id {1}", Cells.Count - 1, item);

      return true;
    }

public bool IsDebugEnabled = false;
private void Debug(string str)
    {
      if(IsDebugEnabled)
        Debug.Print(str);
    }
}

And do you think it makes sense to write such a debug if all functions are 2-10 lines long?
Perhaps you know some more optimal solutions for writing debugging?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Skobkin, 2018-07-23
@skobkin

Usually, a logger is passed to services / classes where logging / debugging is required and logging levels are used to display or not display different information.
Then, if debugging is necessary, when starting the application, you can specify the logging level as a parameter and receive logs. And by default, for example, use the QUIET / NONE / OFF type level (in different loggers - in different ways). They also use a debugger
for debugging .

#
#, 2018-07-23
@mindtester

own implementation
of the logger in each class will quickly tire you .
then very convenient to use anywhere
3 - more convenient compiler options, for constructs that are clearly not needed in production

#if DEBUG
            var sw = new Stopwatch();
            sw.Start();
#endif
            /// что то делаем
#if DEBUG
            sw.Stop();
            var ts = sw.Elapsed;
            $"total time '{name_of_action}':".log();
            $"\t{ts.Hours:00}:{ts.Minutes:00}:{ts.Seconds:00}.{ts.Milliseconds:000}".log();
#endif

A
Alexander Kuznetsov, 2018-07-23
@DarkRaven

To debug an application, an integrated approach is usually used and it is dictated by the situation.
First, you can use something called a debugger to debug the behavior - set breakpoints and step through the steps to see how the monitored data changes.
Secondly, logging by levels is used to debug the application.
Logging at the TRACE level: you write everything you think is necessary, everything that will help you later understand how the process went. In practice, this is a huge number of messages, the usefulness of which in a normal situation is extremely low.
Logging at the INFO level: you write informational messages, for example, about a change in the state of a process.
And so on, through the levels.
Your logging system cuts off everything that is below an acceptable level without fixing it.
In addition to the above, for debugging, or rather, in order to be sure that everything goes exactly as it should go, tests are written. Unit tests, integration tests, UI tests, etc. And all this, taken together (logging and execution of tests), gives a relatively complete understanding that everything is going as it should, and if it doesn’t, you see that everything broke in test A, and in the logs - what exactly broke.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question