Use of Tracing in Web API2

Introduction

This article explains how to do tracing in Web API 2. Tracing is used for finding the ITracewriter instance. If the ITraceWriter instance is present then we use tracing otherwise tracing is not used.

Features of Tracing:

  • Application Level Tracing
    This type of tracing allows you to see the recent tracing data without restarting the tracing session. It removes the older tracing data and only displays the recent data.

  • Integrating tracing functionality
    The class "System.Diagnostics.Trace" is for the tracing output and we can forward the instrumentation events to this class.

  • Programmatic to trace message
    It allows you to access and manipulate the trace.

Now see the use of tracing in the Web API.

Step 1

First we create a Web API Project as in the following:

  • Start Visual Studio 2013.
  • From the Start Window select "New Project".
  • Select "Installed" -> "Template" -> "Visual C#" -> "Web" and select the "ASP.NET web Application".

    create web application.jpg

  • Click on the "OK" button.

    Select Web API

  • From the Project window select the "Web API" template.

Step 2

Now install the "Microsoft ASP.NET Web API2 Tracing" form the NuGet package manager.

  • Go To tools menu Select "Library Package Manager".
  • The select "Manage Nuget Packages for Solutions".
  • And install the "Microsoft ASP.NET Web API2 Tracing".

    Install Tracing package

Step 3

There are two ways to enable the default TraceWirter, the first is a short route and another is a long route. For the short route add the simple code to the "Global.asax.cs" that is "confog.EnableSystemDiagnosticsTracing()." And in the other, the long route, we can add the class in the App_Start folder named "TracingConfig.cs". So we add a class in this folder:

  • Right-click on the App_Start folder then select "Add" -> "Class".
  • Change the name and click on the "Ok" button.
    Add TraceConfic class

Step 4

Now we add a static method in the "TraceConfig.cs" file that is the Register method.

  1. public static void Register(HttpConfiguration config)  
  2. {  
  3. } 

Step 5

In this step we will ensure that the config is not null; if it is null then it throws an exception. We also create the SystemDiagnosticsTraceWriter instance.

The entire code looks like this:

  1. public class TracingConfig  
  2. {  
  3.     public static void Register(HttpConfiguration config)  
  4.     {  
  5.         if (config == null)  
  6.         {  
  7.             throw new ArgumentNullException("config",  
  8.                 @"Expected type HttpConfiguration.");  
  9.         }  
  10.         SystemDiagnosticsTraceWriter writer = new SystemDiagnosticsTraceWriter()  
  11.         {  
  12.             MinimumLevel = TraceLevel.Info,  
  13.             IsVerbose = false  
  14.         };  
  15.     }  
  16. } 

Step 6

Now the Register method will call where the other configurations are called from the application_start in the global.asax.

  1. protected void Application_Start()  
  2. {  
  3.     AreaRegistration.RegisterAllAreas();  
  4.     GlobalConfiguration.Configure(WebApiConfig.Register);  
  5.     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);  
  6.     RouteConfig.RegisterRoutes(RouteTable.Routes);  
  7.     BundleConfig.RegisterBundles(BundleTable.Bundles);  
  8.     TracingConfig.Register(GlobalConfiguration.Configuration);  
  9. }   

Step 7

Now we run the application and the actual URL navigate with the "http://localhost:6564/api/values" and we can see the bunch of information:

execute application

Output of tracing

Step 8

Now we write the trace logs code from the custom code.

Using the preceding we can see how the ITraceWriter has started the Writing Tracing information. We will also wrap the Tracing method with the WriteToTrace  method. For categorizing your trace it takes a string. In the ValuesController add the following code:

  1. private void WriteToTrace(string category, string log, System.Web.Http.Tracing.TraceLevel level)  
  2. {  
  3.     ITraceWriter trceriter = Configuration.Services.GetTraceWriter();  
  4.     if (trceriter != null)  
  5.     {  
  6.         trceriter.Trace(  
  7.             Request, category, level,  
  8.             (traceRecord) =>  
  9.             {  
  10.                 traceRecord.Message =  
  11.                     String.Format("In this custom code = {0}", log);  
  12.             });  
  13.     }  
  14. } 

Now we will invoke our WriteToTrace method from the Get method that specifies the Trace as a part of the category that is called the Controller.

  1. // GET api/values  
  2. public IEnumerable<string> Get()  
  3. {  
  4.     WriteToTrace("Controllers""There are 2 elements in the return array", TraceLevel.Info);  
  5.     return new string[] { "value1""value2" };  
  6. } 

Now we will again execute the application and navigate to the URL, we will see the additional information in the trace output.

traceinformation


Similar Articles