C # writes log information to a file

Source: Internet
Author: User

In order to run the program on the server in a timely manner to track the error, you can add the program to write the log where necessary.
stringfolder =string. Format (@"D:\\{0}\\{1}", DateTime.Now.ToString ("yyyy"), DateTime.Now.ToString ("MM")); //determine if a folder exists            if(!directory.exists (folder))            {directory.createdirectory (folder); }            Try{File.writealltext (string. Format ("{0}\\{1}.txt", folder, DateTime.Now.ToString ("YYYYMMDD")),"Test information overwrites write information to the file", Encoding.UTF8); //working with text lines before writing text to a file//StreamWriter a parameter overrides by default//StreamWriter The second argument is false to overwrite the existing file, true to append the text to the end of the file                using(StreamWriter file =NewStreamWriter (string. Format ("{0}\\{1}.txt", folder, DateTime.Now.ToString ("DD")),true) ) {file. WriteLine ("The test information is appended to the file");//append directly to end of file, no line breakFile. WriteLine ("---------------------------------"); File. WriteLine ();//append directly to the end of the file, line wrapfile.                Close (); }            }            Catch(Exception) {Throw; }
C # Delete all files under the specified folder and keep folders
New DirectoryInfo (path);             if (dir. Exists)            {                = dir. GetDirectories ();                 foreach inch Childs)                {Child                    . Delete (true);                Dir. Delete (true);            

C # writes log information to a file

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.