Error handling in DOTNET

zhaozj2021-02-17  48

Recently, I made an outsourcing project for the old American, and I was forced to have a method about the wrong handling. I didn't dare to hide it, and I won it to everyone. First, all automatic error handles in the shield process do not come out: "System.Web.Services.protocols.soapexception: system.Web.Services.protocols.soapexception: The server cannot process the request." Waiting for the error page, but should be Some simple and easy-to-understand things are here to use the error handling page in Duwamish 7.0:

an Error Has Occurred

We were unable to completion your request. This failure Has Been logged with our system administrators, who are currently working to resolve the problem. We apologize for any inconvenience caused by this temporary service outage, and we appreciate your patience as we work to improve our web site. course It is also very simple to do this, you can configure this in web.config (if your error page is Error.ASPX): This configuration you can For your needs, you need to modify (such as the value of MODE). Second, write the error information to the log.

We define the structure of the error log information as follows PUBLIC STRUCT ERRORLOGITEM {public string user; // Current login public string appname; // application name public string classname; // Error class name public string functionName; // error occurred Method (Event) Name Public String Position; / / Error Position (or Other Information) Public String ErrorInfo; // Error Message Public DateTime Occurtime; / / Error Time} Error Log As shown: public class errorlog {private static ErrorLog _Instance = null; private static string strRootName; private static XmlElement xmlRoot; private static XmlDocument xmlDoc; private ErrorLog () {Load ();} private bool Load () {if CreateErrorLogFile (File.Exists (AppGlobal.ErrorLogFile)!) (AppGlobal.errorLogfile); if (xmldoc == null) xmldoc = new xmldocument (); try { xmlDoc.Load (AppGlobal.ErrorLogFile);} catch {return false;} xmlRoot = xmlDoc.DocumentElement; if (! xmlRoot = null) strRootName = "/" xmlRoot.Name "/"; return true;} private void CreateErrorLogFile (string strfilename) {StringBuilder SB = new stringbuilder (); sb.append (""); sb.append ("");

sb.Append ( ""); XmlDocument xmlDoc = new XmlDocument (); xmlDoc.LoadXml (sb.ToString ()); xmlDoc.Save (strFileName);} private void AddXmlAttribute (XmlElement xNode, string strAttr, string strAttrvalue) {XmlAttribute xAttr = (XmlAttribute) xmlDoc.CreateNode (XmlNodeType.Attribute, strAttr, null); xAttr.InnerText = strAttrvalue; xNode.Attributes.Append (xAttr);} public void SetErrorLog (ErrorLogItem errItem) {XmlElement xErrorElement = xmlDoc .CreateElement ( "error"); AddXmlAttribute (xErrorElement, "username", errItem.User); AddXmlAttribute (xErrorElement, "application", errItem.AppName); AddXmlAttribute (xErrorElement, "classname", errItem.ClassName); AddXmlAttribute (xErrorElement , "functionname", erritem.functionname); AddXMLAT tribute (xErrorElement, "position", errItem.Position); AddXmlAttribute (xErrorElement, "occurtime", errItem.OccurTime.ToString ( "g")); xErrorElement.InnerText = errItem.ErrorInfo; xmlRoot.AppendChild (xErrorElement); if ( xmlRoot.ChildNodes.Count> AppGlobal.MaxErrorLogCount) xmlRoot.RemoveChild (xmlRoot.FirstChild); xmlDoc.Save (AppGlobal.ErrorLogFile);} public static ErrorLog Instance () {if (_Instance ==

NULL) {_instance = new errorlog ();} return _instance;}} // end class We can write the information to the log by calling the setErrorLog method.

Of course, the location of the log and the number of logging error messages are also the and we obtained the following method of their value: public static string errorLogFile {get {return ConfigurationSettings.AppSettings [ "errlogfile"] Trim ();}.} public static int MaxErrorLogCount {get {return int. PARSE ("MaxerRlogcount"]. Trim ());}} I have to automatically send the error message to our mailbox (this is very important - at least this project, I can't went to the United States to debug, so that foreigners can not always tell me what went wrong) method of sending mail as follows: public static bool SendErrorLogMail (string strTo, ErrorLogItem errItem) {MailLink.Load (AppGlobal.MAIL_CFG_FILE_PATH); string strName = MailLink .GetNodeText (MailLinkItem.username); string strcode = mailink.getnodetext (MailLinkItem.password); string strFrom = MailLink.GetNodeText (MAILLINKITEM.MAILFROM); string strSubject = "Error Log"; string strSmtpServer = MailLink.GetNodeText (MAILLINKITEM.MAILSMTPSERVER); string strMailBody = "

An error occur "; strMailBody = "
login user:" Erritem.user ""; strmailbody = "
Applicatin Name:"

Erritem.Appname ""; strmailbody = "

classname:" erritem.classname ""; strmailbody = "
function name:" erritem.functionname " "; strmailbody ="
error position: " erritem.position " "; strmailbody ="
error information: " erritem.errorinfo " "; Strmailbody ="
"; strmailbody ="
"; strmailbody ="
erritem.occurtime ""; Bool Blresult = MailLink.sendmail (STRTO, STRMAILBODY, STRSUBJECT, STRMAILBODY, STRSUBJECT, STRFROM, STRNAME, STRCODE, STRSMTPSERVER); RETURN BLRESULT;} Everyone should understand that some of the specific methods can call everyone to write their own code.

Of course, also can be configured e-mail address: extraction method: public static string ErrorLogEmail {get {return ConfigurationSettings.AppSettings [ " . errorlogemail "] Trim (); method}} Finally, is a method to call a write log and mail to: public static void LogAppError (Exception thisErr, string strClass, string strFunc, string strPos, string strUser) {ErrorLogItem errItem = new ErrorLogItem (); errItem.AppName = "Your AppName"; errItem.ClassName = strClass; errItem.ErrorInfo = thisErr.ToString (); errItem.FunctionName = strFunc; errItem.OccurTime = DateTime.Now; errItem.Position = strPos; Erritem.user = struser; try {ErrorLog.instance (). setErrorLog (Erritem); SenderrorLogmail (ERRORLOGEMAIL, SB.TOSTRING ());} catch {} finally {} throw new exception ("An Error Occur:" THISERR.TOSTRING ());} So, how do you capture an exception in the program? Here is a piece of code: public dataset getPrsninfo (int Aitrx_no, int aiincid_no, int aiprsn_id) {try {ddmswspinfo.candiservice ddms = new ddmswspinfo.candiservice (); ddmscs.URL =

转载请注明原文地址:https://www.9cbs.com/read-28640.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.053, SQL: 9