Click here to Skip to main content
13,794,944 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


10 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


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.


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

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

Use system.WebServer section for IIS Integrated Model.

      <add name="HttpPOSTLogger" 

      type="MySolution.HttpModules.HttpPOSTLogger, MySolution.HttpModules" />

Use system.web section for IIS Classic Model.

        <add name="HttpPOSTLogger" 

        type="MySolution.HttpModules.HttpPOSTLogger, MySolution.HttpModules"/>

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.


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


::1, -, 10/31/2017, 10:53:20, W3SVC1, machine-name, ::1, 5, 681, 662, 200, 0, POST, 


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.


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.


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


About the Author

Dimitri Rakviashvili
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionDoesn't work for SOAP webservices Pin
Chuck Bevitt31-Jan-18 11:56
memberChuck Bevitt31-Jan-18 11:56 
Questionuseful Pin
zpaulo_carraca1-Nov-17 8:28
memberzpaulo_carraca1-Nov-17 8:28 
PraiseNice and Simple Pin
John B Oliver31-Oct-17 13:25
memberJohn B Oliver31-Oct-17 13: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
Web05 | 2.8.181207.3 | Last Updated 31 Oct 2017
Article Copyright 2017 by Dimitri Rakviashvili
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid