EventLog.Source 属性

定义

获取或设置在写入事件日志时要注册和使用的源名称。

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

属性值

在事件日志中注册为项源的名称。 默认值为空字符串("")。

属性

例外

此源名称导致注册表项路径的长度超过 254 个字符。

示例

以下示例创建源 MySource (如果尚不存在),并将条目写入事件日志 MyNewLog

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;
int main()
{
   
   // Create the source, if it does not already exist.
   if (  !EventLog::SourceExists( "MySource" ) )
   {
      EventLog::CreateEventSource( "MySource", "MyNewLog" );
      Console::WriteLine( "CreatingEventSource" );
   }

   
   // Create an EventLog instance and assign its source.
   EventLog^ myLog = gcnew EventLog;
   myLog->Source = "MySource";
   
   // Write an informational entry to the event log.    
   myLog->WriteEntry( "Writing to event log." );
   Console::WriteLine( "Message written to event log." );
}
using System;
using System.Diagnostics;
using System.Threading;

class MySample{

    public static void Main(){

        // Create the source, if it does not already exist.
        if(!EventLog.SourceExists("MySource"))
        {
            // An event log source should not be created and immediately used.
            // There is a latency time to enable the source, it should be created
            // prior to executing the application that uses the source.
            // Execute this sample a second time to use the new source.
            EventLog.CreateEventSource("MySource", "MyNewLog");
            Console.WriteLine("CreatingEventSource");
            Console.WriteLine("Exiting, execute the application a second time to use the source.");
            // The source is created.  Exit the application to allow it to be registered.
            return;
        }

        // Create an EventLog instance and assign its source.
        EventLog myLog = new EventLog();
        myLog.Source = "MySource";

        // Write an informational entry to the event log.
        myLog.WriteEntry("Writing to event log.");

        Console.WriteLine("Message written to event log.");
    }
}
Option Explicit
Option Strict
Imports System.Diagnostics
Imports System.Threading

Class MySample
    Public Shared Sub Main()
        ' Create the source, if it does not already exist.
        If Not EventLog.SourceExists("MySource") Then
            EventLog.CreateEventSource("MySource", "MyNewLog")
            Console.WriteLine("CreatingEventSource")
        End If
        
        ' Create an EventLog instance and assign its source.
        Dim myLog As New EventLog()
        myLog.Source = "MySource"
        
        ' Write an informational entry to the event log.    
        myLog.WriteEntry("Writing to event log.")
        
        Console.WriteLine("Message written to event log.")
    End Sub
End Class

注解

事件源指示记录事件的内容。 它通常是应用程序的名称,或者应用程序子组件的名称(如果应用程序很大)。 应用程序和服务应写入应用程序日志或自定义日志。 设备驱动程序应写入系统日志。

仅当写入事件日志时,才需要指定事件源。 在将条目写入事件日志之前,必须将事件源注册到事件日志中,作为有效的事件源。 编写日志条目时,系统会使用 Source 属性来查找要在其中放置条目的相应日志。 如果要读取事件日志,可以指定 Source、 或 LogMachineName

注意

如果要连接到本地计算机上的日志, MachineName 则不需要指定 。 如果未指定 , MachineName则本地计算机 (“。”假定 ) 。

使用 WriteEventWriteEntry 将事件写入事件日志。 必须指定事件源才能写入事件;在使用源写入第一个条目之前,必须创建并配置事件源。

在安装应用程序期间Create新的事件源。 这允许操作系统有时间刷新其已注册事件源及其配置的列表。 如果操作系统尚未刷新其事件源列表,并且您尝试使用新源编写事件,则写入操作将失败。 可以使用 或使用 CreateEventSource 方法配置新源EventLogInstaller。 必须在计算机上具有管理权限才能创建新的事件源。

可以为现有事件日志或新的事件日志创建事件源。 为新事件日志创建新源时,系统会为该日志注册源,但在写入第一个条目之前不会创建该日志。

源在本地计算机上必须是唯一的;新的源名称不能与现有源名称或现有事件日志名称匹配。 每个源一次只能写入一个事件日志;但是,应用程序可以使用多个源写入多个事件日志。 例如,应用程序可能需要为不同的事件日志或不同的资源文件配置多个源。

如果更改 Source 值, EventLog 则会关闭其注册到的 ,并释放所有事件句柄。

必须为编写本地化条目或写入直接字符串配置源。 如果应用程序使用资源标识符和字符串值写入条目,则必须注册两个单独的源。 例如,使用资源文件配置一个源,然后在 方法中 WriteEvent 使用该源将使用资源标识符的条目写入事件日志。 然后创建不带资源文件的其他源,并在 方法中 WriteEntry 使用该源将字符串直接写入使用该源的事件日志。

若要更改现有源的配置详细信息,必须删除该源,然后使用新配置创建它。 如果其他应用程序或组件使用现有源,请使用更新的配置创建新的源,而不是删除现有源。

注意

如果源已映射到日志,并且你将其重新映射到新日志,则必须重新启动计算机才能使更改生效。

适用于

另请参阅