Tracing is a notification mechanism that allows framework and application code to say things like “Object A is now performing operation B” or “Object A encountered exception E when performing operation B.”
Introduction
This article demonstrates how to enable tracing in Web API 2 .We can use this feature to trace what the Web API framework does before and after it invokes our controller. We can also use it to trace our own code.Previous article has been explained about how to get started..In this part we will continue with the remaining
- Step 1:Now lets call the register method in global.asax file which we have created earlier
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using TracingWebAPI2Demo.App_Start;
namespace TracingWebAPI2Demo
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
configTrace.Register(GlobalConfiguration.Configuration);
}
}
}
- Step 2:Open WebApiConfig.cs file from App_Start folder and enable tracing
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace TracingWebAPI2Demo
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.EnableSystemDiagnosticsTracing();
}
}
}
let’s run the default application and navigate to the URL http://localhost:[port#]/api/values . As we can see below, a big bunch of Information logs came in with the details of the Operation, the path of execution as well as Response status.
- Step 3:Now lets write into Trace Logs from Custom Code
Open ValuesController file from controllers folder and add the following implementation
This method takes in a string for categorizing our traces, the actual string that we want to log and the TraceLevel at which we want to log.
private void WriteToTrace(string category, string log, System.Web.Http.Tracing.TraceLevel level)
{
System.Web.Http.Tracing.ITraceWriter trcewriter = Configuration.Services.GetTraceWriter();
if (trcewriter != null)
{
trcewriter.Trace(
Request, category, level,
(traceRecord) =>
{
traceRecord.Message =
String.Format("In this custom code = {0}", log);
});
}
}
From the Get method we call our WriteToTrace method that specifies the Trace as a part of the Category called Controllers with the log string specifying how many elements being returned and the TraceLevel is set to Info.
// GET api/values
public IEnumerable<string> Get()
{
WriteToTrace("Controllers", "2 elements are in the return array", TraceLevel.Info);
return new string[] { "value1", "value2" };
}
Conclusion
In this article we have seen how to implement tracing in web API 2
Reference
http://blogs.msdn.com/b/roncain/archive/2012/08/16/asp-net-web-api-tracing-preview.aspx