Some programs can be difficult to grasp when debugging breakpoints (such as service programs), some programs need to cycle countless times, to see the results of each or every time, and so on.
Let's write a simple log program, TXT file generated in the Debug directory
Using System; Using System.Collections.Generic; Using System.Text; Using System.IO; Using System.Windows.Forms; Namespace Common {public class Log {private string logFile; private StreamWriter writer; private FileStream FileStream = Null Public Log (String fileName) {logFile = filename; CreateDirectory (LogFile); public void log (string info) {try {System.IO.FileInfo FileInfo = new System.IO.FileInfo (logFile); if (!fileinfo.exists {FileStream = Fileinfo.create (); writer = new StreamWriter (fileStream);} else {fileStream = Fileinfo.open (Filemode.ap Pend, FileAccess.Write); writer = new StreamWriter (FileStream); } writer. WriteLine (DateTime.Now + ":" + info);} finally {if (writer!= null) {writer. Close (); Writer. Dispose (); Filestream.close (); Filestream.dispose (); }} public void CreateDirectory (string infoPath) {DirectoryInfo DirectoryInfo = Directory.getparent (InfoPath); if (!dir ectoryinfo.exists) {directoryinfo.create ();}} } }
Time to use:
Log log = new log (AppDomain.CurrentDomain.BaseDirectory + @ "/log/log.txt");
Log.log ("content");