共用方式為


IHttpCoNtext::GetCurrentExecutionStats 方法

擷取目前內容的執行統計資料。

語法

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

參數

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. 此模組會建立 CHttpModule 類別,其中包含 OnBeginRequestOnMapRequestHandlerOnSendResponse 方法。

  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) 而不是明確宣告每個函式的呼叫慣例。

規格需求

類型 描述
Client - 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
標頭 Httpserv.h

另請參閱

IHttpCoNtext 介面