2012-04-03 1 views
0

J'ai du code (on le trouve sur Internet) pour gérer les erreurs d'application. J'ai écrit dans le journal des événements.Traitement des erreurs au niveau de l'application

void Application_Error(object sender, EventArgs e) 
     { 
      Exception myError = null; 

      if (HttpContext.Current.Server.GetLastError() != null) 
      { 
       string eventLog = "MySite"; 
       string eventSource = "www.mysite.com"; 
       string myErrorMessage = ""; 

       myError = Server.GetLastError(); 

       while (myError.InnerException != null) 
       { 
        myErrorMessage += "Message\r\n" + 
         myError.Message.ToString() + "\r\n\r\n"; 
        myErrorMessage += "Source\r\n" + 
         myError.Source + "\r\n\r\n"; 
        myErrorMessage += "Target site\r\n" + 
         myError.TargetSite.ToString() + "\r\n\r\n"; 
        myErrorMessage += "Stack trace\r\n" + 
         myError.StackTrace + "\r\n\r\n"; 
        myErrorMessage += "ToString()\r\n\r\n" + 
         myError.ToString(); 

        myError = myError.InnerException; 
       } 

       if (EventLog.SourceExists(eventSource)) 
       { 

        EventLog myLog = new EventLog(eventLog); 
        myLog.Source = eventSource; 


        myLog.WriteEntry("An error occurred in the Web application " 
        + eventSource + "\r\n\r\n" + myErrorMessage, 
         EventLogEntryType.Error); 
       } 
      } 
     } 

C'est des lignes de journal des événements:..

Type Date   Time  Source   Event    Category 
Error 03.04.2012 16:44:41 www.mysite.com 0 "An error occurred in the Web application www.mysite.com 

" 
Error 03.04.2012 16:43:31 www.mysite.com 0 "An error occurred in the Web application www.mysite.com 

" 
Error 03.04.2012 16:42:56 www.mysite.com 0 "An error occurred in the Web application www.mysite.com 

" 
Error 03.04.2012 16:42:56 www.mysite.com 0 "An error occurred in the Web application www.mysite.com 

" 
Error 03.04.2012 16:42:54 www.mysite.com 0 "An error occurred in the Web application www.mysite.com 

" 
Error 03.04.2012 16:37:27 www.mysite.com 0 "An error occurred in the Web application www.mysite.com 

"

Comme vous avez noté une erreur se produit environ une fois dans une seconde, mais les informations au sujet d'une erreur, le vide
Qu'est-ce que mauvais avec ce code?
Merci

Répondre

0

Le problème est dans cette ligne imo

while (myError.InnerException != null) 

Vous écrivez le détail de l'erreur que s'il y a un InnerException, mais il est pas toujours vrai

Vous avez oublié aussi appeler

Server.ClearError(); 

A la fin de votre traitement (mais Je suppose que c'est un choix)