Introduction
This article explains tracing in the ASP. NET Web API. The use of tracing shows the diagnostic information of a single request of an ASP. NET page.
Working of Tracing
- Tracing is used to follow the path of the execution page.
- It displays the diagnostic information of the the execution page at run time.
- It is used to debug the application.
- It can be integrated with the system level tracing that provides multiple levels of tracing.
Features of Tracing
- Application level tracing
This 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 messages
It allows you to access and manipulate the trace messages by the code.
Tracing in Web API
In the ASP.NET Web API we can use our choice of tracing or logging library. In that we use the System.Diagnostics.Trace class.
Here is an example. There is a class that is implemented with the ITraceWriter interface.
public class trace:ITraceWriter
{
public void Trace(HttpRequestMessage reqt, string type, TraceLevel step,
Action<TraceRecord> traceAction)
{
TraceRecord record = new TraceRecord(reqt, type,step);
traceAction(record);
WriteTrace(record);
}
protected void WriteTrace(TraceRecord record)
{
var msg = string.Format("{0};{1};{2}",
record.Operator, record.Operation, record.Message);
System.Diagnostics.Trace.WriteLine(msg, record.type);
}
ITraceWriter Interface: There is a "Trace()" method for calling the trace action. It allows the setting of a value in the TraceRecord. Here we use the step, type,
traceAction and request that are the parameters of the ITraceWriter.
-
HttpRequestMessage: We use the parameter "reqt" that is associated within the formation of the trace.
-
type: It is a user defined string parameter that is associated with the trace.
-
TraceLevel: There are we use the "step" parameter that specify the level of the trace.
-
traceAction: It invokes the action that accepts the trace records.
When we use the tracing in the Web API and for enabling the tracing in it, we need to implement our web API with the "ITraceWriter" Interface. Here we use the object
of the "HttpConfiguration" for doing this.
For example:
public static void Register(HttpConfiguration config)
{
config.Services.Replace(typeof(ITraceWriter), new trace());
}
For replacing the default traces we invoke "HttpConfigration.Services.Replace".
Add the Trace to the code
Add the trace to the code using this code:
public class DetailsController : ApiController
{
public HttpResponseMessage GetAllDetails()
{
Configuration.Services.GetTraceWriter().Info(
Request,"DetailsController","Get the all details of the Students");
}
}
Here we invoke the "HttpConfiguration.Services.GetTraceWriter" for getting the trace and it is accessed by the property of the "ApiController.Configuration".
Working of the Web AP Tracing
-
At the time of enabling the tracing in the Web API, the Web API wraps the various parts of the request that are used for calling the trace.
-
We use an interface the "IHttpControllerSelector" to select the controller.
-
If we do not use the ITraceWriter interface then the components of the tracing are not instantiated and do not provide any performance.
Process diagram of Tracing