Compartilhar via


How to: Configure and Read Event Log Properties

You can configure and read properties of an event log to manage how the log stores events. The EventLogConfiguration class contains the event log properties that can be read and set by your application.

Example

Description

The following code example uses the EventLogInformation and EventLogConfiguration classes to set how events are stored in a log after the log has reached its maximum size. The following code example also shows you how to read event log properties such as the path to the log file and the size of the log file.

Code

Imports System
Imports System.Diagnostics.Eventing.Reader

Public Class ConfigureAndReadEventLogProperties

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

        Dim session As New EventLogSession
        Dim logName As String = String.Empty

        ' Get the names of all the logs on the local computer.
        ' This step is not necessary, but it allows you to find
        ' the name of a log to pass into the EventLogConfiguration 
        ' constructor.        
        'Dim name As String
        'For Each name In session.GetLogNames()
        '    Console.WriteLine(name)
        'Next

        ' Pass in the name of the log that you want to access 
        ' or configure. This examples uses the 
        ' "Microsoft-Windows-TaskScheduler/Operational" log.
        logName = "Microsoft-Windows-TaskScheduler/Operational"
        Dim log As New EventLogConfiguration(logName)

        ' Read the log properties.
        Console.WriteLine("Configuring the {0} log.", log.LogName)
        Console.WriteLine("The log path is: {0}.", log.LogFilePath)
        Console.WriteLine("Maximum log size (in bytes): {0}.", _
            log.MaximumSizeInBytes)

        ' Get event log information.
        Dim logInformation As EventLogInformation = _
            session.GetLogInformation(log.LogName, PathType.LogName)
        Console.WriteLine("The last time the log was written to: {0}", _
            logInformation.LastWriteTime.ToString())
        Console.WriteLine("Log file size (in bytes): {0}.", _
            logInformation.FileSize)

        ' Configure the log properties.
        log.IsEnabled = True
        log.LogMode = EventLogMode.Circular

        Try

            log.SaveChanges()

        Catch e As UnauthorizedAccessException

            Console.WriteLine("You do not have permission to configure this log!" & _
                " Try running this application with administrator privileges. " + e.Message)
        End Try

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

class ConfigureAndReadEventLogProperties
{
    static void Main(string[] args)
    {
        EventLogSession session = new EventLogSession();
        string logName = "";

        // Get the names of all the logs on the local computer.
        // This step is not necessary, but it allows you to find
        // the name of a log to pass into the EventLogConfiguration 
        // constructor.        
        //foreach (string name in session.GetLogNames())
        //{
        //    Console.WriteLine(name);
        //}

        // Pass in the name of the log that you want to access 
        // or configure. This examples uses the 
        // "Microsoft-Windows-TaskScheduler/Operational" log.
        logName = "Microsoft-Windows-TaskScheduler/Operational";
        EventLogConfiguration log =
            new EventLogConfiguration(logName);

        // Read the log properties.
        Console.WriteLine("Configuring the {0} log.", log.LogName);
        Console.WriteLine("The log path is: {0}.", log.LogFilePath);
        Console.WriteLine("Maximum log size (in bytes): {0}.",
            log.MaximumSizeInBytes);

        // Get event log information.
        EventLogInformation logInformation =
            session.GetLogInformation(log.LogName, PathType.LogName);
        Console.WriteLine("The last time the log was written to: {0}",
            logInformation.LastWriteTime.ToString());
        Console.WriteLine("Log file size (in bytes): {0}.",
            logInformation.FileSize);

        // Configure the log properties.
        log.IsEnabled = true;
        log.LogMode = EventLogMode.Circular;

        try
        {
            log.SaveChanges();
        }
        catch (UnauthorizedAccessException e)
        {
            Console.WriteLine("You do not have permission to configure this log!" +
                " Try running this application with administrator privileges. " + e.Message);
        }
    }
}

Compiling the Code

The following code example requires references to the System.dll and System.Core.dll files.

See Also

Reference

EventLogConfiguration

Concepts

Event Log Scenarios

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation. All rights reserved.