Is there a centralised error handling process in C# - c#

Is there a way to centralize the error handling or exceptions handling without using try catch methods?

Use AppDomain's UnhandledException event:
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
// log the exception
}
For ASP.NET use you will use glabal.asax.

If this is for ASP.NET you can add a Global.asax file to the website and handle the Application_Error method.
This is how I generally use it:
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource("MySource",
"Application");
}
System.Diagnostics.EventLog.WriteEntry("MySource",
Server.GetLastError().ToString());
}

You could try AOP based addons like PostSharp that injects your exception handling code to your classes and/or methods that have custom attributes. This is done post-compile, so your source code remains clean. Check this out - http://www.sharpcrafters.com/postsharp/documentation/getting-started

If you are using WinForms, you could have a look at my other answer related to this. It does use try-catch though, as there is no other way that I know off.
See other answers for ASP.NET and possible other .NET uses.

Related

C# .NET VSTO Excel Add-In Global Error Handler Options

I have an Excel Add-In created using VSTO and C# .NET. I am interested in creating a handler for unhandled exceptions. Based on some research and other posts, there are a couple of events that I can register for and they seem to do the trick.
So, something like this:
private void ThisAddIn_Startup(object sender, EventArgs e)
{
AppDomain.CurrentDomain.UnhandledException += this.CurrentDomain_UnhandledException;
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var errorDialog = new ErrorDialog("Something went wrong", e.ExceptionObject as Exception);
errorDialog.ShowDialog();
}
Great. HOWEVER, as soon as the error dialog shows and exits, i.e. the code above returns/completes, the code that triggered the exception resumes exactly where it left off, thus re-triggering the exception. It becomes an infinite loop.
In WPF, we would simply mark it as handled, (e.Handled = true;) and that would take care of it. However I don't see a similar option here in WinForms.
What are my options with Excel Add-Ins VSTO/Win Form exception handlers?
The only thing I can think of is to catch the first event and ask the user to restart the app.
There has to be something better. How can I really handle the exception?

How can I log errors automatically using Serilog?

I am building a console app that basically just make calls to the Database's Store Procedures. This works fine, but now I've came across this little issue - Error Handling.
I'm using Serilog to log any errors I get into a text file, but the issue I'm getting with this is that I have to do a try-catch everywhere. Of course, for a little software, using and put try-catch isn't much of a pain, but when it comes to building large softwares, I'm guessing you can sometime forget to put in error-handling codes?? So I was wondering if there's a way where I can tell Serilog to log and print out the error whenever there is one as a default??
https://stackify.com/catch-unhandled-exceptions-csharp/
static void Main(string[] args)
{
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
//do something with the file contents
}
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
// Log the exception, display it, etc
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
// Log the exception, display it, etc
}

Global exception handler with UnhandledExceptionEventArgs not working

After reading this MSDN page, I've created a global exception handler in my .net class library, for logging purposes, which looks like this:
static void OnException(object sender, UnhandledExceptionEventArgs args)
{
Exception ex = (Exception)args.ExceptionObject;
Logging.LogException(ex);
}
But then if I throw new UnauthorizedAccessException() or throw new Exception() from a method, this does not catch it at all.
The MSDN page says:
UnhandledExceptionEventArgs provides access to the exception object
and a flag indicating whether the common language runtime is
terminating. The UnhandledExceptionEventArgs is one of the parameters
passed into UnhandledExceptionEventHandler for the
AppDomain.UnhandledException event
I believe what I'm doing falls under the AppDomain (and not ThreadException)? What am I doing wrong here?
PS. I'm trying to avoid a try-catch block, since apparently it's bad practice. This class library is called from a windows service which runs periodically so I'd rather not let it 'crash' to avoid memory leaks due to unforeseen exceptions and would prefer to monitor the event logs regularly.
You will need to install the exception handler in the current app domain in order for it to fire:
AppDomain.CurrentDomain.UnhandledException += OnException;
Otherwise its just a method declaration that will never be called.
You mention that you are trying to avoid a try catch, but inside your handler, that wouldn't be a bad idea:
static void OnException(object sender, UnhandledExceptionEventArgs args)
{
try
{
Exception ex = (Exception)args.ExceptionObject;
Logging.LogException(ex);
}
catch
{
// do nothing to silently swallow error, or try something else...
}
}
...Because you don't want to explode in your error handler. Either swallow if stability is of primary importance, or try a secondary (more basic) logging method to insure that no exception falls through the cracks.
Normally, swallowing an exception silently is a poor practice, but this is inside an error handling block where failure means crashing an app.

