C # write error messages to logs

Source: Internet
Author: User

Create a folder in the application, name it errorlog, and then execute the following program

 

Code

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. IO;

Namespace consoleapplication1
{
Class Program
{
Static void main (string [] ARGs)
{
// Create an error file directory
String Path = directory. getcurrentdirectory ();
Directoryinfo A = directory. getparent (PATH );
A = directory. getparent (A. fullname );
String [] dirs = directory. getdirectories (A. fullname, "errorlog ");
String Pa = dirs [0] + "\" + datetime. Now. year. tostring () + "\" + datetime. Now. Month. tostring ();
String log = pa + "\" + datetime. Now. tow.datestring () + ". txt ";
/* Check and create a folder */
If (! Directory. exists (PA ))
{
Directory. createdirectory (PA );
}
/* Check and create a log record */
If (! File. exists (log ))
File. Create (log). Close ();

// Write the following error information to the directory
Using (streamwriter W = file. appendtext (log ))
{
W. writeline ("error time: {0}", "123 ");
W. writeline ("__________________________");
W. Flush ();
W. Close ();
}
}
}
}

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.