Exemplo n.º 1
0
        public static void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory,
                                     IWriteToFileText writeToFileText)
        {
            app.UseExceptionHandler(errorApp =>
            {
                errorApp.Run(async context =>
                {
                    var errorFeature        = context.Features.Get <IExceptionHandlerFeature>();
                    var exception           = errorFeature.Error;
                    var isConnectionTrusted = context.Request.IsTrusted();

                    var problemDetails = ProblemDetailsFactory.Build(exception, isConnectionTrusted);

                    var logger = LoggerConfigurations.Configure(loggerFactory, writeToFileText);

                    var logString =
                        $"Processing request {context.Request.Path},\n Status Code: {problemDetails.Status.Value},\n Details:{exception.Demystify().ToString() ?? problemDetails.Detail}\n";

                    if (!(exception is BusinessLogicException))
                    {
                        logger.LogError(logString);
                    }

                    context.Response.StatusCode = problemDetails.Status.Value;
                    context.Response.WriteJson(problemDetails, "application/problem+json");
                });
            });
        }