コード例 #1
0
        public ApiExceptionHandlerMiddleware(
            RequestDelegate next,
            IOptions <ApiExceptionHandlerOptions> options,
            ILoggerFactory loggerFactory,
            IHostingEnvironment hostingEnvironment)
        {
            this.next    = next ?? throw new ArgumentNullException(nameof(next));
            this.options = options?.Value ?? throw new ArgumentException(nameof(options));
            logger       = loggerFactory.CreateLogger <ApiExceptionHandlerMiddleware>();

            // If displaying the stack trace option has not been provided, default to true if development, false otherwise
            doIncludeDebugInfo = this.options.IncludeDebugInfo.HasValue ?
                                 this.options.IncludeDebugInfo.Value :
                                 hostingEnvironment.IsDevelopment();
        }
コード例 #2
0
 /// <summary>
 /// Adds middleware that will handle exceptions for requests and return an object with the exception data.
 /// </summary>
 /// <param name="app"></param>
 /// <param name="options"></param>
 /// <returns></returns>
 public static IApplicationBuilder UseApiExceptionHandler(this IApplicationBuilder app, ApiExceptionHandlerOptions options)
 {
     if (app == null)
     {
         throw new ArgumentNullException(nameof(app));
     }
     if (options == null)
     {
         throw new ArgumentNullException(nameof(options));
     }
     return(app.UseMiddleware <ApiExceptionHandlerMiddleware>(Options.Create(options)));
 }