Reputation: 389
I am having an issue in an HttpHandler when an exception gets thrown and handled by Application_Error()
within my global application file (global.asax
). I am trying to write the exception.Message
in my application's EventLog source.
And to clarify before you ask, yes the EventLog source exists. The problem I'm having is that the exception itself doesn't write to my application's EventLog Source (for the sake of this example we'll say the event log source is named HttpEndpoint
), but instead logs itself to the ASP.NET 4.0.30319.0 event log source.
Is there any way around this without doing away with global.asax's Application_Error()
method, wrapping the method that executes before this error is thrown in a try{}catch{}
and writing to the application's event log source in that manner?
The code in Application_Error()
is pretty darn straight-forward:
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 );
}
The event log source of HttpEndpoint
is held as an <AppSetting>
within the Web.Config file, and is assigned to the _eventLogSource
member in global.asax
. I also do other logging within the application to the same event log source (outside of global.asax
), and that all works just fine and dandy.
Any ideas? Any help / feedback is much appreciated.
Upvotes: 0
Views: 893
Reputation: 389
So I did some experimenting...this seems to occur because of the cast from Exception
to OutputMessageQueueWriteFailedException
.
If I don't attempt to cast the exception type, and just let the exception's message get written to the event log in the _windowsEventLog.LogError()
line instead of _windowsEventLog.LogFatal()
, it gets written to the event log source I want.
I'm not quite sure why exactly that is. Any elaborations on this topic are welcomed!
Upvotes: 1