IHttpContext::GetCurrentExecutionStats 方法

检索当前上下文的执行统计信息。

语法

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

parameters

pdwNotification
指向包含当前通知的 的指针 DWORD

pdwNotificationStartTickCount
指向 的 DWORD 指针,其中包含当前通知开始的时钟周期计数。

ppszModule
指向包含当前模块名称的字符串的指针。

pdwModuleStartTickCount
指向 的 DWORD 指针,该指针包含当前模块开头的刻度计数。

pdwAsyncNotification
指向包含当前异步通知的 的指针 DWORD

pdwAsyncNotificationStartTickCount
指向 的 DWORD 指针,其中包含异步通知的开始计时周期计数。

返回值

HRESULT。 可能的值包括(但并不限于)下表中的项。

说明
NO_ERROR 指示操作成功。
E_INVALIDARG 指示指定的参数无效。

备注

开发人员可以使用 GetCurrentExecutionStats 方法检索当前上下文的特定执行信息。 例如, pdwNotificationpdwAsyncNotification 参数包含当前同步或异步通知的值,参数 ppszModule 包含当前上下文的模块名称。

三个返回参数 pdwModuleStartTickCountpdwNotificationStartTickCountpdwAsyncNotificationStartTickCount分别包含模块开始和当前同步和异步通知的开始计时周期计数。

注意

时钟周期计数是自系统启动以来经过的毫秒数。 有关检索时钟周期计数的详细信息,请参阅 GetTickCount 方法。

示例

下面的代码示例演示如何创建执行以下任务的 HTTP 模块:

  1. 模块注册 RQ_BEGIN_REQUESTRQ_MAP_REQUEST_HANDLERRQ_SEND_RESPONSE 通知。

  2. 该模块创建包含 OnBeginRequestOnMapRequestHandlerOnSendResponse 方法的CHttpModule 类。

  3. 当 Web 客户端请求 URL 时,IIS 会调用模块的 OnBeginRequestOnMapRequestHandlerOnSendResponse 方法。 其中每种方法都会调用一个名为 RetrieveExecutionStats 的私有方法,该方法执行以下任务:

    1. 使用 方法检索执行统计信息, GetCurrentExecutionStats 并测试错误。

    2. 创建一个字符串,其中包含当前通知的开头的刻度计数。

    3. 暂停一秒钟。

    4. 创建一个字符串,其中包含从当前通知开始的已用刻度计数。

    5. 将执行统计信息作为事件写入事件查看器的应用程序日志。

  4. 该模块从内存中删除 CHttpModule 类,然后退出。

#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
    );
}

模块必须导出 RegisterModule 函数。 可以通过为项目创建模块定义 (.def) 文件导出此函数,也可以使用 开关编译模块 /EXPORT:RegisterModule 。 有关详细信息,请参阅 演练:使用本机代码创建Request-Level HTTP 模块

可以选择使用调用约定编译代码, __stdcall (/Gz) 而不是为每个函数显式声明调用约定。

要求

类型 说明
客户端 - Windows Vista 上的 IIS 7.0
- Windows 7 上的 IIS 7.5
- Windows 8 上的 IIS 8.0
- Windows 10 上的 IIS 10.0
服务器 - Windows Server 2008 上的 IIS 7.0
- Windows Server 2008 R2 上的 IIS 7.5
- Windows Server 2012 上的 IIS 8.0
- Windows Server 2012 R2 上的 IIS 8.5
- Windows Server 2016 上的 IIS 10.0
产品 - 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

另请参阅

IHttpContext 接口