learning...
learning...

Reputation: 3174

Kentico tapping into Page not Found event

My task is to log exception and page not found in our database, from inside a Kentico project. For page not found, i need to tap into "CMSRequestEvents.End.After" event. This event receives sender and event args. How do i crack event args and find page not found related stuff? Since this event gets fired for all the requests, I need to handle only "page not found" and log it. SystemEvents.Exception.Execute gets fired when an exception happens and page not found is not an exception. We are using Kentico version 7.

[CustomHandleError]
public partial class CMSModuleLoader
{
    private class CustomHandleErrorAttribute : CMSLoaderAttribute
    {
        /// <summary>
        /// Called automatically when the application starts
        /// </summary>
        public override void Init()
        {
            // Assign custom handlers to the appropriate events
            SystemEvents.Exception.Execute += System_Exception_Execute;
            CMSRequestEvents.End.After += Request_End_After;
        }

        private void System_Exception_Execute(object sender, SystemEventArgs e)
        {
            try
            {
                var exception = e.Exception;
                var errorloggerHelper = new ErrorLoggerHelper();
                errorloggerHelper.LogError(exception);
            }
            catch
            {
                //do nothing 
            }
        }

        private void Request_End_After(object sender, EventArgs e)
        {
            //since this gets called for each request, HANDLE only "page not found" and log

        }
    }
}

Upvotes: 0

Views: 149

Answers (1)

rocky
rocky

Reputation: 7696

I will assume that by "our database" you mean database other than the Kentico's one.
As Kentico already logs "Page Not Found" events I'd suggest attaching to the EventLogInfo.TYPEINFO.Events.Insert.After event and filtering by EventCode=="PAGENOTFOUND" as described in the documentation.

Screenshot (v8): enter image description here

Another option would be utilizing custom error pages.

Upvotes: 2

Related Questions