. NET creates its own log class

Source: Internet
Author: User

Internship has been nearly three months, because it is the school-enterprise joint training program, so there is no salary, fortunately, the company for us these interns do not have any requirements, just at the beginning I still feel to do something to learn quickly, but later, found that the company assigned to our task is not urgent, also not too difficult.

Up to now about 2, 3 modules of the design and implementation, at first, in the file read and write and record logs repeatedly received criticism, because the external university time and did not record the habit of day, making this time to develop in the key location of the habit of recording the log has become somewhat difficult,

At first I used Postsharp's third-party plug-in to record my own running logs, but later found that in code porting, if the other computers did not install POSTSHARP, it would complicate the porting process, especially when deploying.

Now that the new module is being used, it is decided not to use Postsharp, and to write a simple logging class to record operations at the critical location.

No nonsense, directly out of the code, the code is relatively simple, the implementation of the function is very simple.

Can be directly copied and pasted to use, at the same time, I also provide the source code for beginners like me to download.

1 usingSystem;2 usingSystem.IO;3 4 namespaceCustomerlog5 {6 7     /// <summary>8     ///Save Log9     /// </summary>Ten      Public classLogging One     { A         #regionLog classification -         /// <summary> -         ///Save normal log the         /// </summary> -         /// <param name= "message" ></param> -          Public Static voidWritelog (stringmessage) -         { +             stringLogcontent =string. Format ("[{0}] =>{1}", DateTime.Now.ToString ("YYYY-MM-DD Hh:mm:ss"), message); -Setfile (@"Log.txt", logcontent); +         } A  at         /// <summary> -         ///Saving critical logs -         /// </summary> -         /// <param name= "message" ></param> -          Public Static voidWritekeylog (stringmessage) -         { in             varLogcontent =string. Format ("[{0}]=>{1}", DateTime.Now.ToString ("YYYY-MM-DD Hh:mm:ss"), message); -Setfile (@"KeyLog.txt", logcontent); to         } +  -         /// <summary> the         ///Save error message Log *         /// </summary> $         /// <param name= "ex" ></param>Panax Notoginseng          Public Static voidWritebuglog (Exception ex) -         { the             varLogcontent =string. Format ("[{0}] error occurred in: {1},\r\n content: {2}", +DateTime.Now.ToString ("YYYY-MM-DD Hh:mm:ss"), ex. Source, ex. Message); ALogcontent + =string. Format ("\ r \ n [{0}] trace: {1}", DateTime.Now.ToString ("YYYY-MM-DD Hh:mm:ss"), the Ex. StackTrace); +Setfile (@"BugLog.txt", logcontent); -         } $         #endregion $  -         #regionGeneral operations -         /// <summary> the         ///standardize the write process, which can be customized after inheritance -         ///saved by default in the log directory of the debug directoryWuyi         /// </summary> the         /// <param name= "filename" >file name</param> -         /// <param name= "Logcontent" >Write Content</param> Wu         protected Static voidSetfile (stringFileNamestringlogcontent) -         { AboutIsexist ();//determine if the log directory exists $             stringErrlogfilepath = Environment.currentdirectory +@"\log\"+filename. Trim (); - StreamWriter SW; -             if(!file.exists (errlogfilepath)) -             { AFileStream FS1 =NewFileStream (Errlogfilepath, FileMode.Create, FileAccess.Write); +SW =NewStreamWriter (FS1); the             } -             Else $             { theSW =NewStreamWriter (Errlogfilepath,true); the             } the SW. WriteLine (logcontent); the SW. Flush (); - SW. Close (); in         } the  the         //determine if a log file exists About         Private Static voidisexist () the         { the             stringPath = Environment.currentdirectory +@"\log\"; the             if(!file.exists (path)) +             { - directory.createdirectory (path); the             }Bayi         } the         #endregion the     } -}

↓↓↓↓↓↓↓↓↓↓ "At present will only use the Baidu cloud disk share, ask the great God Professor more useful sharing way"

Http://pan.baidu.com/s/1kTNcfdH

. NET creates its own log class

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.