How to: Configure and Read Event Log Properties

.NET Framework 3.5

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.



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.


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}.",

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

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

        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

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation. All rights reserved.

Community Additions