Original http://blog.csdn.net/hejialin666/article/details/6106648
Some programs in debugging difficult to catch breakpoints (such as service programs), some programs need to loop 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 ;p ublic 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.append, 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); directoryinfo.exists) {directoryinfo.create ();}}}}
When used:
Log log = new log (AppDomain.CurrentDomain.BaseDirectory + @ "/log/log.txt");
Log.log ("content");
C # Simple write a log in txt, debugging is useful