. NET captures global exceptions and logs, Multithreading sends MailTips

Source: Internet
Author: User
Tags mailmessage smtpclient

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

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.