Udostępnij za pośrednictwem


How to: Retrieve Information About an Event Provider

You can read properties of an event publisher to discover the events that the publisher can publish, the name of the publisher, the path to the resource files used by the publisher, and other properties. The ProviderMetadata class contains the event publisher properties that can be read by your application.

Example

Description

The following code example uses the System.Diagnostics.Eventing.Reader classes to read event publisher properties such as the path to the resource file used by the publisher and the events that the publisher can raise.

Code

Imports System
Imports System.Globalization
Imports System.Diagnostics.Eventing.Reader

Public Class ReadEventPublisherProperties

    Public Overloads Shared Function Main( _
    ByVal args() As String) As Integer

        Dim session As New EventLogSession()
        Dim providerMessageFilePath As String = String.Empty
        Dim providerResourceFileName As String = String.Empty
        Dim providerHelpLink As String = String.Empty
        Dim providerDisplayName As String = String.Empty

        ' You can get the names of all the event publishers on the local
        ' computer.
        'Dim name As String
        'For Each name In session.GetProviderNames()
        '    Console.WriteLine(name)
        'Next

        Try

            Dim providerName As String = "Microsoft-Windows-TaskScheduler"
            Dim metaData As ProviderMetadata = New ProviderMetadata(providerName, _
                session, _
                CultureInfo.CurrentCulture)

            If Not metaData Is Nothing Then

                providerDisplayName = metaData.DisplayName           ' Display Name of the provider
                If Not metaData.HelpLink Is Nothing Then
                    providerHelpLink = metaData.HelpLink.ToString    ' Link to external help on event
                End If
                providerMessageFilePath = metaData.MessageFilePath     ' Source of provider metadata
                providerResourceFileName = metaData.ResourceFilePath   ' Resource with provider metadata
            End If

            Console.WriteLine("This provider publishes the following events:")
            Dim eventData As EventMetadata
            For Each eventData In metaData.Events

                Console.WriteLine("--------------------------------------------------")
                Console.WriteLine("Event ID = {0}. Description: {1}", _
                    eventData.Id, eventData.Description)
            Next

        Catch ex As UnauthorizedAccessException

            ' Process has insufficient permissions
            Console.WriteLine(ex.ToString())

        Catch ex As EventLogException

            ' General Event Exception
            Console.WriteLine(ex.ToString())
        End Try

    End Function
End Class
using System;
using System.Globalization;
using System.Diagnostics.Eventing.Reader;

class ReadEventPublisherProperties
{
    static void Main(string[] args)
    {
        EventLogSession session = new EventLogSession();
        String providerMessageFilePath = String.Empty;
        String providerResourceFileName = String.Empty;
        String providerHelpLink = String.Empty;
        String providerDisplayName = String.Empty;

        // You can get the names of all the event publishers on the local
        // computer.         
        //foreach (string name in session.GetProviderNames())
        //{
        //    Console.WriteLine(name);
        //}

        try
        {
            string providerName = "Microsoft-Windows-TaskScheduler";
            ProviderMetadata metaData = new ProviderMetadata(providerName,   //Provider name to look up
                                        session,                             //The session
                                        CultureInfo.CurrentCulture);         //Culture of active thread

            if (null != metaData)
            {
                providerDisplayName = metaData.DisplayName;             //Display Name of the provider
                if (null != metaData.HelpLink)
                    providerHelpLink = metaData.HelpLink.ToString();    //Link to external help on event
                providerMessageFilePath = metaData.MessageFilePath;     //Source of provider metadata
                providerResourceFileName = metaData.ResourceFilePath;   //Resource with provider metadata
            }

            Console.WriteLine("This provider publishes the following events:");
            foreach (EventMetadata eventData in metaData.Events)
            {
                Console.WriteLine("--------------------------------------------------");
                Console.WriteLine("Event ID = {0}. Description: {1}",
                    eventData.Id, eventData.Description);
            }
        }
        catch (UnauthorizedAccessException ex)
        {
            //Process has insufficient permissions
            Console.WriteLine(ex.ToString());
        }
        catch (EventLogException ex)
        {
            //General Event Exception
            Console.WriteLine(ex.ToString());
        }
    }
}

Compiling the Code

This example requires references to the System.dll and System.Core.dll files.

See Also

Concepts

Event Log Scenarios

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation. All rights reserved.