private ExceptionHandlerMiddleware CreateMiddleware(
            RequestDelegate next,
            IOptions <ExceptionHandlerOptions> options)
        {
            next ??= c => Task.CompletedTask;
            var listener = new DiagnosticListener("Microsoft.AspNetCore");

            var middleware = new ExceptionHandlerMiddleware(
                next,
                NullLoggerFactory.Instance,
                options,
                listener);

            return(middleware);
        }
            static async Task Awaited(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
            {
                ExceptionDispatchInfo? edi = null;
                try
                {
                    await task;
                }
                catch (Exception exception)
                {
                    // Get the Exception, but don't continue processing in the catch block as its bad for stack usage.
                    edi = ExceptionDispatchInfo.Capture(exception);
                }

                if (edi != null)
                {
                    await middleware.HandleException(context, edi);
                }
            }