Asp.net core ASP.NET核心MVC错误处理禁用默认处理
我添加了medleware登录错误Asp.net core ASP.NET核心MVC错误处理禁用默认处理,asp.net-core,Asp.net Core,我添加了medleware登录错误 public class ErrorLoggingMiddleware { private readonly RequestDelegate _next; private readonly ILogger _logger; public ErrorLoggingMiddleware(RequestDelegate next, ILogger<ErrorLoggingMiddleware> logger) {
public class ErrorLoggingMiddleware {
private readonly RequestDelegate _next;
private readonly ILogger _logger;
public ErrorLoggingMiddleware(RequestDelegate next, ILogger<ErrorLoggingMiddleware> logger) {
_next = next;
_logger = logger;
}
public async Task Invoke(HttpContext context) {
try {
await _next(context);
} catch (Exception e) {
string user = "", host = "";
try {
user = context.User?.Identity.Name;
} catch {
user = "Can't get the User!";
}
try {
host = context.Request.Host.ToString();
} catch {
host = "Can't get the Host!";
}
_logger.LogError($"User='{user}'\r\nHost='{host}'\r\n{e}");
throw;
}
}
}
SmtpLogger发送错误成功,但我收到两条消息,而不是一条
如果我在中间件中发表评论,那么我只收到一条消息。但如果出现错误,则视图为空。
如何禁用默认错误处理和关于错误的第二条消息。默认消息,如“连接ID”18230571306091282804,“请求ID”(null)”:应用程序引发了未处理的异常
我试图注释启动文件中的许多行,但没有任何帮助。
我没有看到调用堆栈默认消息。。。不知道是谁叫它问题是开发环境。它添加了microsoft.aspnetcore.diagnostics.exceptionhandlermiddleware,并执行其他日志记录。要解决此问题,需要将Env设置为Production您是否尝试查看请求路径context.request.path?@ricardopers我在Home\Index中抛出了新的异常,并且在context.request.path中看到了此路径
public static class ErrorLoggingMiddlewareExtensions {
public static IApplicationBuilder UseErrorLogging(this IApplicationBuilder builder) {
return builder.UseMiddleware<ErrorLoggingMiddleware>();
}
}
app.UseErrorLogging();