Tracing the Application

One of the most challenging things with remotely deployed code is debugging when something has gone wrong. Web API enables a very rich automatic tracing ecosystem that is turned off by default but can be enabled by the developer as needed. The built-in tracing functionality wraps many of the built-in components and can correlate data from individual requests as it moves throughout the layers of the system.

The central part of tracing is the ITraceWriter service. Web API does not ship with any implementations of this service, as it is anticipated that developers will likely already have their own favorite tracing system (such as ETW, log4net, ELMAH, or many others). Instead, Web API looks on startup to see if an implementation of ITraceWriter is available in the service list, and if so, automatically begins tracing all requests. The developer must choose how best to store and browse this trace information — typically, by using the configuration options provided by their chosen logging system.

Application and component developers can also add tracing support to their systems by retrieving the ITraceWriter service and, if it's not null, writing tracing information to it. The core ITraceWriter interface only contains a single Trace method, but there are several extension methods which are designed to make it easy to trace different levels of messages (debug, info, warning, error, and fatal messages) as well as helpers to trace entry and exit to both synchronous ...

Get Professional ASP.NET MVC 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.