A better. Net 3.5 exception report class

Source: Internet
Author: User
Tags mailmessage smtpclient
A good exception reporting class was found, with the extension method above. Net 3.5 used,

The solution is to send an email report when an exception occurs.

Using system;

Using system. Collections. Generic;

Using system. LINQ;

Using system. Web;

Using system. net. mail;

Namespace experiement

{

Public static class myextension

{

Public static void senderroremail (this exception ex)

{

Mailmessage = new mailmessage (New mailaddress ("from@gmail.com ")

, New mailaddress ("to@gmail.com "));

Mailmessage. Subject = "exception occured in your site ";

Mailmessage. isbodyhtml = true;

System. Text. stringbuilder errormessage = new system. Text. stringbuilder ();

Errormessage. appendline (string. Format ("<B> {0} </B >:{ 1}", "exception", Ex. Message ));

Errormessage. appendline (string. Format ("<B> {0} </B >:{ 1}", "stack trace", Ex. stacktrace ));

If (ex. innerexception! = NULL)

{

Errormessage. appendline (string. Format ("<B> {0} </B >:{ 1}", "inner exception", Ex. innerexception. Message ));

Errormessage. appendline (string. Format ("<B> {0} </B >:{ 1}", "inner stack trace", Ex. innerexception. stacktrace ));

}

Mailmessage. Body = errormessage. tostring ();

System. net. networkcredential networkcredentials = new

System. net. networkcredential ("youraccount@gmail.com", "password ");



Smtpclient = new smtpclient ();

Smtpclient. enablessl = true;

Smtpclient. usedefadefacredentials = false;

Smtpclient. Credentials = networkcredentials;

Smtpclient. Host = "smtp.gmail.com ";

Smtpclient. Port = 587;

Smtpclient. Send (mailmessage );



}

}

}

Usage:

Using system;

Namespace experiement

{

Public partial class webform1: system. Web. UI. Page

{

Protected void page_load (Object sender, system. eventargs E)

{

Try

{

Throw new exception ("My custom exception ");

}

Catch (exception ex)

{

Ex. senderroremail ();

Response. Write (ex. Message );

}

}

}

}

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.