al3*_*ull 3 c# logging sitecore
目前,我正在寻找一个自定义500错误页面,以及记录和提醒网站管理员有关被击中的情况(什么URL,堆栈跟踪,时间戳等).
我尝试在系统配置下定义自定义http错误,但没有命中错误页面.我能够处理404s及其相关错误(layoutnotfound).
我应该拦截global.asax中的上下文来处理500个错误并返回自定义页面吗?Sitecore是否有另一种方式来实现我正在寻找的东西?
主要是,我正在寻找使用Sitecore记录/警告500个错误的最佳做法
艾玛很棒,做得很好.您还可以使用log4net记录异常.您所做的只是在global.asax中添加application_error方法,您可以跟踪发生的所有错误.您还可以添加不同的appender,并可以在日志文件,数据库和电子邮件中记录消息.
以下是记录错误的代码,并包含一些其他信息,如url和Current 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();
}
Run Code Online (Sandbox Code Playgroud)
如果你想要一个简单的解决方案,我建议你去Elmah.如果您想拥有更多控制权和更多日志记录选项,则应使用自定义log4net解决方案.