我已经实现了错误处理代码来将异常记录到 Splunk(使用 log4net),无论它们发生在我的 MVC 4 应用程序中的什么地方。问题是当视图文件 (cshtml) 中出现异常时,没有与该异常匹配的 Splunk 日志。
我调试了代码,发现所有正确的方法都被调用了。当我检查传递给 ILog.Error(string message) 方法的值时,该值为:
System.Exception: Test view exception
at ASP._Page_Views_Home_Index_cshtml.Execute() in c:\dev\MyMVCApp\Views\Home\Index.cshtml:line 5
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.StartPage.RunPage()
at System.Web.WebPages.StartPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
除视图异常记录的消息外,所有其他日志尝试均成功。我的 log4net 配置如下:
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net, Version=1.2.11.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a" />
<!-- snip -->
</configSections>
<!-- snip -->
<log4net>
<appender name="SplunkAppender" type="log4net.Appender.RemoteSyslogAppender">
<remoteAddress value="splunk.myawesomeserver.com" />
<remotePort value="2221" />
<layout type="log4net.Layout.PatternLayout">
<ConversionPattern value="[%-5p] (%c) %m%n" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="SplunkAppender" />
</root>
</log4net>
</configuration>
我这个配置错了吗?将消息传递给 ILog.Error() 后,有没有办法找出问题所在?