編輯

共用方式為


EventLog.Log Property

Definition

Gets or sets the name of the log to read from or write to.

public:
 property System::String ^ Log { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.SettingsBindable(true)]
public string Log { get; set; }
[System.ComponentModel.TypeConverter("System.Diagnostics.Design.LogConverter, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string Log { get; set; }
[System.ComponentModel.TypeConverter("System.Diagnostics.Design.LogConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string Log { get; set; }
[System.ComponentModel.SettingsBindable(true)]
[System.ComponentModel.TypeConverter("System.Diagnostics.Design.LogConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string Log { get; set; }
[<System.ComponentModel.SettingsBindable(true)>]
member this.Log : string with get, set
[<System.ComponentModel.TypeConverter("System.Diagnostics.Design.LogConverter, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>]
member this.Log : string with get, set
[<System.ComponentModel.TypeConverter("System.Diagnostics.Design.LogConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>]
member this.Log : string with get, set
[<System.ComponentModel.SettingsBindable(true)>]
[<System.ComponentModel.TypeConverter("System.Diagnostics.Design.LogConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>]
member this.Log : string with get, set
Public Property Log As String

Property Value

The name of the log. This can be Application, System, Security, or a custom log name. The default is an empty string ("").

Attributes

Examples

The following example reads entries in the event log, "NewEventLog", on the local computer.

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
int main()
{
   EventLog^ myNewLog = gcnew EventLog;
   myNewLog->Log = "NewEventLog";
   System::Collections::IEnumerator^ myEnum = myNewLog->Entries->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      EventLogEntry^ entry = safe_cast<EventLogEntry^>(myEnum->Current);
      Console::WriteLine( "\tEntry: {0}", entry->Message );
   }
}
using System;
using System.Diagnostics;

class MySample{

    public static void Main(){

        EventLog myNewLog = new EventLog();
        myNewLog.Log = "NewEventLog";
        foreach(EventLogEntry entry in myNewLog.Entries){
            Console.WriteLine("\tEntry: " + entry.Message);
        }
    }
}
Imports System.Diagnostics

Class MySample
    Public Shared Sub Main()
        Dim myNewLog As New EventLog()
        myNewLog.Log = "NewEventLog"
        Dim entry As EventLogEntry
        For Each entry In  myNewLog.Entries
            Console.WriteLine((ControlChars.Tab & "Entry: " & entry.Message))
        Next entry
    End Sub
End Class

Remarks

Three log files exist by default on the server: Application, System, and Security. Applications and services use the Application log file. Device drivers use the System log file. The system generates success and failure audit events in the Security log when auditing is turned on. If you have other applications installed, like Active Directory on Windows servers, there might be other default log files. In addition, you can create custom log files on a local or remote computer. Custom logs help organize your entries in a more detailed way than is allowed when your components write events to the default Application log.

Note

Log names are limited to eight characters. According to the system, MyLogSample1 and MyLogSample2 are the same log.

If you write to an event log, it is not enough to specify the Log property. You must associate a Source property with your event log resource to connect it to a particular log. It is not necessary to specify a Source when only reading from a log, but an event source must be associated with the event log resource in the server's registry. You can specify only the Log name and MachineName (server computer name) to read from it.

Note

You are not required to specify the MachineName if you are connecting to a log. If you do not specify the MachineName, the local computer (".") is assumed.

If the Source property has not been specified, a call to Log returns an empty string if Log has not been explicitly set (by setting the Log property, or through the constructor). If the Source has been specified, Log returns the name of the log to which that source was registered.

A source can only be registered to one log at a time. If the Source property was set for an instance of EventLog, you cannot change the Log property for that EventLog without changing the value of Source or calling DeleteEventSource first. If you change the Log property after the Source property has been set, writing a log entry throws an exception.

The operating system stores event logs as files. When you use EventLogInstaller or CreateEventSource to create a new event log, the associated file is stored in the %SystemRoot%\System32\Config directory on the specified computer. The file name is set by appending the first 8 characters of the Log property with the ".evt" file name extension.

You cannot create a new log using the Log property alone (without specifying a source for the log). You can call CreateEventSource, passing in a new log name as a parameter, and then call DeleteEventSource. However, the intent is usually either to create (and write entries to) new application-specific logs, or to read from existing logs.

If the Log value changes, the event log is closed and all event handles are released.

Caution

If you set the Log property to the name of a log that does not exist, the system attaches the EventLog to the Application log, but does not warn you that it is using a log other than the one you specified.

Applies to

See also