// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, Services.IloggingProvider logger) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseHsts(); } app.UseAuthentication(); //configure all exeptions in one place. app.ConfigureExceptions(logger); app.UseHttpsRedirection(); app.UseMvc(); app.ConfigureDoc(); }
public static void ConfigureExceptions(this IApplicationBuilder app, Services.IloggingProvider logger) { app.UseExceptionHandler(config => { config.Run(async context => { context.Response.StatusCode = 500; context.Response.ContentType = "application/json"; var error = context.Features.Get <IExceptionHandlerFeature>(); if (error != null) { var ex = error.Error; logger.Error(ex); await context.Response.WriteAsync(new Models.ErrorModel() { StatusCode = 500, ErrorMessage = ex.Message }.ToString()); //ToString() is overridden to Serialize object } }); }); }