'ASP.NET Core 6 MVC app custom ExceptionFilter does not catch all exceptions

I have web app with custom exception filter.

public class CustomExceptionFilter : ExceptionFilterAttribute
{
    public override void OnException(ExceptionContext context)
    {
        // do stuff to log exception
    }
}

Exception filter is added to filters inside startup class.

public class Startup
    {
      
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc(options =>
            {
                // ...
                options.Filters.Add(new CustomExceptionFilter());
                // ...
            });
        }        
    }

This custom filter catches almost all non-handled exceptions besides this one.

 Microsoft.AspNetCore.Mvc.ViewFeatures.CookieTempDataProvider
 The temp data cookie .AspNetCore.Mvc.CookieTempDataProvider could not be loaded.
 System.IndexOutOfRangeException: Index was outside the bounds of the array.
    at System.Text.Json.JsonHelpers.TryParseDateTimeOffset(ReadOnlySpan`1 source, DateTimeParseData& parseData)
    at System.Text.Json.JsonHelpers.TryParseAsISO(ReadOnlySpan`1 source, DateTime& value)
    at System.Text.Json.JsonReaderHelper.TryGetEscapedDateTime(ReadOnlySpan`1 source, DateTime& value)
    at System.Text.Json.JsonDocument.TryGetValue(Int32 index, DateTime& value)
    at System.Text.Json.JsonElement.TryGetDateTime(DateTime& value)
    at Microsoft.AspNetCore.Mvc.ViewFeatures.Infrastructure.DefaultTempDataSerializer.DeserializeDictionary(JsonElement rootElement)
    at Microsoft.AspNetCore.Mvc.ViewFeatures.Infrastructure.DefaultTempDataSerializer.Deserialize(Byte[] value)
    at Microsoft.AspNetCore.Mvc.ViewFeatures.CookieTempDataProvider.LoadTempData(HttpContext context)

I'm using TempData to preserve some data between posts and redirects. I've looked at all calls where TempData is used but cannot find the place where this error could show up. This particular error is spat out using Serilog.

My question is why the custom exception filter does not catch this IndexOutOfRangeException? Is there a way to catch them or configure Serilog to be more specific? I would like trace where it comes from to get rid of it.

Follow up

Found similar bug that is described in aspnet core git issues. But my problem is not with some format of string. I get out of range exception even if I check TempData count or Keys.


public static bool HasValue(this ITempDataDictionary tempData, string key)
        {
            try
            {
                if (tempData == null)
                    return false;

                // if no tempData is set, it enters here, generates no
                // exception, but spits out warning through Serilog.
                if (tempData.ContainsKey(key) == false)
                    return false;

                if (tempData.Count == 0)
                    return false;

                return tempData.Peek(key) != null;
            }
            catch (Exception ex)
            {
                // ...
            }

            return false;
        }

Temp solution so I can sleep at night

Adding logging override to serilog configuration.

 Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft.AspNetCore.Mvc.ViewFeatures.CookieTempDataProvider", LogEventLevel.Error);



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source