ASP.NET WebApi IExceptionLogger nicht zu fangen Ausnahmen

Ich versuche zum einrichten einer globalen exception-handler, wie hier beschrieben: Web-API-Globale Fehlerbehandlung. Ich habe setup ein Fall, wo eine Ausnahme wird geworfen in einem controller-Konstruktor. Aber meine Ausnahme ist nicht immer protokolliert. Stattdessen WebApi ist einfach wieder die Ausnahme und die volle stack-trace der an den aufrufenden client als JSON-Nachricht.

Ich weiß nicht, ob es eine Rolle spielt, aber meine Controller-Aktionen werden mit async /await, wie diese:

[HttpGet, Route("GetLocationNames")]
public async Task<IEnumerable<String>> Get()
{
    return await adapter.GetLocationNames();
}

Habe ich die folgende Implementierung:

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);
        }
    }
}

Und ich bin registriert wie dieses:

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;
        }
    }
}

Und hier ist, meine Pakete.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>

InformationsquelleAutor Mark J Miller | 2014-05-29

Schreibe einen Kommentar