XNa advanced programming: Xbox 360 and Windows 3-3

Source: Internet
Author: User
Tags log4net
3.3Record error information

DebuggingCodeIt is very complicated, especially when the rendering loop goes wrong, but you don't get any exceptions. It is not enough to set only a few debugging breakpoints, especially when the game fails to run for a period of time, debugging is not a proper choice. You may want to know what happened to each frame, but you do not want to track it step by step. For such problems, you can output some information to the console.Visual StudioAnd the information will be lost when the project is started next time.

In a relatively large project I have done, a class is very important, that is Log Class, which records messages, warnings, errors, or debugging information in a text file. This class is short and simple, but if you use it correctly, it will bring a lot of fun for debugging and testing. There are also many more advanced logging classes and frameworks ( Logging classes and
Frameworks ) Can be used, such Log4net , You can get: Http://logging.apache.org/log4net
. The log is not just a few lines of information, you can use WebService To remotely obtain users and use your applications.ProgramCan be activated. Windows Error events can also be used to perform many other tasks. This book has not been discussed much, because it is a very complicated topic. For simple games in this book, use Log Class is enough.

Take a lookLogClass implementation (inBreakoutThe game has a more complex version ):

Public   Class Log
{
# Region Variables
Private   Static Streamwriter =   Null ;
Private   Const   String Logfilename =   " Log.txt " ;
# Endregion


It usesLog.txtFile to store all messages and use a staticStreamwriterObject To facilitate access to static methods. When this class is called for the first time, it will be initialized through its static constructor:

# Region Static constructor to create log file
Static Log ()
{
// Open File
Filestream File =   New Filestream (
Logfilename, filemode. openorcreate,
Fileaccess. Write, fileshare. readwrite );
Writer =   New Streamwriter (File );
// Go to end of File
Writer. basestream. Seek ( 0 , Seekorigin. End );
// Enable auto flush (always be up to date when reading !)
Writer. autoflush =   True ;
// Add some info about this session
Writer. writeline ( " /// Session started: "   +
Stringhelper. writeisodateandtime (datetime. Now ));
} // Log ()
# Endregion

enumeration value fileshare. readwrite make sure that files are read and written from outside when the game is running. In addition to writer is set to the end of the file, enable attributes autoflush make sure that the new data is immediately written into the log file, and then add the start time of an operation. The auxiliary class stringhelper .

Finally, it is the most important of this class and the only method you will always call:

# Region Write log entry
Static   Public   Void Write ( String Message)
{
Datetime CT = Datetime. now;
String S =   " [ "   + Ct. Hour. tostring ( " 00 " ) +   " : "   +
Ct. Minute. tostring ( " 00 " ) +   " : "   +
Ct. Second. tostring ( " 00 " ) +   " ] "   +
Message;
Writer. writeline (s );
# If Debug
// In debug mode write that message to the console as well!
System. Console. writeline (s );
# Endif
} // Write (Message)
# Endregion

First, add a timestamp before the message, and then write the messageLog.txtFile. If you are debugging the project, output the message to the console. Now, you canBreakoutThe log function is added to the game.Log.txtAdd an update message at the end of the file, as shown below:

Log. Write ( " Level "   + Level +   " Completed. " );

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.