Logging request / response in ASP.NET MVC web API

Logging ; this is one of the most important activity or aspect of any web application. I won’t list down the reasons and bore you. So I’ll straightaway move into the stuff.

In this article I’ll show you how we can log requests and responses in ASP.Net MVC Web API using log4net. There are multiple frameworks for logging which you can use.But log4net being one of the most popular framework,I’ll use this in my example.

The steps are as follows:
1.First you need to install log4net in your web api application.
log4net

2. Global.asax.cs Configuration:
In Application_start() method you have to write one line of code as shown:
2

3. web.config configuration :
(i)In configSections tag you have got to add a section for log4net.
config section

(ii) In configuration tag you need to add a node /section for log4net.
configuration
4. Action filter:
Now the main activity / part remains as to where we should write the main code for logging.In ASP.NET MVC web API the best place to write it is in an action filter. There are two methods in our action filter:
(i)OnActionExecuting() -This is called just before any the action method which is to be executed.
(ii)OnActionExecuted() – This is called after any action method is executed.
It is in these two methods where logging takes place.

main code

5. Controller coding:
If you want to enable logging for only some particular methods then just put an annotation of the filter above the particular action methods .However if you want to enable logging for each and every method of the controller, just put the annotation”[LogAction]” above the controller “LoggingExampleController” as shown below:
annotation

I am sharing the Source Code
Please download the project and open the “WebApplication1.sln” file in visual studio 2015 and just run it in debug mode.After that fire the URL : http://localhost:62736/api/LoggingExample/get?id=1 to debug the application.
Please note the port no :62736 might vary from machine to machine.

After running the URL you will see log files being generated in the folder “ProdLogs
As you see in the log below i have logged in the controller method that has been invoked along with the parameters.The successful completion of the method has also been logged.status code along with the return data type are the metadata that i have logged.

logs

That’s all needed to enable logging in your web API. You can use the code in my source code to enable logging in your application with the bare minimum change of namespaces.
I hope my post will help you save at least 6-8 hrs of your development work. 🙂

Limitations : This type of logging cannot log those traffic to your API which has wrong / badly formed URL.In some public web API’s you need to store these information as sometimes hackers use brute force or SQL injection to try to hack your web API.But in intranet application where there are almost nil chances of badly formed URLs ,this is definitely a better way to have logging.
You might have wondered after reading this post as to why I have not mentioned error logging which is also important aspect of logging. Well I’ll write about error logging in my future posts.

2 thoughts on “Logging request / response in ASP.NET MVC web API

Add yours

Leave a comment

Create a free website or blog at WordPress.com.

Up ↑