Auxiliary class -- Record error information

Source: Internet
Author: User
Tags log4net
Record error information

Debug gamesCodeIt may be very complicated, especially if you don't get any exceptions, but some rendering loops have errors. Setting only a few breakpoints is not enough, especially if the game encounters an error after running for a period of time, debug is not the right option. You want to know what each frame is running, but you do not want to gradually discover it through 500 frames. For such problems, you can only throw some text to the console, but this can only be used in Visual Studio, and all the console content will be lost when you start the project next time.

Among all the large projects I have done, one of the most important classes is the log class, which only writes messages, warnings, errors, or debug texts to a simple text file. This class is short and simple, but if you use it in the correct way, it will give you a more pleasant debugging and testing session. In addition, there are more advanced logging classes and frameworks (such as log4net) available, you can find them at http://logging.apache.org/log4net. Logs not only write several lines to text files. From ApplicationProgramLog Data is often used to remotely obtain user errors. With a WebService, You can activate Windows Error events and perform many other tasks. These are not covered in this book, because this is a very complex topic. For simple games in this book, it should be enough to use the log class.

First take a look at the log class (a more complex version can be found in the breakout game ):

 
Public class log {# region variables Private Static streamwriter writer = NULL; private const string logfilename = "log.txt"; # endregion

The consumer uses a log.txt file to store all messages and uses a static streamwriter object to facilitate access in static methods.

# Region static constructor to create log filestatic 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 at:" + stringhelper. writeisodateandtime (datetime. now);} // log () # endregion

When the game is running, the enumerated value fileshare. readwrite ensures that you can always read and write files from outside. In addition, to set the writer to the end of the file, the autoflush attribute ensures that new data is saved to the log file immediately, and a little text is added to indicate that the session has started. For timestamp, you will use an auxiliary method of the stringhelper class, and you will immediately learn this class.

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

# Region write log entrystatic 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 simple timestamp before the message. After the message is written to the log.txt file, if the project is in debug mode, the message is also output to the console. Now, only the following code line is passed. When you complete a break of the second chapter of the breakoutgame in every region, you add a new line to the log.txt file:

 
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.