IHttpContext::GetCurrentExecutionStats Method

 

Retrieves the execution statistics for the current context.

Syntax

virtual HRESULT GetCurrentExecutionStats(  
   DWORD* pdwNotification,  
   DWORD* pdwNotificationStartTickCount = NULL,  
   PCWSTR* ppszModule = NULL,  
   DWORD* pdwModuleStartTickCount = NULL,  
   DWORD* pdwAsyncNotification = NULL,  
   DWORD* pdwAsyncNotificationStartTickCount = NULL  
) const = 0;  

Parameters

pdwNotification
A pointer to a DWORD that contains the current notification.

pdwNotificationStartTickCount
A pointer to a DWORD that contains the tick count for the start of the current notification.

ppszModule
A pointer to a string that contains the name of the current module.

pdwModuleStartTickCount
A pointer to a DWORD that contains the tick count for the start of the current module.

pdwAsyncNotification
A pointer to a DWORD that contains the current asynchronous notification.

pdwAsyncNotificationStartTickCount
A pointer to a DWORD that contains the tick count for the start of an asynchronous notification.

Return Value

An HRESULT. Possible values include, but are not limited to, those in the following table.

Value Description
NO_ERROR Indicates that the operation was successful.
E_INVALIDARG Indicates that a specified parameter is not valid.

Remarks

Developers can use the GetCurrentExecutionStats method to retrieve specific execution information for the current context. For example, the pdwNotification and pdwAsyncNotification parameters contain the values for the current synchronous or asynchronous notification, and the ppszModule parameter contains the name of the module for the current context.

Three of the return parameters, pdwModuleStartTickCount, pdwNotificationStartTickCount, and pdwAsyncNotificationStartTickCount, respectively, contain the tick counts for the start of the module and the start of the current synchronous and asynchronous notifications.

Note

The tick count is the number of milliseconds that have elapsed since the system was started. For more information about retrieving tick counts, see the GetTickCount method.

Example

The following code example demonstrates how to create an HTTP module that performs the following tasks:

  1. The module registers for the RQ_BEGIN_REQUEST, RQ_MAP_REQUEST_HANDLER, and RQ_SEND_RESPONSE notifications.

  2. The module creates a CHttpModule class that contains OnBeginRequest, OnMapRequestHandler, and OnSendResponse methods.

  3. When a Web client requests a URL, IIS calls the module's OnBeginRequest, OnMapRequestHandler, and OnSendResponse methods. Each of these methods calls a private method named RetrieveExecutionStats that performs the following tasks:

    1. Retrieves the execution statistics by using the GetCurrentExecutionStats method and tests for an error.

    2. Creates a string that contains the tick count for the start of the current notification.

    3. Pauses for one second.

    4. Creates a string that contains the elapsed tick count from the start of the current notification.

    5. Writes the execution statistics as an event to the application log of the Event Viewer.

  4. The module removes the CHttpModule class from memory and then exits.

#define _WINSOCKAPI_
#include <windows.h>
#include <sal.h>
#include <httpserv.h>
#include <wchar.h>

// Create the module class.
class MyHttpModule : public CHttpModule
{
private:

    // Create a handle for the event viewer.
    HANDLE m_hEventLog;

    // Define a method the retrieves the current execution statistics.
    void RetrieveExecutionStats(
        IHttpContext * pHttpContext, LPCSTR szNotification )
    {
        HRESULT hr = S_OK;
        DWORD  dwNotification = 0;
        DWORD  dwNotificationStart = 0;
        PCWSTR pszModule = NULL;

        // Retrieve the current execution statistics.
        hr = pHttpContext->GetCurrentExecutionStats(
            &dwNotification,&dwNotificationStart,
            &pszModule,NULL,NULL,NULL);

        // Test for an error.
        if (SUCCEEDED(hr))
        {
            // Create strings for the statistics.
            char szNotificationStart[256];
            char szTimeElapsed[256];
            
            // Retrieve and format the statistics.
            sprintf_s(szNotificationStart,255,
                "Tick count at start of notification: %u",
                dwNotificationStart);
            // Pause for one second.
            Sleep(1000);
            // Retrieve and format the statistics.
            sprintf_s(szTimeElapsed,255,
                "Ticks elapsed since start of notification: %u",
                GetTickCount() - dwNotificationStart);
            
            // Allocate space for the module name.
            char * pszBuffer = (char*) pHttpContext->AllocateRequestMemory(
                (DWORD) wcslen(pszModule)+1 );
            
            // Test for an error.
            if (pszBuffer!=NULL)
            {
                // Return the module information to the web client.
                wcstombs(pszBuffer,pszModule,wcslen(pszModule));
                // Create an array of strings.
                LPCSTR szBuffer[4] = {szNotification,pszBuffer,
                    szNotificationStart,szTimeElapsed};
                // Write the strings to the Event Viewer.
                WriteEventViewerLog(szBuffer,4);
            }
        }
    }

public:

