Skip to content

Instantly share code, notes, and snippets.

@jsakamoto
Created December 25, 2015 02:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsakamoto/9bb33b7ca1c380af288c to your computer and use it in GitHub Desktop.
Save jsakamoto/9bb33b7ca1c380af288c to your computer and use it in GitHub Desktop.
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