C # Help class for writing text logs (multithreading supported) Release edition (not applicable to ASP. NET programs ),
The automatic collection mechanism of iis does not apply to ASP. NET programs.
Code:
Using System; using System. collections. concurrent; using System. configuration; using System. IO; using System. text; using System. threading; using System. threading. tasks; namespace CommonDll {// <summary> /// write log class // </summary> public class LogUtil {# region field public static string path = ConfigurationManager. deleetask[ "LogPath"]; public static int fileSize = 10*1024*1024; // the size of the log-separated file is private static. ConcurrentQueue <Tuple <string, DateTime> queue = new ConcurrentQueue <Tuple <string, DateTime> (); # endregion # region constructor static LogUtil () {Task. factory. startNew (new Action (delegate () {StringBuilder log; string path; Tuple <string, DateTime> tuple; string item; while (true) {log = new StringBuilder (); path = CreateLogPath (); while (queue. tryDequeue (out tuple) {item = string. format (@ "{0} {1}", t Uple. item2.ToString ("yyyy-MM-dd HH: mm: ss. fff "), tuple. item1); log. appendFormat ("\ r \ n {0}", item) ;}if (log. length> 0) WriteFile (log. toString (2, log. length-2), path); Thread. sleep (100 );}}));} # endregion # region Write File // <summary> // Write File // </summary> public static void WriteFile (string log, string path) {try {if (! Directory. Exists (Path. GetDirectoryName (path) {Directory. CreateDirectory (Path. GetDirectoryName (path);} if (! File. exists (path) {using (FileStream fs = new FileStream (path, FileMode. create) {fs. close () ;}} using (FileStream fs = new FileStream (path, FileMode. append, FileAccess. write) {using (StreamWriter sw = new StreamWriter (fs) {sw. writeLine (log); sw. flush ();} fs. close ();}} catch {}# endregion # path of the log file generated by region /// <summary> /// path of the generated log file /// </summary> public static string CreateLogPath (){ Int index = 0; string logPath; bool bl = true; do {index ++; logPath = Path. combine (path, "Log" + DateTime. now. toString ("yyyyMMdd") + (index = 1? "": "_" + Index. toString () + ". txt "); if (File. exists (logPath) {FileInfo fileInfo = new FileInfo (logPath); if (fileInfo. length <fileSize) {bl = false ;}} else {bl = false ;}} while (bl); return logPath ;} # endregion # region write error log // <summary> // write error log // </summary> public static void LogError (string log) {queue. enqueue (new Tuple <string, DateTime> ("[Error]" + log, DateTime. now) ;}# endregion # region write operation Log /// <summary> /// write operation log /// </summary> public static void Log (string log) {queue. enqueue (new Tuple <string, DateTime> ("[Info]" + log, DateTime. now) ;}# endregion }}View Code
Test code:
Private void button#click (object sender, EventArgs e) {int n = 10000; DateTime dtStart = DateTime. now; for (int I = 1; I <= n; I ++) {ThreadPool. queueUserWorkItem (new WaitCallback (delegate (object obj) {int j = (int) obj; LogUtil. log ("test" + j. toString ("00000"); if (j = n) {double sec = DateTime. now. subtract (dtStart ). totalSeconds; MessageBox. show (n + "logs completed, time consumed" + sec. toString ("0.000") + "seconds") ;}}, I );}}View Code
: