Click here to Skip to main content
15,118,865 members
Articles / Web Development / IIS
Posted 31 Oct 2017

Tagged as


12 bookmarked

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

Image 1

Image 2

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

Comments and Discussions

QuestionForm data Pin
Member 1071760919-Mar-21 7:20
MemberMember 1071760919-Mar-21 7:20 
SuggestionAdapted for content-type xml and JSON Pin
Clement Albert18-Nov-20 7:36
MemberClement Albert18-Nov-20 7:36 
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 
useful in itself and didatic
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.