我有以下代码,目的是不记录某些错误,例如UnauthorizedAccessException。
反正有停止记录的原因,因为它记录了所有内容(不是UnzuthorizeAccessException时有两次记录,但我知道那是因为我手动调用Logger.error)。
app.UseExceptionHandler(appError =>
{
appError.Run(async context =>
{
var contextFeature = context.Features.Get<IExceptionHandlerFeature>();
if (contextFeature != null)
{
string msg = "";
if (contextFeature.Error is UnauthorizedAccessException)
{
context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
msg = "Unauthorised.";
// DO NOT LOG!
}
else {
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
msg = "Server Error!";
Logger.error(contextFeature.Error);
}
await context.Response.WriteAsync(msg);
}
});
});