Introduction
This is a simple custom error logging class, here I mainly used in API interface development, the application mobile side of the entry record
Log parameters
/// <summary> ///Log Parameters/// </summary> Public Static classLogreq {/// <summary> ///Enter the parameter/// </summary> Public Static stringLogreqstr =""; /// <summary> ///after encrypting the entry parameter/// </summary> Public Static stringDescryptstr =""; /// <summary> ///request from a joint audience/// </summary> Public Static stringLogmintinfostr =""; }
Defining Parameters
Error log Definition Class
Public classLogclass {Private stringLogFile; PrivateStreamWriter Writer; PrivateFileStream FileStream =NULL; Private stringReqstr = Logreq.logreqstr;//App Request Entry Private stringDescryptstr = Logreq.descryptstr;//App request entry [after encryption] Private stringMediinforeq = Logreq.logmintinfostr;//The union requests the entry PublicLogclass (stringfileName) {LogFile=FileName; CreateDirectory (LogFile); } /// <summary> ///System Error Reporting/// </summary> /// <param name= "ex" ></param> Public voidExclog (Exception ex) {Try{FileInfo FileInfo=NewFileInfo (LogFile); if(!fileinfo.exists) {FileStream=fileinfo.create (); Writer=NewStreamWriter (FileStream); } Else{FileStream=Fileinfo.open (Filemode.append, FileAccess.Write); Writer=NewStreamWriter (FileStream); } writer. WriteLine ("Current Time:"+DateTime.Now); Writer. WriteLine ("after the encryption, enter the parameter:"+descryptstr); Writer. WriteLine ("Enter the parameter information:"+reqstr); Writer. WriteLine ("The Joint request entry:"+mediinforeq); Writer. WriteLine ("Exception Information:"+Ex. Message); Writer. WriteLine ("Exception object:"+Ex. Source); Writer. WriteLine ("Call stack: \ n"+Ex. Stacktrace.trim ()); Writer. WriteLine ("Trigger Method:"+Ex. TargetSite); Writer. WriteLine (); Writer. Close (); } finally { if(Writer! =NULL) {writer. Close (); Writer. Dispose (); Filestream.close (); Filestream.dispose (); } } } /// <summary> ///Custom Error Reporting/// </summary> /// <param name= "ex" ></param> Public voidExclog (Customerexception ex) {Try{FileInfo FileInfo=NewFileInfo (LogFile); if(!fileinfo.exists) {FileStream=fileinfo.create (); Writer=NewStreamWriter (FileStream); } Else{FileStream=Fileinfo.open (Filemode.append, FileAccess.Write); Writer=NewStreamWriter (FileStream); } writer. WriteLine ("Current Time:"+DateTime.Now); Writer. WriteLine ("Enter the parameter information:"+reqstr); Writer. WriteLine ("after the encryption, enter the parameter:"+descryptstr); Writer. WriteLine ("The Joint request entry:"+mediinforeq); Writer. WriteLine ("Exception Code:"+ (int) ex. Code); Writer. WriteLine ("Exception Information:"+Ex. MSG); Writer. WriteLine (); Writer. Close (); } finally { if(Writer! =NULL) {writer. Close (); Writer. Dispose (); Filestream.close (); Filestream.dispose (); } } } Public voidCreateDirectory (stringInfoPath) {DirectoryInfo DirectoryInfo=directory.getparent (InfoPath); if(!directoryinfo.exists) {directoryinfo.create (); } } }
error Log Class
Use example (Specify path)
var New Log.logclass ("e:\\website\\nanchang\\exclog\\" + DateTime.Now.ToString (" YYYYMMDD " " _log.txt " ); Log. Exclog (ex);
PS: If you need to customize the road strength, please modify the HA
Custom error Logging classes