ntfishersdk@hotmail.com
New member
- Joined
- Oct 15, 2007
- Messages
- 3
- Programming Experience
- 5-10
Hello,
I have a fairly large console application that is setup the following way:
Console Project: Executes Business code to process.
Business Logic Layer Project: Contains all business rules to process
Business Entities Layer Project: Contains all data objects i.e.(DataSets)
Data Access Layer Project: Contains Data access ADO helpers.
Anyway this applications main function is to accept parameters and process the creation of files that are generated from the data base as well as process incoming files and upload them to the database.
I am trying to implement exception handling policies into my application but since there is really no exceptions i can plan for and catch and recover from i am trying to log all exceptions at an application level.
The problem with this code is that the actual exception appears to be swallowed by the CLR and does not get logged properly in the application event log. Instead of the actual inner exception being logged a CLR exception is logged and the app thread is terminated and the application is thrown into debug mode from the CLR. What I need to happen is the application log the inner exception and the application exit gracefully.
I have done this type of policy handling and logging in windows forms and ASP.NET but I am not familiar with how to handle this in a console application. I know there is some known issues with how the CLR terminates console application main threads when an exception occurs but have been unable to find any method that satisfies my desired result.
If anyone has anything that can help with this it would be greatly appreciated.
I have a fairly large console application that is setup the following way:
Console Project: Executes Business code to process.
Business Logic Layer Project: Contains all business rules to process
Business Entities Layer Project: Contains all data objects i.e.(DataSets)
Data Access Layer Project: Contains Data access ADO helpers.
Anyway this applications main function is to accept parameters and process the creation of files that are generated from the data base as well as process incoming files and upload them to the database.
I am trying to implement exception handling policies into my application but since there is really no exceptions i can plan for and catch and recover from i am trying to log all exceptions at an application level.
VB.NET:
Module PopsIFConsole
''' <summary>
'''
''' </summary>
''' <remarks></remarks>
Sub Main()
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf CurrentDomain_UnhandledException
Dim customerBC As New ARCustomerBC()
customerBC.WriteCustomerFile()
Dim accountsPayableBC As New AccountsPayableBC()
accountsPayableBC.WriteAccountsPayableFiles()
Dim openObligations As New AROpenObligationsBC()
openObligations.ProcessOpenObligations()
Dim newObligations As New ARNewObligationsBC()
newObligations.WriteNewObligationsFile()
Dim omsFileInterface As OMSFileInterface = New OMSFileInterface()
omsFileInterface.CreateOMSMasterFile("LFR")
omsFileInterface.CreateOMSMasterFile("BWK")
System.Console.WriteLine("Done")
System.Console.Read()
End Sub
Private Sub HandleException(ByVal ex As Exception, ByVal policy As String)
Dim rethrow As Boolean = False
Try
rethrow = ExceptionPolicy.HandleException(ex, policy)
Catch innerEx As Exception
Dim errorMsg As String
errorMsg = String.Format("An unexpected exception occured " & _
"while calling HandleException with policy '{0}.{1}{2}'", _
policy, Environment.NewLine, innerEx.ToString())
System.Console.WriteLine(errorMsg, "Application Error")
Throw ex
End Try
If (rethrow) Then
' WARNING: This will truncate the stack of the exception
Throw ex
Else
System.Console.WriteLine("An unhandled exception occurred " & _
"and has been logged. Please contact support.")
End If
End Sub
Private Sub CurrentDomain_UnhandledException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
If TypeOf e.ExceptionObject Is System.Exception Then
HandleException(CType(e.ExceptionObject, System.Exception), "Global Policy")
End If
If Not e.IsTerminating Then
' Exception occurred in a thread pool or finalizer thread
' Debugger launches only explicitly
Else
' Exception occurred in managed thread
' Debugger will also launch when not launched explicitly
End If
' This is a final release;
' logging is done to Syslog and over the Web if possible
' Debugger is mostly not available
If Not e.IsTerminating Then
' Exception occurred in a thread pool or finalizer thread
' Application keeps open
Else
' Exception occurred in managed thread
' Application is closing, so you should log now
End If
End Sub
End Module
The problem with this code is that the actual exception appears to be swallowed by the CLR and does not get logged properly in the application event log. Instead of the actual inner exception being logged a CLR exception is logged and the app thread is terminated and the application is thrown into debug mode from the CLR. What I need to happen is the application log the inner exception and the application exit gracefully.
I have done this type of policy handling and logging in windows forms and ASP.NET but I am not familiar with how to handle this in a console application. I know there is some known issues with how the CLR terminates console application main threads when an exception occurs but have been unable to find any method that satisfies my desired result.
If anyone has anything that can help with this it would be greatly appreciated.
Last edited by a moderator: