When debugging and publishing a program, it is often necessary to save some information output, here write a own log record class, record information more convenient. You can also do more extensions if you need to, such as logging exception information.
Using system;using system.io;namespace windowsformsapplication1{public static class Logerhelper {#region Gen Build Log///-----------------------------------------------------------------------------//<summary> Create error Log in c:\errorlog\</summary>//<param name= "message" > Record information </param>//<returns></ returns>//-----------------------------------------------------------------------------public static V OID Createlogtxt (String message) {string strpath; The path of the file DateTime dt = DateTime.Now; try {strpath = directory.getcurrentdirectory () + "\\Log"; WinForm Project \bin\ Directory Create log folder if (Directory.Exists (strpath) ==false)//project directory under the log directory ' If the record exists, true does not have this directory {directory.createdirectory (strpath); Create a DirectoryDirectory 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 the log file Filewriter.writeline ("[" + dt. ToString ("Yyyy-mm-dd HH:mm:ss") + "]" + message); Filewriter.close (); Close StreamWriter Object} catch (Exception ex) {string Str=ex. Message.tostring (); }} #endregion}}