Sunday, 2 December 2012

write error message in Notepad using C#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net.Mail;
using System.Net;

public partial class ErrorMessageInNotePad : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnCreateError_Click(object sender, EventArgs e)
    {
        try
        {
            throw new System.ArgumentException("Sajil Created the ErrorMessage!!!");
        }
        catch (Exception ex)
        {
//HERE YOU CAN USE PAGE NAME,FILE NAME,MODULE NAME OR FUNCTION                                               NAME TO   IDENTIFY THE ERROR SPOT  AND YOU CAN ALSO MAKE THESE CODE AS SAPARATE FUNCTION  AND USE IN CACTH BLOCK.

            StreamWriter sw = new StreamWriter(Server.MapPath("ErrorNotepad") + "\\ErrorList.txt", true);
            sw.WriteLine("Error occurred at", DateTime.Now.ToString());
            sw.WriteLine(ex.Message, DateTime.Now.ToString());
            sw.Dispose();
//CODE FOR SEND MAIL THAT NOTEPAD
            MailMessage newmsg;
            MailAddress mailfrom = new MailAddress("xxxxx.com");
            MailAddress mailto = new MailAddress("xxxxx.com");
            newmsg = new MailMessage(mailfrom, mailto);

            newmsg.Body ="You Have Error Message!!!";
         
            Attachment att = new Attachment(Server.MapPath("ErrorNotepad") + "\\ErrorList.txt");

            newmsg.Attachments.Add(att);
            SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential("xxxxx", "xxxxx");
            smtp.EnableSsl = true;
            smtp.Send(newmsg);
           

        }
    }
}
POST COMMENT IF ITS USEFULL,HAPPY CODING.....

No comments:

Post a Comment