Error Emailing

This is a forum to suggest new features for mojoPortal. 

This thread is closed to new posts. You must sign in to post in the forums.
5/23/2008 1:26:46 PM
Gravatar
Total Posts 12

Error Emailing

Here is the error handling code that I use on most of my sites.  It sends out an email to me whenever there is an error.  It also maintains the http status codes for errors like 404, which is important as it tells search engines that the url is bad and you really don't want those errors being left in the log anyways.

It would be great if you would add something like this to mojo portal.  I think too many sites on the net have problems that no one ever finds out about because they're just stuffed in a log somewhere.

 

public static void HandleException(Exception exception, HttpContext context)
{
HttpException httpException = exception as HttpException;
if (httpException != null && (httpException.GetHttpCode() == 405 || httpException.GetHttpCode() == 404 || httpException.GetHttpCode() == 403))
{
context.Response.StatusCode = httpException.GetHttpCode();
context.Response.SuppressContent = true;
context.Response.End();
return;
}

SmtpClient mailClient = new SmtpClient(System.Configuration.ConfigurationManager.AppSettings["MailServer"]);

StringBuilder errorMessage = new StringBuilder();

if (exception is HttpUnhandledException)
exception = exception.InnerException;

errorMessage.Append(exception.ToString());

if (httpException != null)
errorMessage.Append("\n\nHTTP EXCEPTION CODE: " + httpException.GetHttpCode());

if (exception.InnerException != null)
{
errorMessage.Append("\n\n ***INNER EXCEPTION*** \n");
errorMessage.Append(exception.InnerException.ToString());
}

if (context != null)
{
errorMessage.Append("\n\n ***REQUEST PARAMETERS*** \n");
foreach (string LName in context.Request.Params.Keys)
{
errorMessage.AppendFormat("\n{0} = {1};", LName, context.Request[LName]);
}
}

try
{
mailClient.Send
(
SiteName + " Web Server <server@server.com>",
System.Configuration.ConfigurationManager.AppSettings["ErrorEmail"],
SiteName + " Error " + Guid.NewGuid().ToString(),
errorMessage.ToString()
);
}
catch (System.Net.Mail.SmtpException)
{
}
}

5/23/2008 1:27:28 PM
Gravatar
Total Posts 12

Re: Error Emailing

~

5/24/2008 7:11:17 AM
Gravatar
Total Posts 18439

Re: Error Emailing

Hi,

We use log4net for logging. I've never done this, but my understanding is you can configure it to send email using the SMTP Appender.

http://logging.apache.org/log4net/release/config-examples.html (towards the bottom of the page)

http://logging.apache.org/log4net/release/sdk/log4net.Appender.SmtpAppender.html

In mojoportal, logging is configured in the log4net.config file in the root of the web. I copied some of the example configurations from th above and pasted them into our log4net.config file just now and committed it to svn trunk. You may be able to uncomment and configure it to send you email.

In my experience, once your site has been up for a while you will get lots of errors caused by bots and scripts making strange requests as they poke at your site looking for vulnerabilities or trying to spam your blog or contact form and this may become very annoying as email. log4net has a progressive series of logging levels, in mojoportal it defaults to INFO but you may want to set it to ERROR to reduce the noise.

Best,

Joe

You must sign in to post in the forums. This thread is closed to new posts.