Share via


Configurazione e avvio della sessione del logger del kernel NT

La sessione Nt Kernel Logger è una sessione di traccia eventi che registra un set predefinito di eventi del kernel. Non si chiama la funzione EnableTrace per abilitare i provider del kernel. Usare invece il membro EnableFlags di EVENT_TRACE_PROPERTIES struttura per specificare gli eventi del kernel che si desidera ricevere. La funzione StartTrace usa i flag di abilitazione specificati per abilitare i provider del kernel.

È presente una sola sessione del logger kernel NT. Se la sessione è già in uso, la funzione StartTrace restituisce ERROR_ALREADY_EXISTS.

Per informazioni dettagliate sull'avvio di una sessione di traccia eventi, vedere Configurazione e avvio di una sessione di traccia eventi.

Per informazioni dettagliate sull'avvio di una sessione di logger privato, vedere Configurazione e avvio di una sessione logger privata.

Per informazioni dettagliate sull'avvio di una sessione di logger globale, vedere Configurazione e avvio della sessione globale del logger.

Per informazioni dettagliate sull'avvio di una sessione autoLogger, vedere Configurazione e avvio di una sessione autoLogger.

L'esempio seguente illustra come configurare e avviare una sessione del logger del kernel NT che raccoglie gli eventi del kernel TCP/IP di rete e li scrive in un file circolare di 5 MB.

#define INITGUID  // Include this #define to use SystemTraceControlGuid in Evntrace.h.

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <strsafe.h>
#include <wmistr.h>
#include <evntrace.h>

#define LOGFILE_PATH L"<FULLPATHTOTHELOGFILE.etl>"

void wmain(void)
{
    ULONG status = ERROR_SUCCESS;
    TRACEHANDLE SessionHandle = 0;
    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,
    // which get appended to the end of the session properties structure.
    
    BufferSize = sizeof(EVENT_TRACE_PROPERTIES) + sizeof(LOGFILE_PATH) + sizeof(KERNEL_LOGGER_NAME);
    pSessionProperties = (EVENT_TRACE_PROPERTIES*) malloc(BufferSize);    
    if (NULL == pSessionProperties)
    {
        wprintf(L"Unable to allocate %d bytes for properties structure.\n", BufferSize);
        goto cleanup;
    }
    
    // Set the session properties. You only append the log file name
    // to the properties structure; the StartTrace function appends
    // the session name for you.

    ZeroMemory(pSessionProperties, BufferSize);
    pSessionProperties->Wnode.BufferSize = BufferSize;
    pSessionProperties->Wnode.Flags = WNODE_FLAG_TRACED_GUID;
    pSessionProperties->Wnode.ClientContext = 1; //QPC clock resolution
    pSessionProperties->Wnode.Guid = SystemTraceControlGuid; 
    pSessionProperties->EnableFlags = EVENT_TRACE_FLAG_NETWORK_TCPIP;
    pSessionProperties->LogFileMode = EVENT_TRACE_FILE_MODE_CIRCULAR;
    pSessionProperties->MaximumFileSize = 5;  // 5 MB
    pSessionProperties->LoggerNameOffset = sizeof(EVENT_TRACE_PROPERTIES);
    pSessionProperties->LogFileNameOffset = sizeof(EVENT_TRACE_PROPERTIES) + sizeof(KERNEL_LOGGER_NAME); 
    StringCbCopy((LPWSTR)((char*)pSessionProperties + pSessionProperties->LogFileNameOffset), sizeof(LOGFILE_PATH), LOGFILE_PATH);

    // Create the trace session.

    status = StartTrace((PTRACEHANDLE)&SessionHandle, KERNEL_LOGGER_NAME, pSessionProperties);

    if (ERROR_SUCCESS != status)
    {
        if (ERROR_ALREADY_EXISTS == status)
        {
            wprintf(L"The NT Kernel Logger session is already in use.\n");
        }
        else
        {
            wprintf(L"EnableTrace() failed with %lu\n", status);
        }

        goto cleanup;
    }

    wprintf(L"Press any key to end trace session ");
    _getch();

cleanup:

    if (SessionHandle)
    {
        status = ControlTrace(SessionHandle, KERNEL_LOGGER_NAME, pSessionProperties, EVENT_TRACE_CONTROL_STOP);

        if (ERROR_SUCCESS != status)
        {
            wprintf(L"ControlTrace(stop) failed with %lu\n", status);
        }
    }

    if (pSessionProperties)
        free(pSessionProperties);
}

Configurazione e avvio di una sessione di logger privato

Configurazione e avvio di una sessione SystemTraceProvider

Configurazione e avvio di una sessione autoLogger

Configurazione e avvio di una sessione di traccia eventi

Aggiornamento di una sessione di traccia eventi