After these basic materials, it is to consider the core of the publisher. How to analyze an exception, match an exception, then get a new exception record and release it, this class is called ExceptionManager, I don't querger the entire class All code, only some of the core code:
When an exception is processed in the publisher, the abnormality matching information of the corresponding language is first obtained according to the current language. If not, the backup matching information is obtained:
ExceptionDetAildata ExceptionDetAilds = logaccess.getexceptiondetail ();
ExceptionDetAildata.exceptiondetailrow [] exceptiondetials = (ExceptionDetAildata.exceptionDetailrow []) ExceptionDetails.exceptionDetail.select ("Language = '" Language "");
IF (ExceptionDetials.Length == 0)
{
ExceptionDetials = (ExceptionDetAildata.exceptionDetailrow []) ExceptionDetails.exceptionDetail.select ("Language Is Null);
}
Then analyze the abnormality, remove the relevant attributes in the corresponding field:
String ExceptionType = EE.GETTYPE (). TOSTRING (). Trim ();
String inMessage = "";
String Outmessage = "";
String Source = "";
String stacktrace = "";
String Targetsite = "";
String helplink = "";
String eventlever = "";
Userhelplink = "";
#Region Analysis Abnormal Attributes
IF (Ee.Message! = NULL)
{
INMESSAGE.TRIM ();
}
IF (ee.helplink! = null)
{
Helplink = ee.helplink.trim ();
}
IF (Ee.Source! = null)
{
Source = ee.source.trim ();
}
IF (EE.STACKTRACE! = NULL)
{
StackTrace = EE.STACKTRACE.TRIM ();
}
IF (ee.targetsite! = null)
{
Targetsite = ee.targetsite.tostring (). TRIM ();
}
#ndregion
Then, matching abnormal information, in order to achieve structured queries, it is a relatively stupid algorithm. After all, I think the skill here is not too important:
#REGON query exception handling
BOOL FIND = FALSE;
Foreach (ExceptionDetAildata.exceptionDetailrow ExcectionDetail in ExceptionDetials)
{
if (exceptionDetail.Type.Trim (). ToLower () == exceptionType.ToLower () &&! exceptionDetail.IsInMessage1Null () &&! exceptionDetail.IsInMessage2Null () &&! exceptionDetail.IsInMessage3Null () &&! exceptionDetail.IsInMessage4Null ()) {
String [] MathString = New String [4];
MathString [0] = ExceptionDetail.inMessage1.trim ();
MathString [1] = ExceptionDetail.inMessage2.trim ();
MathString [2] = ExceptionDetail.inMessage3.trim ();
MathString [3] = ExceptionDetail.inMessage4.trim ();
Find = MatchMessage (INMESSAGE, MATHSTRING);
IF (Find == True)
{
GetErrorMessage (Out Outmeseage, Out Eventlever, ExceptionDetail);
Break;
}
}
}
IF (Find == False)
{
......
Here, a method MatchMessage is used to determine whether the abnormal data matches:
Internal static bool matchmessage (string message, string "matchmessage)
{
FOREACH (String Match In MatchMessage)
{
IF (Message.Tolower (). Indexof (Match.tolower ()) == -1)
{
Return False;
}
}
Return True;
}
There is another way to get the output information:
Internal Static Void GetErrorMessage (Out String Outmese, Out String Eventlever, ExceptionDetails)
{
Outmessage = "";
IF (! exceptiondetail.ishelplinknull ())
{
Userhelplink = exceptiondetail.helplink.trim ();
}
Else
{
Userhelplink = "";
}
Eventlever = ExceptionDetail.EventLevel;
IF (! exceptiondetail.isoutMessagenull ())
{
CustomoutMessageData DS = logaccess.getcustomoutMestage ();
CustomOutMessageData.customoutMessageRow CustomoutMestage = NULL;
IF (DS.CUSTOMOTMESSAGE.FINDBYLANGUAGUE (Thread.currentthread.currentuicultuicultuicultuiculture.name)! = null)
{
CustomoutMessage = ds.customoutMessage.FindbyLanguage (thread.currentthread.currentuicultuicultuicultuicultuicultunicuicultuicultuicultuicultuicultuicultuicultunicuicultuicultuicultuicultuicultuicultuicultuicultuicultuicultuicultuicultuicultuicultuicultuicultunicuicultuicultuicultuicultuicultuicultuicultunicuicultuicultuicultuicultuicultuicultuicultuicultuicultuicultuicultuniculation
Else IF (DS.CUSTOMOTMESSAGE.FINDBYLANGUAGE ("default")! = null)
{
CustomOutMestage = DS.CUSTomoutMessage.FindbyLanguage ("default");
}
if (CustomoutMessage! = NULL)
{
IF (ExceptionDetail.Eventlevel.tolower () == EventLevel.Error.Tostring (). TOLOWER ())
Outmessage = CustomOutMestage.errorExceptionMestage ":" exceptiondetail.outMessage.trim () "" CustomoutMessage.ErrorpleaseDosomethingMessage;
Else IF (ExceptionDetail.Eventlevel.tolower () == Eventlevel.Failureaudit.toString (). TOLOWER ())
Outmessage = CustomoutMestage.FailureauditExceptionMessage ":" exceptiondetail.outMessage.trim () " CustomOndMestage.FailureauditPleaseDosomethingMessage;
Else IF (ExceptionDetail.Eventlevel.tolower () == Eventlevel.information.toString (). TOLOWER ())
Outmessage = CustomoutMestage ": exceptiondetail.outMessage.trim () " " CustomoutMessage.informationPleaseDosomethingMessage;
Else IF (ExceptionDetail.Eventlevel.tolower () == Eventlevel.suCcessAudit.toString (). TOLOWER ())
Outmessage = CustomoutMessage.SuccessAuditExceptionMessage ":" exceptiondetail.outMessage.trim () " CustomoutMessage.SuccessauditPleaseDosomethingMessage;
Else IF (ExceptionDetail.Eventlevel.tolower () == Eventlevel.warning.toString (). TOLOWER ())
Outmessage = CustomOutMestage.WarningexceptionMestage ":" exceptiondetail.outMessage.trim () "" CustomoutMessage.warningpleaseDosomethingMessage;}
Else
{
Outmessage = ExceptionDetail.outMessage.trim ();
}
}
}
The localization of the output information is realized, and the information head, friendly information, and the like according to the current language output.
Now, after these output information is obtained, it is necessary to record it into the log. The following two methods are written to the system log and XML file, respectively:
Write the system log:
Private static void public publishtosystemlog (ExceptionLogData.exceptionLogrow ExceptionLog)
{
IF (! EventLog.SourceExists (Appconfig.ApplicationName))
{
EventLog.createEventSource (Appconfig.ApplicationName, "storm");
}
EventLog EventLog = New EventLog ();
EventLog.source = Appconfig.ApplicationName;
String message = "";
CustomoutMessageData DS = logaccess.getcustomoutMestage ();
CustomoutMessageData.customoutmessageRow CustomoutMestage;
IF (DS.CUSTOMOTMESSAGE.FINDBYLANGUAGUE (Thread.currentthread.currentuicultuicultuicultuiculture.name)! = null)
{
CustomOutMestage = ds.customoutMessage.FindbyLanguage (thread.currentthread.currentuicultuicultuicultuicultuicultuicultuicultuicultuicultuicultuicultuicultuicultuicultuicultuicultuicultuiculture)
}
Else IF (DS.CUSTOMOTMESSAGE.FINDBYLANGUAGE ("default")! = null)
{
CustomOutMestage = DS.CUSTomoutMessage.FindbyLanguage ("default");
}
Else
{
CustomOutMessage = NULL;
}
String eventlever = exceptionLog.eventlevel.tolower ();
EventLoGENTRYTYPE LOGTYPE = EventLoGENTRYTYPE.ERROR;
IF (Eventlever == EventLoGENTRYTYPE.ERROR.TOSTOSTRING (). TOLOWER ())
{
LogType = EventLoGENTRYTYPE.ERROR;
}
Else IF (Eventlever == EventloGentrytype.Failureaudit.toString (). TOLOWER ())
{
LogType = EventLoGentrytype.Failureaudit;
}
Else if (Eventlever == EventLoGENTRYTYPE.INFORMATION.TOSTRING (). TOLOWER ())
{
LogType = EventLoGentrytype.information;
}
Else IF (Eventlever == EventLoGENTRYTYPE.SUCCESSAUDITRYTYPE.SUCCESSAUDIT.TOSTRING (). TOLOWER ())
{
Logtype = EventLoGENTRYTYPE.SUCCCESSAUDIT;
}
Else if (Eventlever == EventloGentrytype.warning.toString (). TOLOWER ())
{
LogType = EventLoGentrytype.warning;
}
if (CustomoutMessage! = NULL)
{
Message = (CustomoutMessage.LogtimeHeader ": (ExceptionLog.ISLogtimenull ()?" "" ":" YYYY-MM-DD HH: MM: SS ")) " / n ");
Message = (CustomoutMessage.exceptionTypeHeader ": (ExceptionLog.isexceptionTypenull ()?" ": eXceptionLog.exceptionType) " / n ");
Message = (CustomoutMessage.Exceer ":" (ExceptionLog.isexceptiomoriginalMessagenull ()? "": eXceptionLog.exceptiomoriginalMessage) "/ n");
Message = (CustomoutMessage.ExceptionSourceHeader ": (ExceptionLog.isexceptionSourcenull ()?" ": eXceptionLog.exceptionSource) " / n ");
Message = (CustomoutMessage.exceptionStackTraceHeader ":" (ExceptionLog.isexceptionStackTracenull ()? "": eXceptionLog.exceptionStackTrace) "/ n");
Message = (CustomoutMessage.exceptionTargetsiteHeader ":" (ExceptionLog.isexcectionTARGETSITENULL ()? "": eXceptionLog.exceptionTargetsite "/ n");
Message = (CustomoutMessage.ExceptionHelplinkHeader ": (ExceptionLog.isexceptionHelplinknull ()?" ": eXceptionLog.exceptionHelplink) " / n ");
message = (? customOutMessage.CustomMessageHeader ":" (exceptionLog.IsCustomMessageNull () "": exceptionLog.CustomMessage) "/ n"); message = (customOutMessage.CustomHelpLinkHeader ":" (exceptionLog.IsCustomHelpLinkNull ( ) "": ":": "/ n");
Message = (CustomoutMessage.userNameHeader ":" (ExceptionLog.IsuserNamenull ()? "": "": eXceptionLog.username) "/ n");
Message = (CustomoutMessage.ExceptioninduceducedPathHeader ":" (ExceptionLog.isexceptionInducedpathnull ()? "": eXceptionLog.exceptioninducedpath) "/ n");
}
Else
{
Message = ("Logtime:" ("": ":": ")) " YYY-MM-DD HH: MM: SS ") " YYY-MM-DD HH: MM: SS ")
Message = ("ExceptionType:" (ExceptionLog.isexceptionTypenull ()? "": eXceptionLog.exceptionType) "/ n");
Message = ("ExceptiomoriginalMessage:" (ExceptionLog.isexceptiomoriginalMessagenull ()? "": ": eXceptionLog.exceptiomoriginalMessage) " / n ");
Message = ("ExceptionLog.isexceptionSourcenull ()?" ": eXceptionLog.exceptionSource) " / n ");
Message = ("ExceptionStackTrace:" (ExceptionLog.isexceptionStackTRACENULL ()? "": eXceptionLog.exceptionStackTrace) "/ n");
Message = ("ExceptionTargetsite:" (ExceptionLog.isexceptionTargetsitenull ()? "": eXceptionLog.exceptionTargetsite) "/ n");
message = (? "ExceptionHelpLink:" (exceptionLog.IsExceptionHelpLinkNull () "": exceptionLog.ExceptionHelpLink) "/ n"); message = ( "CustomMessage:"? (exceptionLog.IsCustomMessageNull () "": exceptionLog .Custommessage) "/ n");
Message = ("CustomHelplink:" (ExceptionLog.iscustomhelPlinknull ()? "": exceptionLog.customHelplink) "/ n");
Message = ("UserName:" (ExceptionLog.ISUSERNAMENULL ()? "": eXceptionLog.username) "/ n");
Message = ("ExceptionInducedPath:" (ExceptionLog.isexceptionInducedpathnull ()? "":: eXceptionLog.exceptioninducedpath) "/ n");
}
EventLog.WriteEntry (Message, LogType);
}
Write the log into the XML file:
Private static void public publishtoxmlfile (ExceptionLogdata.exceptionLogrow ExceptionLog)
{
ExceptionLogData DS = logaccess.readlogfile ();
ExceptionLogData.ExceptionLogrow log = ds.exceptionLog.newexceptionLogRow ();
Log.guid = guid.newguid ();
Log.eventlevel = ExceptionLog.Eventlevel;
IF (! exceptionLog.islogtimenull ())
{
Log.logtime = exceptionLog.logtime;
}
IF (! exceptionLog.isexceptiontypenull ())
{
Log.exceptionType = exceptionLog.exceptionType;
}
IF (! exceptionLog.isexceptiomoriginalMessagenull ())
{
Log.exceptiomoriginalMessage = ExceptionLog.exceptiomoriginalMessage;
}
IF (! exceptionLog.isexceptionsource ")
{
Log.exceptionsource = exceptionLog.exceptionsource;
}
IF (! exceptionLog.isexceptionStackTracenull ())
{
Log.exceptionStackTrace = ExceptionLog.ExceptionStackTrace;
}
IF (! exceptionLog.isexceptiontargetsitenull ())
{
Log.exceptionTargetsite = exceptionLog.exceptionTargetsite;
IF (! exceptionlog.isexceptionhelplinknull ())
{
Log.exceptionhelpLink = exceptionLog.exceptionHelplink;
}
IF (! exceptionLog.iscustomMMessagenull ())
{
Log.custommessage = exceptionLog.customMMessage;
}
IF (! exceptionLog.isusernamenail ())
{
Log.username = exceptionlog.username;
}
IF (! exceptionlog.isexceptioninduceducedpathnull ())
{
Log.exceptioninducedpath = exceptionlog.exceptioninducedpath;
}
IF (! exceptionLog.iscustomhelplinknull ())
{
Log.customhelplink = exceptionLog.customHelplink;
}
DS.ExceptionLog.AddexceptionLogRow (LOG);
Logaccess.writelogfile (DS);
}
After all these things, let's see if they organize them together, when the system is abnormal, the call is this method:
///
/// Handle an exception and return an error message
///
///
Abnormality to be processed
///
Error message
Public Static String Publishexception (Exception EE)
{
String Outmessage;
String helplink;
ExceptionLogData DS = New ExceptionLogdata ();
ExceptionLogdata.exceptionLogRow ExceptionLog = ds.exceptionLog.newexceptionLogRow ();
Outmessage = Publishexception (Out Helplink, Ref ExceptionLog, EE);
IF (ExceptionSetting.ExceptionLogType == canuSeexceptionLogType.xmlfile)
{
PublishToxmlFile (ExceptionLog);
}
Else if (ExceptionSetting.ExceptionLogTyPe == canuseExceptionLogType.systemlog)
{
Publishtosystemlog (ExceptionLog);
}
Else IF (ExceptionSetting.ExceptionLogTyPe == canuseExceptionLogType.all)
{
PublishToxmlFile (ExceptionLog);
Publishtosystemlog (ExceptionLog);
}
Return Outmessage;
}
In this way, our exception publishers have been developed, let's see what records will be separate in the system log and the XML file:
Finally, you can use your exception publishers in the project, pay attention to it, you must grant the ASPNET account to write the three XML files mentioned earlier in the article to write read-write permissions.