【发布时间】:2020-08-14 11:50:30
【问题描述】:
我正在尝试在我们的一个 ASP.NET Web API(非核心)项目(运行 .NET 4.7.2)上实现错误处理,以避免将来遇到错误,我们有几天前。
错误是Web.Config文件中的这段代码没有更新,但是项目发布时自带的DLL更新了,所以版本不匹配。
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
</compilers>
</system.codedom>
compiler 两行中的Version=2.0.1.0 均设置为Version=1.0.8.0,并且发布的 DLL 版本为 2.0.1.0。这给出了一个看起来像这样的错误:
现在,我想在代码中处理这个异常,并将其记录到我们的日志堆栈中。我四处搜索并发现了几种在 web.api 项目中捕获异常的不同方法,但它们似乎都没有奏效。我试过的有:
在global.asax.cs 文件中:
void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
if (ex != null)
Logger.WriteToFile(ex.Message, ex);
}
也在global.asax.cs 文件中:
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Exception ex = (e.ExceptionObject as Exception);
if (ex != null)
Logger.WriteToFile(ex.Message, ex);
}
在自动添加到 Web.API 的 WebApiConfig.Register 函数中(例如,处理路由设置):
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Filters.Add(new GlobalExceptionFilterAttribute()); // <-- this line
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
public class GlobalExceptionFilterAttribute : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext context)
{
var exceptionType = context.Exception.GetType().ToString();
var exception = context.Exception.InnerException ?? context.Exception;
try
{
if (exception != null)
Logger.WriteToFile(exception.Message, exception);
}
finally
{
base.OnException(context);
}
}
}
在 web.config 文件中,我添加了一个应该(理论上)处理 IIS 级别错误的模块(取自 Catch IIS level error to handle in ASP.NET):
<system.webServer>
<modules>
<add type="ErrorHttpModule" name="ErrorHttpModule"/>
</modules>
</system.webServer>
public class ErrorHttpModule : IHttpModule
{
private HttpApplication _context;
public ErrorHttpModule() { }
public void Init(HttpApplication context)
{
_context = context;
_context.Error += new EventHandler(ErrorHandler);
}
private void ErrorHandler(object sender, EventArgs e)
{
Exception ex = _context.Server.GetLastError();
if (ex != null)
Logger.WriteToFile(ex.Message, ex);
}
public void Dispose()
{
}
}
而且这些似乎都没有捕获错误并记录它。我如何捕捉到这个错误?
除了我们的主项目之外,我还在一个完全空白的 Web.API 项目中重现了这个错误。空白 Web.API 项目可以在 https://github.com/Loyalar/IISExceptionCapture 找到。
【问题讨论】:
标签: c# asp.net exception iis asp.net-web-api