Stopwatch + C # log printing method,
It is easy to print the running time of an interface and method in a program. Now, I will share a method for temporarily printing logs I use at work and how to use Stopwatch printing to measure the running time at a certain interval.
The Stopwatch instance can measure the running time of a time interval. The following examples are commonly used:
Reference namespace: using System. Diagnostics;
Stopwatch // Stopwatch instance
Start; // Start or continue to measure the running time at a certain interval
Elapsed; // obtain the total running time measured by the current instance (in minutes and seconds)
ElapsedMilliseconds; // obtain the total running time measured by the current instance (in milliseconds)
Reset; // stop interval measurement, and Reset the running time to zero
Restart; // stop interval measurement, reset the running time to zero, and start measuring the running time
The log printing method. You can specify the directory as follows:
1 public static void WriteError (string message) 2 {3 string path = AppDomain. currentDomain. baseDirectory; // obtain the base Directory 4 path = System. IO. path. getDirectoryName (path) + "\ ErrorLogs"; // set the output log directory to 5 try 6 {7 if (! System. IO. directory. exists (path) 8 {9 System. IO. directory. createDirectory (path); 10} 11 string fileName = System. IO. path. combine (path, DateTime. now. toString ("yyyy-MM-dd") + ". log "); 12 System. IO. streamWriter sw = new System. IO. streamWriter (fileName, true); // file stream creation write data stream 13 sw. writeLine (DateTime. now. toString ("HH: mm: ss: fff") + "-------------------"); 14 sw. writeLine (message); 15 sw. writeLine (); 16 sw. close (); // Close write data stream 17} 18 catch19 {20} 21}
Use Stopwatch to print and measure the running time at a certain interval
1 var s = new Stopwatch (); 2 // code 3 WriteError (s. ElapsedMilliseconds. ToString () + "GetNewUpdateFileList") to test the running time; // print the running time
4 s.Reset();
Note: The method is the method used by Xiaoqi in his work. If it is reproduced, please indicate the source;
You are welcome to point out any mistakes or omissions;
Welcome to comments;