Sitecore 500 错误记录/警报

发布于 2024-11-07 03:09:02 字数 294 浏览 0 评论 0原文

问题

目前,我希望提供一个自定义 500 错误页面,并记录并警告 Web 管理员所遇到的情况(什么 URL、堆栈跟踪、时间戳等)。

我尝试在系统配置下定义自定义http错误,但没有命中错误页面。 我能够处理 404 及其相关错误(layoutnotfound)。

问题

我应该拦截 global.asax 中的上下文来处理 500 错误并返回自定义页面吗? Sitecore 是否有其他方法可以实现我正在寻找的目标?

主要是,我正在寻找使用 Sitecore 记录/警报 500 个错误的最佳实践

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 best practice of logging / alerts for 500 errors with Sitecore

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风情万种。 2024-11-14 03:09:02

尝试使用 ELMAH

这里是一篇关于将其与网站核心。

Try using ELMAH.

Here is an article on using it with Sitecore.

差↓一点笑了 2024-11-14 03:09:02

Elmah 很棒并且做得很好。您还可以使用 log4net 来记录异常。您要做的就是在 global.asax 中添加一个 application_error 方法,您就可以跟踪发生的所有错误。您还可以添加不同的附加程序,并将消息记录在日志文件、数据库中并通过电子邮件发送。

以下是记录错误的代码,并包含一些附加信息,例如 url 和当前 sitecore 项目:

        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();

        }

如果您想要一个简单的解决方案,我建议使用 Elmah。如果您想要更多的控制和更多的日志记录选项,您应该使用自定义的 log4net 解决方案。

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.

娇柔作态 2024-11-14 03:09:02

我尝试定义自定义 http 错误
在系统配置下,但是
错误页面没有被命中。我能够
处理 404 及其相关错误
(布局未找到)。

在这一点上...请注意,本地访问时自定义错误的行为有所不同。另外,默认情况下,您需要为这些页面使用物理文件(而不是 sitecore 页面)。您需要了解返回超过 200 的状态代码时的 IIS 行为,以及它如何依赖于 web.config 和(如果在集成模式下运行)该部分中的配置。特别是,请参阅下面第一个链接中间的流程图。

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

您可能还希望将 RequestErrors.UseServerSideRedirect 设置更改为“true”,以提供更好的 http 响应而无需重定向。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文