I thought about sending all exceptions created in the programm to a server, but didn't find a good solution. My idea was to create a base class, which, with in the constructor, adds the exception to a queue. A sender does wait on the queue and sends all registered exceptions to the server. But at the time the exceptions is registered the constructor is not done, which means it is possibile not all values are set when it is send to the server. Is waiting a set amount of ms a good solution or is there a better way to collect all exceptions, that does not require me to do it for every exception/catch manually? What type of connection should be used? Setting up a tpc connection is expensiv, should I implement a simple UDP based protocol, or is that overkill?
CodePudding user response:
It's better to use existing ones rather than write custom error catchers. Using a nlog style solution makes the project more universal.
CodePudding user response:
You can look at a article by Jason Taylor on how to do this. Here is his github source code link.
I also wrote a simplified example for you. Hope this helps.
Exception filter:
using Microsoft.AspNetCore.Mvc.Filters;
namespace Multitrust.API.Filters
{
public class ApiExceptionFilter : ExceptionFilterAttribute
{
public ApiExceptionFilter()
{
}
public override void OnException(ExceptionContext context)
{
HandleException(context);
}
private void HandleException(ExceptionContext context)
{
//This will catch all exceptions, you can them log them here. I recommend using serilog.
}
}
}
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews(options =>
options.Filters.Add(new ApiExceptionFilter()));
//Bind other services...
}
Let me know if you need any help with this.
Happy coding!