// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseMiddleware <CorrelationIdMiddleware>(); app.UseSerilogRequestLogging(options => { options.EnrichDiagnosticContext = (diagnosticContext, httpContext) => { diagnosticContext.Set("CorrelationId", CorrelationIdMiddleware.GetCorrelationId(httpContext).Value); }; }); app.UseDeveloperExceptionPage(); app.UseSwagger(); app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Weather v1")); app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseMiddleware <ErrorHandelingMiddleware>(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
private static Task HandleExceptionAsync(HttpContext context, Exception exception) { string correlationId = CorrelationIdMiddleware.GetCorrelationId(context).Value.ToString(); var result = new { RequestId = context.TraceIdentifier, CorrelationId = correlationId, Message = "An error occurred in our API. Please refer to the request id with our support team.", }; Log.Error(exception, exception.Message); var response = new ObjectResult(result) { StatusCode = StatusCodes.Status500InternalServerError }; var actionContext = new ActionContext() { HttpContext = context }; return(response.ExecuteResultAsync(actionContext)); }