Learn ASP.NET Web API 2 Tracing Part 1

Rama Sagar
Posted by in ASP.NET Web API category on for Beginner level | Points: 250 | Views : 18345 red flag

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.


Objective


The objective of this article is to know about tracing in Web API 2 and its features.


Lets start  creating a Web API project

  •  Step 1: Create a New Project  Open visual studio 2013 -> click on File -> New Project -> Create new ASP.NET Web Application -> Name it as TracingWebAPI2Demo


 select WEB API template and click on ok


  • Step 2:Now Lets install  Microsoft ASP.NET Web API2 Tracing package through Library package manager
            Open Package Manager console
       
           Click on Tools -> Library Package Manager ->Package Manager Console and And type the following                          command:

 Install-Package Microsoft.AspNet.WebApi.Tracing -Version 5.0.0







  • Step 3:Right-click on the App_Start folder then select "Add" -> "Class".name it as configTrace.cs



Using the code

In the Register method, we first ensure config is not null and if it is, throw an exception. Next we create a new instance of the SystemDiagnosticsTraceWriter. This is the default TraceWriter that comes with the Tracing package mentioned above and it uses System.Diagnostics to do the trace logging. So while debugging, we can see the Logs in the Output windows itself.

As seen below we are setting up the Logger with Tracing level to Info (so it will trace and log pretty much everything) and the IsVerbose is set to false. The complete Register method is as follows

using System;
using System.Web.Http;
using System.Web.Http.Tracing;

namespace TracingWebAPI2Demo.App_Start
{
    public class configTrace
    {
        public static void Register(HttpConfiguration config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config",
                    @"Expected type HttpConfiguration.");
            }
            SystemDiagnosticsTraceWriter writer = new SystemDiagnosticsTraceWriter()
            {
                MinimumLevel = TraceLevel.Info,
                IsVerbose = false
            };
        }
    }
}


Conclusion

In this article we have seen how to get started with tracing in WEB API 2.In Next part we will see the complete implementation

Reference

http://blogs.msdn.com/b/roncain/archive/2012/08/16/asp-net-web-api-tracing-preview.aspx

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
ramasagar
http://www.ramasagar.com
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)