Presenting HttpTracer Library

httptracer

Hello devs! Today I would like to share with you a OSS lib that me and a couple of friends @DylanBerry and @ChaseFlorell have developed, its the HttpTracer lib.

We oftentimes find our selves trying to figure out how to setup a proper way to see the requests and response information that our software exchange with backends. We all have suffered before setting up fiddler when debugging mobile apps in real iOS or Android devices. Your problems to reach informations such as request type, URL, headers, response body are over now :).

Nuget Link here

Project Github Here

68747470733a2f2f64616e69656c636175736572626c6f672e66696c65732e776f726470726573732e636f6d2f323031382f30342f7472616365725f74726163696e675f676574312e676966

Why you should use it?

As you can see in the animation above, our lib will pick up the HttpClient request and response content and dump it in your console output, making your life easier.

Don’t worry about flooding your output window, our logger is actually an Interface, so you can implement your own logger, this will allow you to write this information anywhere you want, like in this sample, will write all your logs to a HttpLog.txt file!

using HttpTracer;
private async Task TraceFileClick()
{
var client = new HttpClient(new HttpTracerHandler(new MyLogger()));
client.DefaultRequestHeaders.Add("client-version", "1.0.0");
client.DefaultRequestHeaders.Add("custom-header", "Hi Mark");
try
{
var result = await client.GetAsync("http://myserviceurl.com");
}
catch (Exception e)
{
Console.WriteLine(e);
}
}

public class MyLogger : ILogger
{
public void Log(string message)
{
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(@"C:\HttpLog.txt")
{
file.WriteLine(message);
}
}
}

We made sure to use Debug.Writeline(); on our default logger, which will only write this information when you are running your software in debug mode.

How?

It is really easy to start using and debugging your Http requests, just add an instance of HttpTracerHandler to your HttpClient creation and start picking up the traces in your Visual Studio console window.

using HttpTracer;
public async Task GetMyData()
{
var tracer = new HttpTracerHandler();
var client = new HttpClient(tracer);
var result = await client.GetAsync("http://myserviceurl.com");
}

If you happen to use custom Http Handlers in your project, we suggest you use our Http handler builder:

using HttpTracer;
public async Task GetMyData()
{
var builder = new HttpHandlerBuilder();

builder.AddHandler(new MyHandler3())
.AddHandler(new MyHandler2())
.AddHandler(new MyHandler1());

var client = new HttpClient(builder.Build());
var result = await client.GetAsync("http://myserviceurl.com");
}

What is next?

Our Plan is to make the the message stream customizable, giving the users of this lib the power to choose what information they would like to see.

Another idea is to create a Visual Studio extension to isolate the data in a default window and make it colourful, which will simplify succeeded and failed requests.
throw new CauserException();

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s