How to log an error through out full application
-
I want to print a log whenever an exception is thrown by try-catch block through out whole application.
-
I want to print a log whenever an exception is thrown by try-catch block through out whole application.
What type of application? WPF? WinForms? Xamarin? ASP.NET?
This space for rent
-
I want to print a log whenever an exception is thrown by try-catch block through out whole application.
A
try..catch
block doesn't throw exceptions; it's used to handle them. If you want to log all unhandled exceptions, then the code you need will vary depending on what type of application you're writing. You could start with theAppDomain.UnhandledException
event[^] and theTaskScheduler.UnobservedTaskException
event[^]. If you really want to log every exception, even those which are handled, then you'll need to subscribe to theAppDomain.FirstChanceException
event[^]. NB: This will generate a lot of noise. You will be logging every exception thrown in any code, including .NET itself, regardless of whether or not it's handled.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
A
try..catch
block doesn't throw exceptions; it's used to handle them. If you want to log all unhandled exceptions, then the code you need will vary depending on what type of application you're writing. You could start with theAppDomain.UnhandledException
event[^] and theTaskScheduler.UnobservedTaskException
event[^]. If you really want to log every exception, even those which are handled, then you'll need to subscribe to theAppDomain.FirstChanceException
event[^]. NB: This will generate a lot of noise. You will be logging every exception thrown in any code, including .NET itself, regardless of whether or not it's handled.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Richard Deeming wrote:
the
AppDomain.FirstChanceException
event[^]. NB: This will generate a lot of noise.Thank you for that! Would you happen to know if using this event would cause a significant slowdown, even if the code in the event handler is trivial?
The difficult we do right away... ...the impossible takes slightly longer.
-
Richard Deeming wrote:
the
AppDomain.FirstChanceException
event[^]. NB: This will generate a lot of noise.Thank you for that! Would you happen to know if using this event would cause a significant slowdown, even if the code in the event handler is trivial?
The difficult we do right away... ...the impossible takes slightly longer.
I'm running it full time ("first chance") in one of my UWP apps and have seen no "noise" or slowdown. UWP runs differently than WPF (infrastructure) and I found I needed it in a few cases where my "local" exception handling missed things. (Or maybe I have fewer exceptions). No noise. (I have a breakpoint in the first chance routine so I'm aware of how often it runs.)
The Master said, 'Am I indeed possessed of knowledge? I am not knowing. But if a mean person, who appears quite empty-like, ask anything of me, I set it forth from one end to the other, and exhaust it.' ― Confucian Analects
-
Richard Deeming wrote:
the
AppDomain.FirstChanceException
event[^]. NB: This will generate a lot of noise.Thank you for that! Would you happen to know if using this event would cause a significant slowdown, even if the code in the event handler is trivial?
The difficult we do right away... ...the impossible takes slightly longer.
Unfortunately, I don't. I've never actually used that event. But I think Visual Studio uses something similar - the output window usually shows first-chance exceptions in the log when you're debugging.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer