C # simple to txt to write the log, debugging is useful

Source: Internet
Author: User
Tags log log

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");

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.