. Net creates its own log class,. netlog

Source: Internet
Author: User

. Net creates its own log class,. netlog

The internship has been nearly three months now. Due to the school-enterprise joint training plan, there is no salary. Fortunately, the company has no requirements for our interns, at the beginning, I felt that I had to do something to learn quickly. However, after that, I found that the tasks assigned to us by the company were not very important and difficult.

Now we have been engaged in the design and implementation of around two or three modules. At first, we were repeatedly criticized for reading, writing, and logging files, because we didn't have the habit of recording days at external universities, this makes it difficult to quickly develop the habit of logging in key locations,

At first, I used a third-party postsharp plug-in to record my running logs. However, it was later published in code porting. If postsharp is not installed on other computers, the porting process will become complicated, in particular, it increases complexity during deployment.

Now, when implementing the new module, I decided not to use postsharp any more. I planned to write a simple log record class to record operations in key locations.

 

The code is simple and the implementation function is very simple.

You can directly copy and paste it for use. At the same time, I also provide the source code for beginners like me to download.

 

1 using System; 2 using System. IO; 3 4 namespace CustomerLog 5 {6 7 // <summary> 8 // save log 9 /// </summary> 10 public class Logging11 {12 # region log category 13/ // <summary> 14 // Save the General Log 15 /// </summary> 16 /// <param name = "message"> </param> 17 public static void writelog (string message) 18 {19 string logContent = string. format ("[{0}] => {1}", DateTime. now. toString ("yyyy-MM-dd hh: mm: ss"), message); 20 SetFil E (@ "Log.txt", logContent ); 21} 22 23 // <summary> 24 // save key logs 25 // </summary> 26 // <param name = "message"> </param> 27 public static void WriteKeylog (string message) 28 {29 var logContent = string. format ("[{0}] => {1}", DateTime. now. toString ("yyyy-MM-dd hh: mm: ss"), message); 30 SetFile (@ "KeyLog.txt", logContent ); 31} 32 33 // <summary> 34 // Save the error message log 35 // </summary> 36 // <param name = "ex"> </ param> 37 Public static void WriteBuglog (Exception ex) 38 {39 var logContent = string. format ("[{0}] error occurred at: {1}, \ r \ n content: {2}", 40 DateTime. now. toString ("yyyy-MM-dd hh: mm: ss"), ex. source, ex. message); 41 logContent + = string. format ("\ r \ n [{0}] Trace: {1}", DateTime. now. toString ("yyyy-MM-dd hh: mm: ss"), 42 ex. stackTrace); 43 SetFile (@ "BugLog.txt", logContent); 44} 45 # endregion46 47 # region general operation 48 // <summary> 49 // standardized write Cheng, after inheritance, you can customize the write content 50 /// by default, the content is saved in the Log directory 51 /// </summary> 52 /// <param name = "filename"> file name </param> 53 // <param name = "logContent"> write content </param> 54 protected static void SetFile (string filename, string logContent) 55 {56 Isexist (); // judge whether the Log directory contains 57 string errLogFilePath = Environment. currentDirectory + @ "\ Log \" + filename. trim (); 58 StreamWriter sw; 59 if (! File. exists (errLogFilePath) 60 {61 FileStream fs1 = new FileStream (errLogFilePath, FileMode. create, FileAccess. write); 62 sw = new StreamWriter (fs1); 63} 64 else65 {66 sw = new StreamWriter (errLogFilePath, true); 67} 68 sw. writeLine (logContent); 69 sw. flush (); 70 sw. close (); 71} 72 73 // determine whether the log file 74 private static void Isexist () 75 {76 string path = Environment. currentDirectory + @ "\ Log \"; 77 if (! File. Exists (path) 78 {79 Directory. CreateDirectory (path); 80} 81} 82 # endregion83} 84}

 

 

I will only use Baidu cloud disk for sharing. Could you please share it more easily]

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

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.