Open Closed

How to filter specific Exceptions? #6496


User avatar
0
william@iwell.nl created
  • ABP Framework version: v7
  • UI Type: Angular
  • Database System: EF Core (SQL Server)
  • Tiered (for MVC) or Auth Server Separated (for Angular): Auth server angular

When a request is cancelled by the browser, the logs show an error (see below). However this is expected behaviour and should not be logged as error as it clutters our logs. Can you guide us in how to filter this error message from the logs?

I have read the documentation about exception handling, but did not find a way https://docs.abp.io/en/abp/latest/Exception-Handling

  1. I've created a SuppressTaskCanceledExceptionFilter. Is that the best way to go, as it inherits and overrides the AbpExceptionFilter? Better to add another filter, is there a way to do so?
  2. After creating the filter I see the message is partially gone. Only the 'An error occurred using the connection to database' stays. How to get rid of that line?
[15:48:35 ERR] An error occurred using the connection to database 'portal-test' on server '.'.
[15:48:35 ERR] ---------- RemoteServiceErrorInfo ----------
{
  "code": null,
  "message": "An internal error occurred during your request!",
  "details": null,
  "data": {},
  "validationErrors": null
}

[15:48:35 ERR] A task was canceled.
System.Threading.Tasks.TaskCanceledException: A task was canceled.
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternalAsync(Boolean errorsExpected, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternalAsync(Boolean errorsExpected, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Stor
using Microsoft.AspNetCore.Mvc.Filters;
    using System.Threading.Tasks;
    [ExposeServices(typeof(AbpExceptionFilter), typeof(IAsyncExceptionFilter))]
    public class SuppressTaskCanceledExceptionFilter : AbpExceptionFilter, ITransientDependency
    {
        public override Task OnExceptionAsync(ExceptionContext context)
        {
            if (context.Exception is TaskCanceledException)
            {
                context.ExceptionHandled = true; // This will suppress the logging
                return Task.CompletedTask;
            }

            return base.OnExceptionAsync(context);
        }
    }

6 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can filter it by logger.

    https://github.com/serilog/serilog-expressions?tab=readme-ov-file#filtering-example

    var loggerConfiguration = new LoggerConfiguration()
        .MinimumLevel.Debug()
        .Filter.ByExcluding(logEvent =>
        {
            //check the exception
    
            return false or true;
        })
        .WriteTo.Console();
    
  • User Avatar
    0
    william@iwell.nl created

    Thanks, adding the filter will remove the [15:48:35 ERR] A task was canceled. System.Threading.Tasks.TaskCanceledException: A task was canceled. line.

    However 3 other lines are still there. Any suggestion how to suppress/filter those?

    [16:46:01 ERR] An error occurred using the connection to database 'portal-test' on server '.'. [16:46:01 ERR] ---------- RemoteServiceErrorInfo ---------- { "code": null, "message": "An internal error occurred during your request!", "details": null, "data": {}, "validationErrors": null }

    [16:46:01 ERR] HTTP GET /api/app/cubes?skipCount=0&maxResultCount=20 responded 500 in 1303.3537 ms

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Replace AbpExceptionFilter and AbpExceptionPageFilter with yours

    context.Services.Configure<MvcOptions>(options =>
    {
        options.Filters.ReplaceOne(
            f => f is ServiceFilterAttribute fa && fa.ServiceType == typeof(AbpExceptionFilter),
            new ServiceFilterAttribute(typeof(YourAbpExceptionFilter))
        );
    
        options.Filters.ReplaceOne(
            f => f is ServiceFilterAttribute fa && fa.ServiceType == typeof(AbpExceptionPageFilter),
            new ServiceFilterAttribute(typeof(YourAbpExceptionPageFilter))
        );
    });
    

    https://github.com/abpframework/abp/blob/rel-8.0/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs#L83

    https://github.com/abpframework/abp/blob/rel-8.0/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs#L68

  • User Avatar
    0
    william@iwell.nl created

    Thanks, the adding the filter indeed removes two of the three log entries.

    There is just one entry left, see below. Any idea how to suppress/filter that one?

    [16:46:01 ERR] An error occurred using the connection to database 'portal-test' on server '.'.
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    I think you can exclude this message by ByExcluding

  • User Avatar
    0
    william@iwell.nl created

    Thanks, that works. Added it like this.

    Log.Logger = new LoggerConfiguration()
                   .Filter.ByExcluding(logEvent => logEvent.MessageTemplate.Text.StartsWith("An error occurred using the connection to database"))
                   .ReadFrom.Configuration(configuration)
                    .CreateLogger();
    
Made with ❤️ on ABP v9.1.0-preview. Updated on December 30, 2024, 14:53