c# - ASP.NET WebApi IExceptionLogger doesn't catch exceptions -
i'm trying setup global exception handler outlined here: web api global error handling. i"ve setup case exception gets thrown within controller constructor. exception isn't getting logged. instead webapi returning exception , full stack trace calling client json message.
i don't know if matters, controllers actions using async / await this:
[httpget, route("getlocationnames")] public async task<ienumerable<string>> get() { return await adapter.getlocationnames(); }
i have following implementation:
using log4net; using system.threading.tasks; using system.web.http.exceptionhandling; namespace warehouse.management.api { public class log4netexceptionlogger : exceptionlogger { private ilog log = logmanager.getlogger(typeof(log4netexceptionlogger)); public async override task logasync(exceptionloggercontext context, system.threading.cancellationtoken cancellationtoken) { log.error("an unhandled exception occurred.", context.exception); await base.logasync(context, cancellationtoken); } public override void log(exceptionloggercontext context) { log.error("an unhandled exception occurred.", context.exception); base.log(context); } public override bool shouldlog(exceptionloggercontext context) { return base.shouldlog(context); } } }
and i'm registering this:
using owin; using system.web.http; using system.web.http.exceptionhandling; namespace warehouse.management.api.config { public static class webapiconfig { public static iappbuilder registerapiconfig(this iappbuilder app, httpconfiguration config) { config.maphttpattributeroutes(); config.services.add(typeof(iexceptionlogger), new log4netexceptionlogger()); return app; } } }
and here's packages.conf:
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="log4net" version="2.0.3" targetframework="net45" /> <package id="microsoft.aspnet.webapi.client" version="5.1.2" targetframework="net45" /> <package id="microsoft.aspnet.webapi.core" version="5.1.2" targetframework="net45" /> <package id="microsoft.aspnet.webapi.owin" version="5.1.2" targetframework="net45" /> <package id="microsoft.aspnet.webapi.webhost" version="4.0.20505.0" targetframework="net45" /> <package id="microsoft.owin" version="2.1.0" targetframework="net45" /> <package id="microsoft.web.infrastructure" version="1.0.0.0" targetframework="net45" /> <package id="newtonsoft.json" version="4.5.11" targetframework="net45" /> <package id="owin" version="1.0" targetframework="net45" /> <package id="unity" version="3.5.1404.0" targetframework="net45" /> <package id="unity.aspnet.webapi" version="3.5.1404.0" targetframework="net45" /> <package id="webactivatorex" version="2.0" targetframework="net45" /> </packages>
turns out ordering. in startup class (not shown) moved call registerapiconfig method (above, in op) before call usewebapi , works.
Comments
Post a Comment