Created
December 25, 2015 02:05
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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