Click here to Skip to main content
15,867,686 members
Articles / Web Development / IIS
Tip/Trick

HttpModule for Logging HTTP POST Data in IIS Log

Rate me:
Please Sign up or sign in to vote.
4.92/5 (8 votes)
31 Oct 2017CPOL2 min read 44.8K   13   6
Simple trick that allows you to log HTTP POST data in standard IIS log, without rewriting existing web application

Introduction

Sometimes in ASP.NET applications, there is a need to log HTTP POST data for future analysis (incident management, statistics, etc.). IIS logs HTTP GET parameters out of the box, using Standard Logging functionality, but this tip is about storing HTTP POST data, that is little bit trickier. There is another feature, called Advanced Logging, that allows you to store any data in IIS log, but in this case you need to post data manually, that is not very useful, when you do not want to change the existing web application.

Background

Fortunately, there are two great features you can use use to solve this problem. The first is Response.AppendToLog, that instructs ASP.NET to append user provided data to standard IIS log, generated by the server (you can call it at any point in ASP.NET net pipeline). The second one is HttpModule that can be used to do the trick without changing the existing web application.

Using the Code

Just create a new Class Library in your solution and add a new class, that implements IHttpModule. Subscribe to BeginRequest event in Init method and process data you want to be appended to the log. The following example logs all data for every POST request:

C#
using System;
using System.Web;

namespace MySolution.HttpModules
{
    public class HttpPOSTLogger : IHttpModule
    {
        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }

        private void context_BeginRequest(object sender, EventArgs e)
        {
            if (sender != null && sender is HttpApplication)
            {
                var request = (sender as HttpApplication).Request;
                var response = (sender as HttpApplication).Response;

                if (request != null && response != null && request.HttpMethod.ToUpper() == "POST")
                {
                    var body = HttpUtility.UrlDecode(request.Form.ToString());
                    if (!string.IsNullOrWhiteSpace(body))
                        response.AppendToLog(body);
                }
            }
        }
    }
}

Do not forget to register a newly created module in web.config of your application:

Use system.WebServer section for IIS Integrated Model.

XML
<system.webServer>
    <modules>
      <add name="HttpPOSTLogger" 
      type="MySolution.HttpModules.HttpPOSTLogger, MySolution.HttpModules" />
    </modules>
</system.webServer>

Use system.web section for IIS Classic Model.

XML
<system.web>
    <httpModules>
        <add name="HttpPOSTLogger" 
        type="MySolution.HttpModules.HttpPOSTLogger, MySolution.HttpModules"/>
    </httpModules>
</system.web>

After you are set up, start your application and perform some POST activity. Now you will see that log files created by IIS (c:\inetput\Logs directory by default) now contain POST data as well, instead of "-" sign.

Before:

::1, -, 10/31/2017, 10:53:20, W3SVC1, machine-name, ::1, 5, 681, 662, 200, 0, POST, 
/MySolution/MyService.svc/MyMethod, -,

After:

::1, -, 10/31/2017, 10:53:20, W3SVC1, machine-name, ::1, 5, 681, 662, 200, 0, POST, 
/MySolution/MyService.svc/MyMethod, 
{"model":{"Platform":"Mobile","EntityID":"420003"}},

Applications

Please be careful, using this solution, because storing POST data for every request would be very expensive in terms of disk storage. In our case, we were interested in storing extremely small JSON data posted to web-service.

Notes

If IIS logging is not set up yet, you can add it manually from Add/Remove Windows Features or Server Manager. After doing this, IIS console should look something like this:

Image 1

Image 2

Samples of log output are provided in "IIS" format, but you can use "W3C" as well.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIIS Failed Requests Tracing Rules can also help Pin
RamonCar24-Oct-22 9:07
RamonCar24-Oct-22 9:07 
QuestionForm data Pin
Member 1071760919-Mar-21 6:20
Member 1071760919-Mar-21 6:20 
SuggestionAdapted for content-type xml and JSON Pin
Clement Albert18-Nov-20 6:36
Clement Albert18-Nov-20 6:36 
QuestionDoesn't work for SOAP webservices Pin
Chuck Bevitt31-Jan-18 10:56
Chuck Bevitt31-Jan-18 10:56 
Questionuseful Pin
zpaulo_carraca1-Nov-17 7:28
zpaulo_carraca1-Nov-17 7:28 
useful in itself and didatic
PraiseNice and Simple Pin
John B Oliver31-Oct-17 12:25
John B Oliver31-Oct-17 12:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.