1
我需要捕獲Application_Start()中的任何錯誤並重定向到HTML錯誤頁面,但例如如果出現錯誤,NLog不記錄任何內容,數據庫不可用:使用NLog獲取Application_Start()中的錯誤並重定向到友好的頁面錯誤
private static Logger logger = LogManager.GetCurrentClassLogger();
protected void Application_Start()
{
try
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AutofacConfig.RegisterDependencies();
AutoMapperConfiguration.Configure();
AuthConfig.RegisterAuth();
SimpleMemberShipConfig.SimpleMembershipInitializer();
EntityFrameworkConfig.Configure();
}
catch (Exception e)
{
logger.Error(e);
}
}
NLOG配置:
<targets>
<target xsi:type="File" name="Heelp_log" fileName="${basedir}/logs/Heelp-${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${callsite:className=true:includeSourcePath=true:methodName=true} ${message}" />
</targets>
<rules>
<logger name="*" minlevel="Error" writeTo="Heelp_log" />
</rules>
要重定向我:
<customErrors mode="On" defaultRedirect="~/Views/Shared/Error.html" />
但它關係到這樣一個頁面(/Views/Shared/Error.html?aspxerrorpath=/):
Server Error in '/' Application.
Runtime Error
Description: An exception occurred while processing your request. Additionally, another exception occurred while executing the custom error page for the first exception. The request has been terminated.
嗨,謝謝!你如何建議我使用NLog記錄錯誤? – Patrick