Record logs (log processing), record log processing
1. Call methods:
By classifying logs, you can quickly locate errors and quickly solve problems in the project, which greatly improves efficiency and facilitates the overall work.
Private string DateDiff (DateTime DateTime1, DateTime DateTime2)
{String dateDiff = null;
TimeSpan ts = DateTime1.Subtract (DateTime2 ). duration (); dateDiff = ts. days. toString () + "day" + ts. hours. toString () + "Hour" + ts. minutes. toString () + "Minute" + ts. seconds. toString () + "seconds ";
Return dateDiff;
}
(1) The DateTime Value Type represents a specific date time between 00:00:00 on January 1, January 1, 0001 AD and on January 1, December 31, 9999 AD. Therefore, you can use the DateTime value type to describe any time within the expected range.
A DateTime value represents a specific time;
(2) The TimeSpan value contains many attributes and methods for accessing or processing a TimeSpan value.
The following list covers some of them:
Add: Add to another TimeSpan value; Days: return the TimeSpan value calculated by the number of Days. Duration: obtains the absolute value of TimeSpan;
Hours: returns the TimeSpan value calculated in Hours; Milliseconds: returns the TimeSpan value calculated in Milliseconds; Minutes: returns the TimeSpan value calculated in Minutes;
Negate: returns the opposite number of the current instance; Seconds: returns the TimeSpan value calculated in Seconds; Subtract: minus another TimeSpan value;
Ticks: the number of tick returned for the TimeSpan value; TotalDays: the number of days indicated by the TimeSpan value; TotalHours: the number of hours represented by the TimeSpan value;
TotalMilliseconds: returns the number of milliseconds in the TimeSpan value. TotalMinutes: returns the number of minutes in the TimeSpan value. TotalSeconds: returns the number of seconds in the TimeSpan value;
1 # region log processing 2 3 Console. writeLine (AppDomain. currentDomain. baseDirectory + "InsureLog \" + "Log Test System"); 4 string strLog = "4d5fe410-1cc6-4e88-97fe-e66a6868e3fb ^ 57.7 | BE7CCB47-2A09-4BCD-BBB4-8F3604B4226F ^ 229 | E2E352FA-A4A5-4DD5-B545-D5FD5274F220 ^ 4.90 | "; 5 string strSysName = "Log Test System"; 6 string strEndTime = DateTime. now. toString ("yyyy-MM-dd HH: mm: ss. fff "); 7 // insurance system name, log type, log Content, transaction parameters, transaction end time 8 LogProcess. recordLog (strSysName, "1", strLog, "222", strEndTime); // 10:12:13. 001 9 Console. writeLine (strEndTime); 10 # endregion log processing 11 string str = strEndTime. substring (strEndTime. length-3); // truncate the last three digits of the string 12 string str1 = strEndTime. substring (0, strEndTime. length-4); // number from right to left, delete four character strings 13 Console. writeLine (str + "|" + str1); 14 15 16 DateTime str2 = Convert. toDateTime (DateTime. now. toString ("yyyy-MM-dd HH: mm: ss. fff "); 17 DateTime str3 = Convert. toDateTime ("15:06:07. 001 "); 18 TimeSpan ts = str2-str3; 19 string str4 = ts. milliseconds. toString (); 20 Console. writeLine ("test time:" + str4 + "millisecond"); 21 22 23 Console. readKey ();
2. method subject:
1 using System; 2 using System. collections. generic; 3 using System. IO; 4 using System. linq; 5 using System. text; 6 using System. windows. forms; 7 8 namespace TestPublicLog 9 {10 public class LogProcess 11 {12 private static string strStartTime = "15:13:01. 001 "; // global variable, log start time 13 14 /// <summary> 15 /// record log processing 16 /// </summary> 17 /// <param name = "strSysName"> Insurance system name [cannot be blank] </param> 18 // <pa Ram name = "logType"> Log Type: 1 indicates normal log, 0 indicates that the exception log [cannot be blank] </param> 19 // <param name = "logContent"> log Content [cannot be blank] </param> 20 // <param name = "tradeParameter"> transaction parameter [null] (store important input parameters) </param> 21 // <param name = "logTime"> record time (accurate to ms), in the format of yyyy-MM-dd HH: mm: ss. fff [null] </param> 22 public static void RecordLog (string strSysName, string logType, string logContent, string tradeParameter, string logTime) 23 {24 try 25 {26 string st RComPath = AppDomain. currentDomain. baseDirectory + "InsureLog \" + strSysName; // path public part 27 string normal_Path = strComPath + "\ normal log "; // normal log storage path 28 string abnormal_Path = strComPath + "\ exception log"; // exception log storage path 29 30 if (string. isNullOrEmpty (logType) 31 {32 MessageBox. the Show ("log status parameter" or "type parameter" input is blank! "," Failed to call the medical insurance log Writing Method: ", MessageBoxButtons. OK, MessageBoxIcon. asterisk); 33 return; 34} 35 else 36 {37 if (logType = "0") 38 {39 // call the log file creation method, input different log paths 40 GetLog (logType, strSysName, normal_Path, logTime, tradeParameter, logContent); // write log 41 GetLog (logType, strSysName, abnormal_Path, logTime, tradeParameter, logContent); // write log 42} 43 else if (logType = "1") 44 {45 GetLog (logType, str SysName, normal_Path, logTime, tradeParameter, logContent); // write log 46} 47 else 48 {49 MessageBox. Show ("log [type parameter] input error! "," Failed to call the medical insurance log Writing Method: ", MessageBoxButtons. OK, MessageBoxIcon. asterisk); 50 return; 51} 52} 53} 54 catch (Exception ex) 55 {56 MessageBox. show ("failed to write medical insurance logs:" + ex. toString (), "runtime error", MessageBoxButtons. OK, MessageBoxIcon. asterisk ); 57} 58} 59 60 # region create a log file 61 // <summary> 62 // create a log file 63 /// </summary> 64 // <param name = "logType"> Log Type </param> 65 // <param name = "strSysName"> Insurance System name </param> 66/ // <Param name = "m_path"> Log Path </param> 67 // <param name = "logTime"> log recording time </param> 68 /// <param name = "tradeParameter"> transaction parameter </param> 69 // <param name = "logContent"> log content </param> 70 public static void GetLog (string logType, string strSysName, string m_path, string logTime, string tradeParameter, string logContent) 71 {72 try 73 {74 string fileName = "LogRecord _" + DateTime. now. toString ("yyyy-MM-dd") + ". Txt"; 75 DirectoryInfo m_DirInfo = new DirectoryInfo (m_path); 76 if (! M_DirInfo.Exists) 77 {78 Directory. createDirectory (m_path); // create a folder 79} 80 FileStream fs = new FileStream (m_path + "\" + fileName, FileMode. append, FileAccess. write); 81 StreamWriter sw = new StreamWriter (fs); // file stream 82 if (string. isNullOrEmpty (logTime) 83 {84 // obtain the start time (accurate to ms milliseconds) 85 strStartTime = DateTime. now. toString ("yyyy-MM-dd HH: mm: ss. fff "); 86 if (string. isNullOrEmpty (tradeParameter) 87 {8 8 sw. writeLine (DateTime. now. toString ("yyyy-MM-dd HH: mm: ss") + "\ r \ n" + "log Content:" + logContent + "\ r \ n "); // obtain the server time 89} 90 else 91 {92 sw during official use. writeLine (DateTime. now. toString ("yyyy-MM-dd HH: mm: ss") + "\ r \ n" + "log Content: "+ logContent +" \ r \ n "+" transaction parameter: "+ tradeParameter +" \ r \ n "); // when used officially, get server time 93} 94} 95 else 96 {97 string getTimeDiffer = GetTimeDiffer (strStartTime, logTime); 98 if (getTimeDiffer = "0") 99 {100 MessageBox. Show ("failed to obtain transaction time consumption! "); 101} 102 else103 {104 if (string. isNullOrEmpty (tradeParameter) 105 {106 sw. writeLine (DateTime. now. toString ("yyyy-MM-dd HH: mm: ss") + "" + "transaction time:" + getTimeDiffer + "\ r \ n" + "log Content: "+ logContent +" \ r \ n "); 107} 108 else109 {110 sw. writeLine (DateTime. now. toString ("yyyy-MM-dd HH: mm: ss") + "" + "transaction time:" + getTimeDiffer + "\ r \ n" + "log Content: "+ logContent +" \ r \ n "+" transaction parameter: "+ tradeParameter +" \ r \ n "); 111} 112} 113} 114 sw. close (); 115 fs. close (); 116} 117 catch (Exception ex) 118 {119 MessageBox. show ("log creation error:" + ex. toString (), "failed to call the [create log file] method:", MessageBoxButtons. OK, MessageBoxIcon. asterisk); 120} 121} 122 # endregion Log File Creation end 123 124 # region resolution time difference (computing time consumption) 125 /// <summary> 126 // resolution time difference (computing time consumed) 127 /// </summary> 128 // <param name = "strStartTime"> Start time </param> 129 /// <param name = "strEndTime"> End Time, that is, the input time </param> 130 // <returns> </returns> 131 public static string GetTimeDiffer (string strStartTime, string strEndTime) 132 {133 try134 {135 // Convert string type to DateTime type 136 DateTime strTimeStart = Convert. toDateTime (strStartTime); 137 DateTime strTimeEnd = Convert. toDateTime (strEndTime); 138 string dateDiff = null; // defines the time difference of 139 // defines the time interval of 140 TimeSpan startTime = new TimeSpan (strTimeStart. ticks); 141 TimeSpan endTime = new TimeSpan (strTimeEnd. ticks); 142 TimeSpan ts = endTime. subtract (startTime ). duration (); 143 // display time 144 // ts. days. toString () + "day" + ts. hours. toString () + "Hour" + ts. minutes. toString () + "Minute" 145 dateDiff = ts. seconds. toString () + "seconds" + ts. milliseconds. toString () + "millisecond"; 146 return dateDiff; 147} 148 catch (Exception ex) 149 {150 MessageBox. show ("failed to get the time difference:" + ex. toString (), "runtime error", MessageBoxButtons. OK, MessageBoxIcon. asterisk); 151 return "0"; 152} 153 154} 155 # End of endregion resolution time difference (computing time consumed) 156} 157}