Learn ASP.NET Web API 2 Tracing Part 2

Rama Sagar
Posted by in ASP.NET category on for Beginner level | Points: 250 | Views : 15170 red flag
Rating: 4 out of 5  
 1 vote(s)

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.”


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()

  • 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

                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }

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)
                    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" };


In this article we have seen how to implement tracing in web API 2



Page copy protected against web site content infringement by Copyscape

About the Author

Rama Sagar
Full Name: RamaSagar Pulidindi
Member Level: Silver
Member Status: Member,MVP
Member Since: 12/30/2012 1:51:40 AM
Country: India
A Software Profesional working in Microsoft .NET technologies since year 2008, and I work for Dake ACE. I am passionate about .NET technology and love to contribute to the .NET community at Dot Net Funda

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)