locked
Log inbound request traffic? RRS feed

  • Question

  • User-1683414442 posted

    We're using asp.net 2.0 on our server and per our agreement with our data-center we only pay for inbound traffic.

     What we want to do is monitor inbound requests on all our web-applications on the server, to log the size of each request so we can see where we're wasting bandwidth. I'm not sure what the easiest way is to do this. Can IIS maybe give me that information already? Do I need to change every application to do this? Or can I write a handler to do this transparently without affecting my applications?

     Any suggestions appreciated.

    Monday, March 26, 2007 10:11 PM

Answers

  • User-1087479560 posted

    Hi,

     Instead of using httpHandler, you should use a httpModule.

    public class LogModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }
    
        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            HttpContext context = app.Context;
            HttpWorkerRequest workerRequest = 
                ((IServiceProvider)context).GetService(typeof(HttpWorkerRequest)) as HttpWorkerRequest;
    
            int length = int.Parse(workerRequest.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength));
             // log it some where
         }
    }
                

     

    Hope it hleps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 27, 2007 4:33 AM

All replies

  • User-1087479560 posted

    Hi,

     Instead of using httpHandler, you should use a httpModule.

    public class LogModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }
    
        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            HttpContext context = app.Context;
            HttpWorkerRequest workerRequest = 
                ((IServiceProvider)context).GetService(typeof(HttpWorkerRequest)) as HttpWorkerRequest;
    
            int length = int.Parse(workerRequest.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength));
             // log it some where
         }
    }
                

     

    Hope it hleps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 27, 2007 4:33 AM
  • User-1683414442 posted

    Thanks

    Is there a way to have this work for all my applications without installing it in each one seperately? Otherwise I'd rather just build the code into my global.asax, but I want to avoid changing every app on my server to do this. It will take me days to do them one-by-one!

    Wednesday, March 28, 2007 2:42 AM
  • User-1087479560 posted

    Hi,

    You can compile this class into an assembly, install it into GAC.

    Register it in [Windows Folder]\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.config, so that it will take effect on all applications on this server.

    Wednesday, March 28, 2007 2:56 AM