An error log record and email reminder function is required recently. If you do not want to write it yourself, you can directly find the following on the Internet:CodeIt's quite easy to use!
This article presents a way to log and mail the errors from any web page.
It logs following details-
- Control on which the error is raised
- Page which controls the error
- Error description
- Trace messages
- Server and client details
Log is created in log folder under root directory.
Source code is provided in class file errorhandler. CS
Using system;
Using system. Data;
Using system. configuration;
Using system. LINQ;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. xml. LINQ;
Using system. net. mail;
Using system. IO;
/// <Summary>
/// This class is designed to log errors and send mails regarding
/// Runtime errors in myweb portal.
///
// Developer: yeotumitsu@sify.com
/// Date: 24-04-2008
/// Modified on: 25-04-2008
/// </Summary>
Public Enum messagetype // Enum is accessed to provide the operation to be done
{
EventLog,
Sendmail,
Mailandeventlog
}
public class errorhandler
{< br> Public messagetype msgtype
{< br> Get
{< br> return this. MT;
}< br> set
{< br> This. mt = value;
}< BR >}
Public messagetype Mt = new messagetype ();
Public String emailreciever = "";
Public String LOGNAME = "";
Public String MailServer = "";
Public String mailsubject = "Error Report" + httpcontext. Current. Request. servervariables ["SERVER_NAME"];
Public errorhandler () // sets default values of the fields
{
This. MailServer = configurationsettings. receivettings ["MailServer"];
This. emailreciever = configurationsettings. receivettings ["mailreciever"];
This. LOGNAME = configurationsettings. deleettings ["LOGNAME"];
}
/// <Summary>
/// Function errorhandler overloaded.
/// Sets the default values if provided by the user.
/// </Summary>
/// <Param name = "_ MailServer"> SMTP Server IP address </param>
/// <Param name = "_ mailreciever"> email id of person who is supposed to recieve error mails </param>
/// <Param name = "_ LOGNAME"> name of the Error Log </param>
Public errorhandler (string _ MailServer, string _ mailreciever, string _ LOGNAME)
{
This. MailServer = _ MailServer;
This. emailreciever = _ mailreciever;
This. LOGNAME = _ LOGNAME;
}
/// <Summary>
/// Raiseerror is called to select the operation to be done
/// </Summary>
/// <Param name = "_ message"> Any message </param>
/// <Param name = "ex"> exception </param>
Public void raiseerror (string _ message, exception ex)
{
Switch (this. MT)
{
Case messagetype. EventLog:
Savetoeventlog (_ message, ex );
Break;
Case messagetype. Sendmail:
Senderrormail (_ message );
Break;
Case messagetype. mailandeventlog:
Savetoeventlog (_ message, ex );
Senderrormail (_ message );
Break;
Default:
Break;
}
}
/// <Summary>
/// Sends mail to the person specified to recieve mails
/// </Summary>
/// <Param name = "_ message"> message to send </param>
/// <Returns> </returns>
Public int senderrormail (string _ message)
{
Mailmessage errormessage = new mailmessage ();
Errormessage. to. Add (New mailaddress (this. emailreciever ));
Errormessage. Subject = This. mailsubject;
Errormessage. isbodyhtml = true;
Errormessage. Priority = mailpriority. High;
Errormessage. Body = _ message + "Please Check log for more information .";
Smtpclient clientsmtp = new smtpclient ();
Try
{
Clientsmtp. Send (errormessage );
}
Catch
{
Return 0;
}
Return 1;
}
/// <Summary>
/// Formats and logs Error
/// </Summary>
/// <Param name = "_ message"> Any message </param>
/// <Param name = "ex"> exception </param>
Private void savetoeventlog (string _ message, exception ex)
{
Try
{
String strphysicalapplicationpath = httpcontext. Current. Request. physicalapplicationpath;
String strfilename = strphysicalapplicationpath + "logs \" + LOGNAME + datetime. Now. tostring ("ddmmmyyyy") + ". txt ";
String strbody = string. empty;
Fileinfo finfo = new fileinfo (strfilename );
Strbody = _ message + environment. newline + environment. newline;
Strbody + = "server address:" + httpcontext. Current. Request. servervariables ["SERVER_NAME"] + environment. newline;
Strbody + = "User address:" + httpcontext. Current. Request. servervariables ["remote_host"] + environment. newline;
Strbody + = "Script Name:" + httpcontext. Current. Request. servervariables ["script_name"] + environment. newline;
Strbody + = "query data:" + httpcontext. Current. Request. url. query + environment. newline;
Strbody + = "occured at:" + datetime. Now + environment. newline + environment. newline;
Strbody + = "################################## -- start of error --
################################# "+ Environment. newline;
Strbody + = ex. stacktrace + environment. newline;
Strbody + = "###################################-- end of error --
################################### "+ Environment. newline +
Environment. newline;
Strbody + = httpcontext. Current. Request. servervariables ["all_http"] + environment. newline;
Directoryinfo dinfo = new directoryinfo (strphysicalapplicationpath + "logs \\");
If (! Dinfo. exists)
{
Dinfo. Create ();
}
If (finfo. exists)
{
Filestream fstream = new filestream (strfilename, filemode. append, fileaccess. Write );
Streamwriter swriter = new streamwriter (fstream );
Swriter. writeline (strbody );
Swriter. Close ();
}
Else
{
Filestream fstream = new filestream (strfilename, filemode. Create, fileaccess. Write );
Streamwriter swriter = new streamwriter (fstream );
Swriter. writeline (strbody );
Swriter. Close ();
}
}
Catch (exception E)
{
Senderrormail (E. Message );
}
}
}
Web config serrings-
<Deleetask>
<Add key = "MailServer" value = "127.0.0.1"/>
<Add key = "mailreciever" value = "you@yourSite.com"/>
<Add key = "LOGNAME" value = "emposterrlog"/>
</Appsettings>
<System.net>
<Mailsettings>
<SMTP from = "admin@yourSite.com">
<Network host = "127.0.0.1"/>
</SMTP>
</Mailsettings>
</System.net>
Calling the method-
Catch (exception ex)
{
String strmsg =
"Date:" + datetime. Now. tostring ("DD/Mmm/YYYY hh: mm: SS") + "error:
"+ Ex. Message +" control: "+ (Control) sender). clientid. tostring ()
+ "Page:" + page;
Errorhandler objerrorhandler = new errorhandler ();
Objerrorhandler. msgtype = messagetype. mailandeventlog;
Objerrorhandler. raiseerror (strmsg, ex );
}
Hope it helps.