Stopwatch + C # log printing method,

Source: Internet
Author: User

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;

Related Article

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.