开发者

Sitecore 500 Error Logging / Alerts

Problem

Currently, I'm looking to serve a custom 500 error page as well as log and alert a web master about the condition that was hit (What URL, stack trace, timestamp, etc.).

I tried defined custom http errors under system configuration, but the error pages were not hit. I am able to handle 404s and their related errors (layoutnotfound).

Question

Should I intercept the context in global.asax to handle the 500 errors and return a custom page? Is there another way with Sitecore to achieve what I'm looking for?

Mainly, I'm looking for be开发者_StackOverflow社区st practice of logging / alerts for 500 errors with Sitecore


Try using ELMAH.

Here is an article on using it with Sitecore.


Elmah is great and does a good job. You can also use log4net to log exceptions. All you do is add an application_error method in global.asax and you can track all the errors that occur. You can also add different appenders and can log messages in a log file, database and email them.

Here is the code that logs the error and includes some additional information like url and Current sitecore item:

        private static readonly ILog log = LogManager.GetLogger(typeof(Global));
        protected void Application_Error(object sender, EventArgs e)
        {
            if (Context != null)
            {
                Exception error = Context.Server.GetLastError().GetBaseException();
                log.Fatal(
                    GetErrorMessage(), error);
            }
        }
        private string GetErrorMessage()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Application_Error: Unhandled exception has been occured.");
            try
            {
                sb.AppendLine("Current Request: " + Context.Request.RawUrl);
                Sitecore.Data.Items.Item currentItem = Sitecore.Context.Item;
                if (currentItem != null)
                    sb.AppendLine(String.Format("Current Item ({0}): {1}", currentItem.ID, currentItem.Paths.FullPath));
                if (Sitecore.Context.Database != null)
                    sb.AppendLine("Current Database: " + Sitecore.Context.Database.Name);
            }
            catch { } // in no way should we prevent the site from logging the error
            return sb.ToString();

        }

If you want an easy solution I would recommend going with Elmah. If you want to have more control and more logging options you should go with a custom log4net solution.


I tried defined custom http errors under system configuration, but the error pages were not hit. I am able to handle 404s and their related errors (layoutnotfound).

On that particular point...Be aware that custom errors behave differently when accessed locally. Also, by default you need to use physical files (not sitecore pages) for these pages. You need be aware of IIS behaviour when returning status codes over 200 and how it is dependant on the configuration within web.config and (if running in integrated mode) the section. In particular, see the flow chart half way down the first link below.

http://learn.iis.net/page.aspx/267/how-to-use-http-detailed-errors-in-iis-70/ http://www.iis.net/ConfigReference/system.webServer/httpErrors http://msdn.microsoft.com/en-us/library/ms689487(v=VS.90).aspx

You might also want to change the RequestErrors.UseServerSideRedirect setting to "true" to provide better http responses without redirects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