Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to logging unhandled exceptions in ASP.NET Web API 2 running on Azure Web Apps
public class ExceptionLoggerTraceRedirector : ExceptionLogger
{
public override void Log(ExceptionLoggerContext context)
{
var logtext = new StringBuilder();
logtext.AppendLine($"Request: {context.Request}").AppendLine();
logtext.AppendLine($"Exception: {context.Exception}");
Trace.TraceError(logtext.ToString());
}
}
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
config.Services.Replace(typeof(IExceptionLogger), new ExceptionLoggerTraceRedirector());
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment