A global exception handler is used to catch all unhandled exceptions within a given application. To handle exceptions globally you can use a built in .NET solution, extend a solution or use pre-built solutions. Today we’ll focus on a pre-built solution by ClearInsights.
Overview
ClearInsights logging provides integrated exception handling for the .NET framework. You can log exceptions within a try-catch block or implement their easy to integrate global exception handling. A huge benefit to using ClearInsights logging is that new exceptions are automatically added to your team’s backlog when integrated (Ex. Jira project backlog).
ClearInsights Global Exception Handling: Example
Below is an example code showing how to use ClearInsights logging withing your VB.NET applications to handle global exceptions.
Imports ClearInsights.Logging Imports Microsoft.Extensions.DependencyInjection Imports Microsoft.Extensions.Logging Module Program Sub Main(args As String()) Dim serviceCollection As New ServiceCollection() ConfigureServices(serviceCollection) Dim serviceProvider = serviceCollection.BuildServiceProvider() 'This will automatically catch any unhandled exceptions System.AppDomain.CurrentDomain.UseClearInsightsExceptionHandling(serviceProvider.GetService(Of ILogger(Of AppDomain))) End Sub Private Sub ConfigureServices(ByVal services As IServiceCollection) services.AddLogging(Function(configure) Return configure.AddClearInsightsLogger(Sub(configuration) configuration.ApiKey = "{ApiKey}" configuration.Secret = "{Environment Client Secret}" configuration.ApplicationName = "{Your Application Name}" End Sub) End Function).AddTransient(Of SomeClass)() End Sub End Module
That’s it! Adding these few lines of code to your VB.NET application will enable you to catch all unhandled exceptions and automatically add them to your team’s backlog.
ClearInsights Centralized Application
Logging & Monitoring
Fully integrated application logging and monitoring – enabling software teams and businesses to build fast while having clear insight into their products and applications.
Add ClearInsights Logging with just a few lines of code and automically push new found errors directly to your teams backlog.
builder.Logging.AddClearInsightsLogger(configuration =>
{
configuration.ApiKey = "{ApiKey}";
configuration.Secret = "{Environment Client Secret}";
configuration.ApplicationName = "{Application Name}";
});
//Add to use ClearInsights global error handling.
//This will automatically catch any unhandled exceptions
app.UseClearInsightsExceptionHandling(options =>
{
//Add to extend the error handler and add additional logic.
//Add logic like custom HTTP response, etc...
options.OnError += (sender, arg) =>
{
var response = "Oops something went wrong";
arg.HttpContext.Response.ContentType = "text/html";
arg.HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
arg.HttpContext.Response.WriteAsync(response);
};
});
ClearInsights community plan is always free for small projects
builder.Logging.AddClearInsightsLogger(configuration =>
{
configuration.ApiKey = "{ApiKey}";
configuration.Secret = "{Environment Client Secret}";
configuration.ApplicationName = "{Application Name}";
});
//Add to use ClearInsights global error handling.
//This will automatically catch any unhandled exceptions
app.UseClearInsightsExceptionHandling(options =>
{
//Add to extend the error handler and add additional logic.
//Add logic like custom HTTP response, etc...
options.OnError += (sender, arg) =>
{
var response = "Oops something went wrong";
arg.HttpContext.Response.ContentType = "text/html";
arg.HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
arg.HttpContext.Response.WriteAsync(response);
};
});