C # Logging class creation of source code sharing

Source: Internet
Author: User
Tags directory create
When debugging and publishing a program, it is often necessary to save some information output, here write a own log record class, record information more convenient. You can also do more extensions if you need to, such as logging exception information.

Using system;using system.io;namespace windowsformsapplication1{public static class Logerhelper {#region Gen Build Log///-----------------------------------------------------------------------------//<summary> Create error Log in c:\errorlog\</summary>//<param name= "message" > Record information </param>//<returns></ returns>//-----------------------------------------------------------------------------public static V                                                   OID Createlogtxt (String message) {string strpath;            The path of the file DateTime dt = DateTime.Now;          try {strpath = directory.getcurrentdirectory () + "\\Log"; WinForm Project \bin\ Directory Create log folder if (Directory.Exists (strpath) ==false)//project directory under the log directory '                       If the record exists, true does not have this directory {directory.createdirectory (strpath); Create a DirectoryDirectory object} strpath = strpath + "\ \" + dt.                ToString ("yyyy"); if (directory.exists (strpath) = = False) {Directory.CreateDirectory (                               strpath); } strpath = strpath + "\ \" + dt. Year.tostring () + "-" + dt.                                 Month.tostring () + ". txt";           StreamWriter filewriter= New StreamWriter (strpath, true); Create the log file Filewriter.writeline ("[" + dt.                 ToString ("Yyyy-mm-dd HH:mm:ss") + "]" + message);                                                 Filewriter.close (); Close StreamWriter Object} catch (Exception ex) {string Str=ex.            Message.tostring (); }} #endregion}}
Related Article

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.