更新事件跟踪会话
若要更新事件跟踪会话的属性,请使用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;
}
}
相关主题