-
Notifications
You must be signed in to change notification settings - Fork 378
/
Copy pathExceptionExtension.cs
30 lines (28 loc) · 1.09 KB
/
ExceptionExtension.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http;
namespace SampleWebApiAspNetCore.Service.Helpers
{
public static class ExceptionExtension
{
public static void AddProductionExceptionHandling(this IApplicationBuilder app, ILoggerFactory loggerFactory)
{
app.UseExceptionHandler(errorApp =>
{
errorApp.Run(async context =>
{
context.Response.StatusCode = 500;
context.Response.ContentType = "text/plain";
var errorFeature = context.Features.Get<IExceptionHandlerFeature>();
if (errorFeature != null)
{
var logger = loggerFactory.CreateLogger("Global exception logger");
logger.LogError(500, errorFeature.Error, errorFeature.Error.Message);
}
await context.Response.WriteAsync("There was an error");
});
});
}
}
}