More than 100 lines of code can be written to 0.5 million multi-thread concurrent log files and 0.5 million log files in 6 seconds.
More than 100 lines of code can be written to 0.5 million multi-thread concurrent log files in 6 seconds. log files can be separated.
Log tool code:
Using System; using System. collections. concurrent; using System. collections. generic; using System. IO; using System. linq; using System. text; using System. threading; using System. threading. tasks; namespace Utils {// <summary> // write log class // </summary> public class LogUtil {# region field public static object _ lock = new object (); public static string path = "D :\\ log"; public static int fileSize = 10*1024*102 4; // log delimiter file size private static ConcurrentQueue <Tuple <string, string> msgQueue = new ConcurrentQueue <Tuple <string, string> (); # endregion # region static constructor static LogUtil () {Thread thread = new Thread (new ThreadStart () =>{ try {int I; List <string> list; tuple <string, string> tuple; while (true) {I = 0; list = new List <string> (); while (msgQueue. tryDequeue (out tuple) & I ++ <10000) {list. add (tupl E. item1.PadLeft (8) + tuple. item2);} if (list. count> 0) {WriteFile (list, CreateLogPath ();} Thread. sleep (1) ;}} catch {}}); thread. isBackground = true; thread. start () ;}# endregion # region Write File /// <summary> // Write File /// </summary> public static void WriteFile (List <string> list, 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) {list. forEach (item = >{# region log Content string value = string. format (@ "{0} {1}", DateTime. now. toString ("yyyy-MM-dd HH: mm: ss. fff "), item); # endregion sw. writeLine (value );}); 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) {msgQueue. enqueue (new Tuple <string, string> ("[Error]", log ));} # endregion # region write operation Log // <summary> // write operation log // </summary> public static void Log (string log) {msgQueue. enqueue (new Tuple <string, string> ("[Info]", log) ;}# endregion }}View Code
Test code:
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. threading; using System. threading. tasks; using System. windows. forms; using Utils; namespace WindowsFormsApplication1 {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void Form1_Load (object sender, EventArgs e) {LogUtil. path = Application. startupPath + "\ log"; // initialize log Path} private void button#click (object sender, EventArgs e) {for (int n = 0; n <10; n ++) {Thread thread = new Thread (new ThreadStart () => {int I = 0; for (int k = 0; k <50000; k ++) {LogUtil. log (I ++ ). toString () + "response @ # $ % ^ & dabcdabcdabcdabcdabcdabcdabcd") ;}}); thread. isBackground = true; thread. start ();}}}}View Code
Test: