C # create your own logging class (source code)

Source: Internet
Author: User

C # create your own logging class (source code)

When debugging and releasing a program, you often need to output and save some information. Here, you write your own logging class, which makes it easier to record information. If necessary, you can perform more extensions, such as recording exception information.

Using System; using System. IO; namespace WindowsFormsApplication1 {public static class LogerHelper {# region create log // region ///-----------------------------------------------------------------------------///Create Error Log in c: \ ErrorLog \///Record Information///
 /// ------------------------------------------------------------------------------- Public static void CreateLogTxt (string message) {string strPath; // file path DateTime dt = DateTime. now; try {strPath = Directory. getCurrentDirectory () + "\ Log"; // create a Log folder if (Directory. exists (strPath) = false) // check whether the Log Directory in the project Directory Exists. If it is true, the Directory {Directory. createDirectory (strPath); // create a Directory as a Directory object} strPath = strPath + "\" + dt. toString ("yyyy"); if (Directory. exists (strPath) = false) {Directory. createDirectory (strPath);} strPath = strPath + "\" + dt. year. toString () + "-" + dt. month. toString () + ". txt "; StreamWriter FileWriter = new StreamWriter (strPath, true); // create a log file FileWriter. writeLine ("[" + dt. toString ("yyyy-MM-dd HH: mm: ss") + "]" + message); FileWriter. close (); // Close the StreamWriter object} catch (Exception ex) {string str = ex. message. toString () ;}# endregion }}


Related Article

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.