Click here to Skip to main content
13,703,177 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

8K views
9 bookmarked
Posted 31 Oct 2017
Licenced CPOL

HttpModule for Logging HTTP POST Data in IIS Log

, 31 Oct 2017
Rate this:
Please Sign up or sign in to vote.
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:

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.

<system.webServer>
    <modules>
      <add name="HttpPOSTLogger" 

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

Use system.web section for IIS Classic Model.

<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:

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)

Share

About the Author

Dimitri Rakviashvili
United States United States
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionDoesn't work for SOAP webservices Pin
Chuck Bevitt31-Jan-18 10:56
memberChuck Bevitt31-Jan-18 10:56 
Questionuseful Pin
zpaulo_carraca1-Nov-17 7:28
memberzpaulo_carraca1-Nov-17 7:28 
PraiseNice and Simple Pin
John B Oliver31-Oct-17 12:25
memberJohn 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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04-2016 | 2.8.180920.1 | Last Updated 31 Oct 2017
Article Copyright 2017 by Dimitri Rakviashvili
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid