更新事件追蹤會話
若要更新事件追蹤會話的屬性,請使用EVENT_TRACE_CONTROL_UPDATE控制項程式碼呼叫ControlTrace函式。 您可以使用從先前呼叫 StartTrace或會話名稱取得的會話控制碼來指定要更新的會話。 事件追蹤會話的屬性會使用 EVENT_TRACE_PROPERTIES 結構中指定的值來更新。 您只能更新屬性的子集。 如需您可以更新的屬性清單,請參閱ControlTrace函式的Properties參數。
如果 ControlTrace 呼叫成功, 則會更新EVENT_TRACE_PROPERTIES 結構以反映新的屬性值。 結構也會包含事件追蹤會話的新執行統計資料。
下列範例示範如何更新核心事件追蹤會話。 範例會查詢目前屬性值,並在更新會話之前更新 結構。
#include <windows.h>
#include <stdio.h>
#include <wmistr.h>
#include <evntrace.h>
#define MAX_SESSION_NAME_LEN 1024
#define MAX_LOGFILE_PATH_LEN 1024
void wmain(void)
{
ULONG status = ERROR_SUCCESS;
EVENT_TRACE_PROPERTIES* pSessionProperties = NULL;
ULONG BufferSize = 0;
// Allocate memory for the session properties. The memory must
// be large enough to include the log file name and session name.
// This example specifies the maximum size for the session name
// and log file name.
BufferSize = sizeof(EVENT_TRACE_PROPERTIES) +
(MAX_SESSION_NAME_LEN * sizeof(WCHAR)) +
(MAX_LOGFILE_PATH_LEN * sizeof(WCHAR));
pSessionProperties = (EVENT_TRACE_PROPERTIES*) malloc(BufferSize);
if (NULL == pSessionProperties)
{
wprintf(L"Unable to allocate %d bytes for properties structure.\n", BufferSize);
goto cleanup;
}
ZeroMemory(pSessionProperties, BufferSize);
pSessionProperties->Wnode.BufferSize = BufferSize;
// Query for the kernel trace session's current property values.
status = ControlTrace((TRACEHANDLE)NULL, KERNEL_LOGGER_NAME, pSessionProperties, EVENT_TRACE_CONTROL_QUERY);
if (ERROR_SUCCESS != status)
{
if (ERROR_WMI_INSTANCE_NOT_FOUND == status)
{
wprintf(L"The Kernel Logger is not running.\n");
}
else
{
wprintf(L"ControlTrace(query) failed with %lu\n", status);
}
goto cleanup;
}
// Update the enable flags to also enable the Process and Thread providers.
pSessionProperties->LogFileNameOffset = 0; // Zero tells ETW not to update the file name
pSessionProperties->Wnode.Flags = WNODE_FLAG_TRACED_GUID;
pSessionProperties->EnableFlags |= EVENT_TRACE_FLAG_PROCESS | EVENT_TRACE_FLAG_THREAD;
// Update the session's properties.
status = ControlTrace((TRACEHANDLE)NULL, KERNEL_LOGGER_NAME, pSessionProperties, EVENT_TRACE_CONTROL_UPDATE);
if (ERROR_SUCCESS != status)
{
wprintf(L"ControlTrace(update) failed with %lu.\n", status);
goto cleanup;
}
wprintf(L"\nUpdated session");
cleanup:
if (pSessionProperties)
{
free(pSessionProperties);
pSessionProperties = NULL;
}
}
相關主題