You don't want app domain stuff usless your are creating app domains. Just
use Application.ThreadException... something like below... sample class
attached.
CustomExceptionHandler eh = new CustomExceptionHandler();
Application.ThreadException += new ThreadExceptionEventHandler(
eh.OnThreadException );
Best,
/jhd
p.s. timers, filesyswatchers are all different processes... need semaphores
vs monitor/lock
Steel City Phantom wrote:
how exactly do you do this on windows services? i am tracking a
filesystemwatcher problem that is looking more and more like a framework
bug, but i can only get very little detail and it kills my service. i
specified the AppDomain.UnhandledException handler in my service main
function, but its still not catching this error that keeps popping up.
Message by Jay B. Harlow [MVP - Outlook] <
[email protected]> on:
7/9/2004 10:58:48 AM
Einar,
In addition to the other comments about using the Exception Management
Block, I normally do my logging in a global exception handler. (Read I may
use the Exception Management Block in my Global Exception Handler)
Also I use try/finally & using more then I use try/catch. I only use
try/catch when there is something specific that I need to do with the
exception, otherwise I let my global exception handlers handle the
exception.
Depending on the type of application you are creating, .NET has three
different global exception handlers.
For ASP.NET look at:
System.Web.HttpApplication.Error event
Normally placed in your Global.asax file.
For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.
For Windows Forms look at:
System.Windows.Forms.Application.ThreadException event
Use AddHandler in your Sub Main.
It can be beneficial to combine the above global handlers in your app, as
well as wrap your Sub Main in a try catch itself.
There is an article in the June 2004 MSDN Magazine that shows how to
implement the global exception handling in .NET that explains why & when
you
use multiple of the above handlers...
http://msdn.microsoft.com/msdnmag/issues/04/06/NET/default.aspx
For example: In my Windows Forms apps I would have a handler attached to
the
Application.ThreadException event, plus a Try/Catch in my Main. The
Try/Catch in Main only catches exceptions if the constructor of the
MainForm
raises an exception, the Application.ThreadException handler will catch all
uncaught exceptions from any form/control event handlers.
Hope this helps
Jay
Hi,
I'm getting into the Trace-functionality in .NET, using it to provide some
much-needed logging across dlls in the project we're working on. However,
being a newbie, I'm wondering if some more experienced loggers can provide
me with some ideas as to how to log in a simple yet flexible manner. For
instance, I'd like the code to be as uncluttered as possible by Trace
statements.
As an example of basic logging functionality, I've come up with some
alternatives for logging exceptions that occur in an object:
1) Wrap the throwing of an exception in a method, which can handle any
logging before throwing the actual exception.
2) Handle logging in the constructors of my own hierarchy of exceptions.
This makes the logging more transparent in the code, but doesn't work for
standard .NET exceptions such as ArgumentNullException (I'd have to
subtype
it).
3) Use a decorator object to wrap the original object, catch all
exceptions,
write to log, and rethrow.
Any and all thoughts/comments are much appreciated!
Regards,
Einar
--
"If it was so, it might be; and if it were so, it would be; but as it
isn't,
it ain't. That's logic" -- Lewis Carroll
------------------------------------------------------------------------
/// <summary>
/// Top level exception handler. Future home of automatic bug report form and
add to bug database.
/// </summary>
internal class CustomExceptionHandler
{
// Handles the exception event.
public void OnThreadException( object sender, ThreadExceptionEventArgs t )
{
DialogResult result = DialogResult.Cancel;
try
{
result = this.ShowThreadExceptionDialog( t.Exception );
}
catch
{
try
{
MessageBox.Show( "Fatal Error", "Fatal Error",
MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop );
}
finally
{
Application.Exit();
}
}
// Exits the program when the user clicks Abort.
if( result == DialogResult.Abort )
Application.Exit();
}
// Creates the error message and displays it.
private DialogResult ShowThreadExceptionDialog( Exception e )
{
string errorMsg = "An error occurred please contact the adminstrator with the
following information:\n\n";
errorMsg = errorMsg + e.Message + "\n\nStack Trace:\n" + e.StackTrace;
return MessageBox.Show( errorMsg, "Application Error",
MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop );
}
}