Custom Error Logging Classes

Source: Internet
Author: User

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

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.