NLog configured to automatically log all exceptions?

Is there a way to configure NLog to automatically log all exceptions my application can send? Currently I am going to all TRY/CATCH blocks and manually adding logging in the CATCH - but what if I miss some? And what if in the future someone else does
Is there a way to tell NLog to just always log all exceptions? Esspecially some that are not caught and could cause a popup?
As far as I know, there is no way to confineNLog to log all exceptions.
If all you want is to log unhandled exceptions, you could add an "UnhandledException Handler" to the AppDomain when initializing your application.
Note that under some circumstances it may not be possible to log the error (e.g. in case of an OutOfMemory exception or something terrible).
Note that the AppDomain also has a FirstChanceException event you can subscribe to, but this would mean that you get notified about every exception that occurs (and may be handled by the usercode) - in this are many.
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(AppDomain_CurrentDomain_UnhandledException);
static void AppDomain_CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
// use logger here to log the events exception object
// before the application quits
}
Note that this will only allow you to log exceptions that cause your application to crash - you cannot prevent it to crash (therefore the name: unhandled exception).
Another option would be to use Aspect Oriented Programming (AOP) - and introduce a Logging aspect after each method call, in case of an error. If your application uses a Layered architecture, this may be relatively easy to do (e.g. add an aspect to all calls of your business logic layer...).
You may find a framework like PostSharp or Spring.Net useful (usually their websites provide some easy examples for that).
For WebApi application you can do this in Global.asax.cs like that
protected void Application_Error()
{
Exception lastException = Server.GetLastError();
NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
logger.Fatal(lastException);
}
MSDN resource
Jacek's answer is good for WebApi.
Here is an answer for console apps:
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(AppDomain_CurrentDomain_UnhandledException);
Object o = SomeMethodToThrowError(); // Cause an exception
}
static void AppDomain_CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
// use logger here to log the events exception object
// before the application quits
Exception ex = (Exception)e.ExceptionObject;
Logger.Error(ex.Message + " " + ex.StackTrace);
}

.Net Equivalent of set_unexpected()

Is there a .net equivalent to the C++ unexpected()/set_unexpected() functionality?
Edit: Sorry--I omitted some details previously:
Language: C# 2.0
I have some legacy apps that seem to be throwing some unhandled exception somewhere. I just want to put something in place to stop the customer's pain until I can trace the actual source of the problem. In C++, the function pointed at by set_unexpected() gets called, as far as I know, when an otherwise unhandled exception bubbles to the main routine. Hence my question about a .net equivalent functionality.
There 3 possible scenarios for handling unhandled exceptions, based on the type of the application:
For windows forms application, hook an event handler to Application.ThreadException
For commandline applications, hook an event handler to AppDomain.UnhandledException
For ASP.NET applications, in Global.asax, create:
protected void Application_Error(Object sender, EventArgs e)
DISCLAIMER: I'm not c++ developer, but from what I read, this should answer your question.
These handlers should catch most unexpected exceptions in your mixed-mode application.
private delegate long UnhandledExceptionFilter(IntPtr exception);
[DllImport("KERNEL32.DLL", SetLastError = true)]
private static extern IntPtr SetUnhandledExceptionFilter([MarshalAs(UnmanagedType.FunctionPtr)] UnhandledExceptionFilter filter);
// put these in your bootstrapper
AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
Application.ThreadException += ApplicationThreadException;
SetUnhandledExceptionFilter(UnhandledExceptionFilter);
void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
...
}
void ApplicationThreadException(object sender, ThreadExceptionEventArgs e)
{
...
}
long UnhandledExceptionFilter(IntPtr exception)
{
....
}

Categories

Resources