我在全局应用程序文件(global.asax
)中抛出异常并由Application_Error()
处理异常时发生HttpHandler中的问题。我正尝试在我的应用程序的EventLog源代码中编写exception.Message
。Global.asax不写入应用程序事件日志来源
并澄清之前,你问,是的EventLog源存在。我遇到的问题是异常本身不写入我的应用程序的EventLog源(为了这个例子我们会说事件日志源被命名为HttpEndpoint
),但是将自身记录到ASP.NET 4.0 .30319.0事件日志源。
有没有什么办法可以解决这个问题,而不会废除global.asax的Application_Error()
方法,在这个错误被抛入try{}catch{}
之前执行的方法,并以这种方式写入应用程序的事件日志源?
在Application_Error()
的代码是相当不错的直截了当:
protected void Application_Error(object sender, EventArgs e)
{
var exception = Server.GetLastError();
if (exception == null)
{
return;
}
// try casting exception to known types for more specific event log entries
var msmqOutputQueueWriteFailedException = exception as OutputMessageQueueWriteFailedException;
if (msmqOutputQueueWriteFailedException != null)
{
_windowsEventLog.LogFatal(_eventLogSource, string.Format(CultureInfo.CurrentCulture, "MSMQ Message Send Failure: {0} - InnerException Message: {1}", msmqOutputQueueWriteFailedException.Message, msmqOutputQueueWriteFailedException.InnerException.Message));
}
_windowsEventLog.LogError(_eventLogSource, String.Format(CultureInfo.CurrentCulture, "Global Exception was thrown: {0}", exception.Message), exception);
}
的HttpEndpoint
事件日志源举行在web.config文件的<AppSetting>
,并分配给_eventLogSource
成员global.asax
。我还在应用程序中对同一事件日志源(global.asax
之外)进行了其他日志记录,并且所有这些日志都可以正常工作。
任何想法?任何帮助/反馈非常感谢。