In the software, the site will inevitably appear a variety of anomalies, if you have unhandled exceptions in your project and do not want to add an exception everywhere then you can learn from the next scenario
1. Create a new global application file Global.asax This file is used when counting the number of people online
2. There is a Application_Error method in this file that is used by this method, adding the following code to catch the exception
1 //code to run when an unhandled error occurs
2Exception ex = Server.GetLastError ();
3StringBuilder SB =NewStringBuilder ();
4 stringDT = DateTime.Now.ToString ();
5Sb. Append ("------\ r \ n"). Append (DT). Append ("\ r \ n Internal error:"). Append (ex. Innerexception.tostring ())
6. Append ("\ r \ n Stack:"). Append (ex. StackTrace). Append ("\ r \ n Content:"). Append (ex. Message)
7. Append ("\ r \ n Source:"). Append (ex. Source). Append ("\ r \ n");
8Log.setlog (sb.) ToString ());
9Server.ClearError ();
TenServer.Transfer ("error.aspx",false);
3. Create a new record log and automatically send a mail alert class
1 usingSystem;
2 usingSystem.Collections.Generic;
3 usingSystem.Linq;
4 usingsystem.web;
5 usingSystem.IO;
6 usingSystem.Threading;
7 /// <summary>
8 ///Summary description of Log
9 /// </summary>
Ten Public classLog
One{
A PublicLog ()
-{
- //
the //TODO: Add constructor logic here
- //
-}
- Private stringLogbody;
+ Public stringLogbody
-{
+ Get{returnLogbody; }
A Set{logbody = value;}
at}
- Public Static voidSetlog (stringStr
-{
- stringPath = HttpContext.Current.Server.MapPath ("Log.log");
-File.appendalltext (path, str);
- //Thread.Sleep (+);
inLog Log1 =NewLog ();
-Log1. Logbody = str;
toThread T =NewThread (NewThreadStart (Log1. SendEmail));
+T.isbackground =true;
-T.start ();
the}
*
$ Private voidSendEmail ()
Panax Notoginseng{
-System.Net.Mail.SmtpClient client =NewSystem.Net.Mail.SmtpClient ();
theClient. Host ="---";
+Client. useDefaultCredentials =false;
AClient. Credentials =NewSystem.Net.NetworkCredential ("---","---");
theClient. Deliverymethod = System.Net.Mail.SmtpDeliveryMethod.Network;
+Client. Timeout =9000;
-System.Net.Mail.MailMessage message =NewSystem.Net.Mail.MailMessage ("---","---");
$Message. Subject ="---";
$Message. Body = Logbody;
-Message. bodyencoding = System.Text.Encoding.UTF8;
-Message. isbodyhtml =true;
the
- Try
Wuyi{
theClient. Send (message);
-
Wu}
- Catch(Exception ex)
About{
$}
-}
-}
Where---fill in on demand
4. Create a new error.aspx file for the tip page to catch the exception after the turn
. NET captures global exceptions and logs, Multithreading sends MailTips