    REQUEST_NOTIFICATION_STATUS
    OnBeginRequest(
        IN IHttpContext * pHttpContext,
        IN IHttpEventProvider * pProvider
    )
    {
        UNREFERENCED_PARAMETER( pProvider );
        // Retrieve and return the execution statistics.
        RetrieveExecutionStats(pHttpContext,"OnBeginRequest");
        // Return processing to the pipeline.
        return RQ_NOTIFICATION_CONTINUE;
    }

    REQUEST_NOTIFICATION_STATUS
    OnMapRequestHandler(
        IN IHttpContext * pHttpContext,
        IN IMapHandlerProvider * pProvider
    )
    {
        UNREFERENCED_PARAMETER( pProvider );
        // Retrieve and return the execution statistics.
        RetrieveExecutionStats(pHttpContext,"OnMapRequestHandler");
        // Return processing to the pipeline.
        return RQ_NOTIFICATION_CONTINUE;
    }

    REQUEST_NOTIFICATION_STATUS
    OnSendResponse(
        IN IHttpContext * pHttpContext,
        IN ISendResponseProvider * pProvider
    )
    {
        UNREFERENCED_PARAMETER( pProvider );
        // Retrieve and return the execution statistics.
        RetrieveExecutionStats(pHttpContext,"OnSendResponse");
        // Return processing to the pipeline.
        return RQ_NOTIFICATION_CONTINUE;
    }

    MyHttpModule()
    {
        // Open a handle to the Event Viewer.
        m_hEventLog = RegisterEventSource( NULL,"IISADMIN" );
    }

    ~MyHttpModule()
    {
        // Test whether the handle for the Event Viewer is open.
        if (NULL != m_hEventLog)
        {
            // Close the handle to the Event Viewer.
             DeregisterEventSource( m_hEventLog );
            m_hEventLog = NULL;
        }
    }

private:

    // Define a method that writes to the Event Viewer.
    BOOL WriteEventViewerLog(LPCSTR * lpStrings, WORD wNumStrings)
    {
        // Test whether the handle for the Event Viewer is open.
        if (NULL != m_hEventLog)
        {
            // Write any strings to the Event Viewer and return.
            return ReportEvent(
                m_hEventLog, EVENTLOG_INFORMATION_TYPE,
                0, 0, NULL, wNumStrings, 0, lpStrings, NULL );
        }
        return FALSE;
    }
};

// Create the module's class factory.
class MyHttpModuleFactory : public IHttpModuleFactory
{
public:
    HRESULT
    GetHttpModule(
        OUT CHttpModule ** ppModule, 
        IN IModuleAllocator * pAllocator
    )
    {
        UNREFERENCED_PARAMETER( pAllocator );

        // Create a new instance.
        MyHttpModule * pModule = new MyHttpModule;

        // Test for an error.
        if (!pModule)
        {
            // Return an error if we cannot create the instance.
            return HRESULT_FROM_WIN32( ERROR_NOT_ENOUGH_MEMORY );
        }
        else
        {
            // Return a pointer to the module.
            *ppModule = pModule;
            pModule = NULL;
            // Return a success status.
            return S_OK;
        }            
    }

    void Terminate()
    {
        // Remove the class from memory.
        delete this;
    }
};

// Create the module's exported registration function.
HRESULT
__stdcall
RegisterModule(
    DWORD dwServerVersion,
    IHttpModuleRegistrationInfo * pModuleInfo,
    IHttpServer * pGlobalInfo
)
{
    UNREFERENCED_PARAMETER( dwServerVersion );
    UNREFERENCED_PARAMETER( pGlobalInfo );

    return pModuleInfo->SetRequestNotifications(
        new MyHttpModuleFactory,
        RQ_BEGIN_REQUEST | RQ_MAP_REQUEST_HANDLER | RQ_SEND_RESPONSE,
        0
    );
}

Your module must export the RegisterModule function. You can export this function by creating a module definition (.def) file for your project, or you can compile the module by using the /EXPORT:RegisterModule switch. For more information, see Walkthrough: Creating a Request-Level HTTP Module By Using Native Code.

You can optionally compile the code by using the __stdcall (/Gz) calling convention instead of explicitly declaring the calling convention for each function.

Requirements

Type Description
Client - IIS 7.0 on Windows Vista
- IIS 7.5 on Windows 7
- IIS 8.0 on Windows 8
- IIS 10.0 on Windows 10
Server - IIS 7.0 on Windows Server 2008
- IIS 7.5 on Windows Server 2008 R2
- IIS 8.0 on Windows Server 2012
- IIS 8.5 on Windows Server 2012 R2
- IIS 10.0 on Windows Server 2016 Technical Preview
Product - IIS 7.0, IIS 7.5, IIS 8.0, IIS 8.5, IIS 10.0
- IIS Express 7.5, IIS Express 8.0, IIS Express 10.0
Header Httpserv.h

See Also

IHttpContext Interface