2013-04-03 47 views
0

我在全局应用程序文件(global.asax)中抛出异常并由Application_Error()处理异常时发生HttpHandler中的问题。我正尝试在我的应用程序的EventLog源代码中编写exception.MessageGlobal.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之外)进行了其他日志记录,并且所有这些日志都可以正常工作。

任何想法?任何帮助/反馈非常感谢。

回答

1

所以我做了一些尝试...这似乎是因为演员从ExceptionOutputMessageQueueWriteFailedException

如果我不尝试转换异常类型,并且让异常消息被写入_windowsEventLog.LogError()行中的事件日志而不是_windowsEventLog.LogFatal(),它会被写入我想要的事件日志源。

我不太清楚为什么。欢迎任何关于此主题的阐述!

相关问题