Page 1 of 1

Notification of ERRORS

Posted: Wed Jul 22, 2009 7:50 am
by NC Software
Anyone built a system to be notified of errors as they occur on your store? I've seen this with other carts and I'm getting ready to implement a system now as I'm seeing a lot of errors in the App_Data/log file that are not being reported in the Help/Error log area. So if anyone has something built and cares to share, please post or redirect me to it.

Thx

Re: Notification of ERRORS

Posted: Wed Jul 22, 2009 8:39 am
by mazhar
The best possiable solution I could think of for error alert will be someting like below

1)- Create a new Email Template for example name it Error Notification, then adjust its layout and put some expected NVelocity vairable to show respective data.

2)- Now in Global.asax file put some code in Application_Error event to load and trigger newly added Email template with appropriate data.

Here are the basic changes needed for this modification. You can enhance the posted Email template to accommodate your custom needs. So download, extract and place the Error Notification.html file in App_Data/Email Templates/1 folder. Next job is to create a new Email Template and assign it the newly added email template file. In to address section put the Email address where you want to receive error alert. Then put following text in the subject text box

Code: Select all

Error occured at $store.Name
and don't forget to mark Send as HTML checkbox.

Then note the EmailTemplateId of this newly added Email template from the query and go to Global.asax and edit file, and change its Application_Error error method as below

Code: Select all

protected void Application_Error(Object sender, EventArgs e)
	{
        // ENABLE ERROR LOGGING FOR SCRIPTS OUTSIDE OF THE INSTALL DIRECTORY
        if (!HttpContextHelper.IsInstallRequest())
        {
            // RECORD THE DETAILS TO THE AC ERROR LOG
            HttpContext ctx = HttpContext.Current;
            Exception exception = ctx.Server.GetLastError();
            
            // IGNORE INVALID VEIW STATE ERRORS
            if (IsViewStateException(exception)) return;
            
            string errorInfo = "An error has occured at " + ctx.Request.Url.ToString();
            Logger.Error(errorInfo, exception);

EmailTemplate emailTemplate = EmailTemplateDataSource.Load(emailTempalteId);
            if (emailTemplate == null)
                return;
            emailTemplate.Parameters.Add("store", Token.Instance.Store);
            emailTemplate.Parameters.Add("exception", exception);
            emailTemplate.Send();
            
        }
	}
Where email template code under Logger.Error statement is newly added code. Now replace emailTempalteId with actual integer value of your error notification Email template id. That's it now you will receive error notification when some problem occurs at your store.

Re: Notification of ERRORS

Posted: Wed Jul 22, 2009 12:24 pm
by NC Software
Thanks for the info, rapid reply, and helpful information that will benefit others. It's also a nice tutorial on the messaging system. I changed the code slightly for anyone else that wants this. I haven't received any e-mails yet to verify so we'll tweak as time goes on. I added a $message to my e-mail template to write the information in as you'll see the code change below:

Code: Select all

	protected void Application_Error(Object sender, EventArgs e)
	{
        // ENABLE ERROR LOGGING FOR SCRIPTS OUTSIDE OF THE INSTALL DIRECTORY
        if (!HttpContextHelper.IsInstallRequest())
        {
            // RECORD THE DETAILS TO THE AC ERROR LOG
            HttpContext ctx = HttpContext.Current;
            Exception exception = ctx.Server.GetLastError();

            // IGNORE HttpExceptions
            //if (exception is HttpException) return;

            // IGNORE INVALID VIEW STATE ERRORS
            if (IsViewStateException(exception)) return;
            
            //string errorInfo = "An error has occured at " + ctx.Request.Url.ToString();
            //Logger.Error(errorInfo, exception);

            EmailTemplate emailTemplate = EmailTemplateDataSource.Load(17);
            if (emailTemplate == null)
                return;
            string errorInfo =
             "<br>Offending URL: " + ctx.Request.Url.ToString() +
             "<br>Source: " + exception.Source +
             "<br>Message: " + exception.Message +
             "<br>Stack trace: " + exception.StackTrace;
            Logger.Error(errorInfo, exception);
            emailTemplate.Parameters.Add("store", Token.Instance.Store);
            emailTemplate.Parameters.Add("exception", exception);
            emailTemplate.Parameters.Add("message", errorInfo);
            emailTemplate.Send();
        }
	}

Re: Notification of ERRORS

Posted: Sat Jul 30, 2011 12:27 pm
by RichWendrock
Since this post is two years old and I see another method for emailing errors using log4net. Which option is best?

I implemented log4net and it does send an email for each event. Occasionally, we get a url coming in that is invalid. In place of &CategoryId= we get &CategoryId=. I would like to know the origin of the requests. I came to the forum looking for a way to capture the REFERRER URL. Still looking. Does anyone know?

Re: Notification of ERRORS

Posted: Sun Jul 31, 2011 5:52 pm
by mikek
We use ELMAH for our clients hosted with us and it works great. You can set up email notifications, web page or RSS error feed.

http://code.google.com/p/elmah/

The ELMAH install requires only elmah.dll copy in your Ablecommerce /bin folder and few lines of web.config code. You can switch on/off error
handling directly from the web.config file. No AbleCommerce code or other changes required.