라이브러리형으로 솔루션을 하나 새로 만든 후. IHttpModule을 상속받는 클래스를 하나 생성한다.
using System ;
using System . Web ;
namespace Test . Solution . 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 ;
// HttpMethod가 POST인 것만 필터링한다.
if ( request != null && response != null && request . HttpMethod . ToUpper () == "POST" )
{
// Form body 조회
string body = HttpUtility . UrlDecode ( request . Form . ToString ());
if ( ! string . IsNullOrWhiteSpace ( body ))
{
// IIS Log 에 기록한다.
response . AppendToLog ( body );
}
else {
// Service Soap(Simple Object Access Protocol) Body(XML)
byte [] byteData = new byte [ request . ContentLength ];
// InputStream에서 바이트를 읽는다.
request . InputStream . Read ( byteData , 0 , byteData . Length );
// 읽은 기록을 다시 초기화한다.
request . InputStream . Position = 0 ;
// 바이트를 문자열로 변환한다.
body = ASCIIEncoding . ASCII . GetString ( byteData );
// IIS Log 에 기록한다.
response . AppendToLog ( body );
}
}
}
}
}
}
빌드 후 bin폴더에 DLL을 추가한다. 그리고 IIS에 모듈을 등록한다.
IIS 관리자 프로그램 중 모듈 위치
관리되는 모듈 추가
이름은 알아볼수있는 이름으로 지정하면되고, 종류에는 네임스페이스.클래스 명으로 지정한다. (ASP.NET 애플리케이션 또는 관리되는 처리기에 대한 요청인 경우에만 호출) 에 대한 체크는 리소스에 해당되는 호출은 건너띄고, aspx나 asmx 같은 페이지에서만 동작하게 하겠다는 의미입니다. 자세한 내용은 아래의 공식 홈페이지를 확인.
managedHandler 에 대한 공식 홈페이지 설명
https://blogs.iis.net/thomad/precondition-what
Another IIS Blog - Achtung! IIS7 Preconditions
Achtung! IIS7 Preconditions If you ever looked into the IIS configuration file applicationhost.config you might have encountered a setting called “precondition”. Here is the handler entry for aspnet_isapi.dll for example: The Need for Precondition
blogs.iis.net
아니면 Web.config 에 직접 추가할수도 있다. <system.webServer> <modules> <add name="HttpPOSTLogger" type="Test.Solution.HttpModules.HttpPOSTLogger" preCondition="managedHandler" /> </modules> </system.webServer>