Notification of ERRORS

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
User avatar
NC Software
AbleCommerce Partner
AbleCommerce Partner
Posts: 4620
Joined: Mon Sep 13, 2004 6:06 pm
Contact:

Notification of ERRORS

Post by NC Software » Wed Jul 22, 2009 7:50 am

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
Neal Culiner
NC Software, Inc.

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Notification of ERRORS

Post by mazhar » Wed Jul 22, 2009 8:39 am

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.

User avatar
NC Software
AbleCommerce Partner
AbleCommerce Partner
Posts: 4620
Joined: Mon Sep 13, 2004 6:06 pm
Contact:

Re: Notification of ERRORS

Post by NC Software » Wed Jul 22, 2009 12:24 pm

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();
        }
	}
Neal Culiner
NC Software, Inc.

User avatar
RichWendrock
Commander (CMDR)
Commander (CMDR)
Posts: 134
Joined: Sat Apr 05, 2008 12:55 am
Location: Austin Texas
Contact:

Re: Notification of ERRORS

Post by RichWendrock » Sat Jul 30, 2011 12:27 pm

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?
Regards,
Richard

http://www.TheHomePageStore.com

AbleCommerce
VERSION: 7.0.7.14588
MSSQL v2005
AC SCHEMA v2005
.NET CLR v2.0.50727.3634

User avatar
mikek
Commander (CMDR)
Commander (CMDR)
Posts: 112
Joined: Wed Oct 15, 2008 9:30 pm
Location: Boston, MA
Contact:

Re: Notification of ERRORS

Post by mikek » Sun Jul 31, 2011 5:52 pm

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.
Mike Kolev

Post Reply