EventLog.LogNameFromSourceName(String, String) 方法

定义

获取指定的源注册到的日志的名称。

public:
 static System::String ^ LogNameFromSourceName(System::String ^ source, System::String ^ machineName);
public static string LogNameFromSourceName (string source, string machineName);
static member LogNameFromSourceName : string * string -> string
Public Shared Function LogNameFromSourceName (source As String, machineName As String) As String

参数

source
String

事件源的名称。

machineName
String

要从中删除日志的计算机名称,对于本地计算机则为“.”。

返回

与注册表中的指定源关联的日志名称。

示例

以下示例从本地计算机中删除源。 该示例从其源中确定日志,然后删除该日志。

注意

多个源可能会写入事件日志。 在删除自定义日志之前,请确保没有其他源写入该日志。

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;
int main()
{
   String^ logName;
   if ( EventLog::SourceExists( "MySource" ) )
   {
      
      // Find the log associated with this source.    
      logName = EventLog::LogNameFromSourceName( "MySource", "." );
      // Make sure the source is in the log we believe it to be in
      if (logName != "MyLog")
          return -1;
      // Delete the source and the log.
      EventLog::DeleteEventSource( "MySource" );
      EventLog::Delete( logName );
      Console::WriteLine( "{0} deleted.", logName );
   }
   else
        {
            // Create the event source to make next try successful.
            EventLog::CreateEventSource("MySource", "MyLog");
        }
}
using System;
using System.Diagnostics;
using System.Threading;

class MySample1
{
    public static void Main()
    {
        string logName;

        if (EventLog.SourceExists("MySource"))
        {
            // Find the log associated with this source.
            logName = EventLog.LogNameFromSourceName("MySource", ".");
            // Make sure the source is in the log we believe it to be in.
            if (logName != "MyLog")
                return;
            // Delete the source and the log.
            EventLog.DeleteEventSource("MySource");
            EventLog.Delete(logName);

            Console.WriteLine(logName + " deleted.");
        }
        else
        {
            // Create the event source to make next try successful.
            EventLog.CreateEventSource("MySource", "MyLog");
        }
    }
}
Option Explicit On
Option Strict On

Imports System.Diagnostics
Imports System.Threading

Class MySample
    Public Shared Sub Main()
        Dim logName As String

        If EventLog.SourceExists("MySource") Then
            ' Find the log associated with this source.    
            logName = EventLog.LogNameFromSourceName("MySource", ".")
            ' Make sure the source is in the log we believe it to be in
            If (logName <> "MyLog") Then
                Return
            End If
            ' Delete the source and the log.
            EventLog.DeleteEventSource("MySource")
            EventLog.Delete(logName)

            Console.WriteLine((logName & " deleted."))
        Else
            ' Create the event source to make next try successful.
            EventLog.CreateEventSource("MySource", "MyLog")
        End If
    End Sub
End Class

注解

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

创建一次只能写入一个日志的新源时,系统会将应用程序注册到事件日志中,作为有效的条目源。 属性 Source 可以是任何字符串,但名称不能由计算机上的其他源使用。 尝试创建重复 Source 值会引发异常。 但是,单个事件日志可以有许多不同的源写入其中。

适用于

另请参阅