A simple debugging and diagnosis method under. net

Source: Internet
Author: User
In . Net Next, we can use Debug And Trace Class pairProgramTracking, breakpoint, debugging, logging, and so on, so as to quickly locate and eliminate program faults. At the same time . Net Provided Tracelistener Class allows us to easily and flexibly select the information output method: log files, event logs, and so on, but sometimes we need to track the output of debugging information in real time, just like IDE Or similar Httpwatch . For example, we put Text Control to view the output of debugging information in real time. Can I Debug And Trace Class output is directed to this control? The answer is yes. Tracelister Class, the followingCodeOutput debugging information RichTextBox Medium:

Using System;
Using System. Collections. Generic;
Using System. text;
Using System. diagnostics;
Using System. Windows. forms;

Namespace Mytracelistenertesting
{
Class Mytracelistener: tracelistener
{
Private RichTextBox _ RichTextBox =   Null ;

Public Mytracelistener (RichTextBox)
{
This. _ RichTextBox=RichTextBox;
This. Needindent= True;
}

Private   Delegate   Void Writedelegate ( String Message );
Private   Void Writeimpl ( String Message)
{
If ( This . Needindent)
{
This. Writeindent ();
This. Needindent= True;
}
This . _ RichTextBox. appendtext (Message );
This . _ RichTextBox. Select ( This . _ RichTextBox. Text. length, 0 );
This . _ RichTextBox. scrolltocaret ();
}

Public   Override   Void Write ( String Message)
{
// This is for thread safety
This . _ RichTextBox. Invoke ( New Writedelegate ( This . Writeimpl ), New   Object [] {Message} );
}

Public   Override   Void Writeline ( String Message)
{
This. Write (Message+Environment. newline );
}
}
}

 

The code above shows that the customTracelisterIt is very convenient. We can easily output log information to different target locations as needed.

The test procedure is as follows:

Using System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. Data;
Using System. drawing;
Using System. text;
Using System. Windows. forms;
Using System. diagnostics;

Namespace Mytracelistenertesting
{
Public Partial Class Form1: Form
{
Public Form1 ()
{
Initializecomponent ();
}

Private   Void F1 ()
{
Int ID = System. Threading. thread. currentthread. managedthreadid;
Debug. writeline ( " Enter F1 by thread "   + Id. tostring ());
Debug. indent ();
This . F2 ();
Debug. unindent ();
Debug. writeline ( " Leave F1 by thread "   + Id. tostring ());
}
Private   Void F2 ()
{
Int ID = System. Threading. thread. currentthread. managedthreadid;
Debug. writeline ( " Enter F1 by thread "   + Id. tostring ());
Debug. indent ();
For ( Int I =   0 ; I <   100 ; I ++ )
{
Debug. writeline ( " Working inside step " + I. tostring () + " By thread "   + Id. tostring ());
System. Threading. thread. Sleep ( 20 );
}
Debug. unindent ();
Debug. writeline ( " Leave F1 by thread "   + Id. tostring ());
}
Private   Void Button#click ( Object Sender, eventargs E)
{

This. F1 ();
}

Private   Void Form1_load ( Object Sender, eventargs E)
{
Debug. listeners. Add (NewMytracelistener (This. Richtextbox1 ));
}
}
}

Currently, it is called independently from other threads.Debug. writelineThere is no problem. You can correctly write information to the control of the interface thread, but when multiple threads callDebug. writelineThere will be deadlocks.TracelistenerIt is not clear how to deal with thread security, and further improvement is needed.

This Article is based on the following reasons: a program is too large and complex, logs need to be recorded in detail for ease of operation. At the beginning, enterpriselibrary was used. The results are indeed good and very convenient, however, it was suggested that the program should be migrated to the PDA platform, therefore, you must discard enterpriselibrary and choose other methods, after some investigation, we decided to use the built-in debug class and trace class, unfortunately, the trace class only provides assert method, that is, debugging information cannot be recorded in release , the above Code takes effect only in debug mode, but as a compromise, it basically meets the project requirements.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.