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 }}