Share a logging class that can be used in multiple threads ., Log Multithreading
I haven't written a blog for a long time. Today I share my own log class, which is not original. I borrowed a previous idea and modified it.
Logs are required. Now the program is multi-threaded and concurrent, and the log may be faulty. lock? Performance is affected. Log4net is too heavyweight. This log is a lightweight tool.
Let's talk about the source code:
1 using System; 2 using System. collections. generic; 3 using System. IO; 4 using System. text; 5 6 namespace GEDU. courseOnline. common 7 {8 // <summary> 9 // log class (multi-threaded version) 10 /// </summary> 11 public class LogHelper 12 {13 private string _ fileName; 14 private static Dictionary <long, long> lockDic = new Dictionary <long, long> (); 15 16 /// <summary> 17 /// get or set the file name 18 /// </summary> 19 public string Fil EName 20 {21 get {return _ fileName;} 22 set {_ fileName = value ;} 23} 24 25 /// <summary> 26 /// constructor 27 /// </summary> 28 /// <param name = "fileName"> full file path name </param> 29 public LogHelper (string fileName) 30 {31 if (string. isNullOrEmpty (fileName) 32 {33 throw new Exception ("FileName cannot be blank! "); 34} 35 Create (fileName); 36 _ fileName = fileName; 37} 38 39 // <summary> 40 // create a file path 41 // </summary> 42 // <param name = "fileName"> file path </param> 43 public void Create (string fileName) 44 {45 var directoryPath = Path. getDirectoryName (fileName); 46 if (string. isNullOrEmpty (directoryPath) 47 {48 throw new Exception ("FileName Path error! "); 49} 50 if (! Directory. exists (directoryPath) 51 {52 Directory. createDirectory (directoryPath ); 53} 54} 55 56 // <summary> 57 // write text 58 // </summary> 59 // <param name = "content"> text content </param> 60 // <param name = "newLine"> line feed mark </param> 61 private void Write (string content, string newLine) 62 {63 using (FileStream fs = new FileStream (_ fileName, FileMode. openOrCreate, FileAccess. readWrite, FileShare. readWrit E, 8, FileOptions. asynchronous) 64 {65 Byte [] dataArray = Encoding. UTF8.GetBytes (content + newLine); 66 bool flag = true; 67 long slen = dataArray. length; 68 long len = 0; 69 while (flag) 70 {71 try 72 {73 if (len> = fs. length) 74 {75 fs. lock (len, slen); 76 lockDic [len] = slen; 77 flag = false; 78} 79 else 80 {81 len = fs. length; 82} 83} 84 catch (Exception ex) 85 {86 while (! LockDic. containsKey (len) 87 {88 len + = lockDic [len]; 89} 90} 91} 92 fs. seek (len, SeekOrigin. begin); 93 fs. write (dataArray, 0, dataArray. length); 94 fs. close (); 95} 96} 97 98 // <summary> 99 // Write File content 100 // </summary> 101 // <param name = "content"> content </param> 102 public void WriteLine (string content) 103 {104 this. write (content, Environment. newLine ); 105} 106 107 // <summary> 108 // write the file content without wrapping 109 /// </summary> 110 // <param name = "content"> content </param> 111 public void Write (string content) 112 {113 this. write (content, ""); 114} 115} 116}View Code
Usage:
1 string strPath = HttpContext. current. server. mapPath (string. format ("/Log/{0: yyyymmdd.txt", DateTime. today); 2 // string strPath = string. format (@ "D: \ Log \ {0: yyyymmdd.txt", DateTime. today); 3 LogHelper logHelper = new LogHelper (strPath); 4 logHelper. writeLine (sWord + "time:" + DateTime. now );
If you have any questions, please advise.