EventLog.LogNameFromSourceName(String, String) EventLog.LogNameFromSourceName(String, String) EventLog.LogNameFromSourceName(String, String) EventLog.LogNameFromSourceName(String, String) Method

定義

取得登錄指定來源的記錄檔的名稱。Gets the name of the log to which the specified source is registered.

public:
 static System::String ^ LogNameFromSourceName(System::String ^ source, System::String ^ machineName);
[System.MonoNotSupported("remote machine is not supported")]
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 String String String

事件來源的名稱。The name of the event source.

machineName
String String String String

要在其上搜尋記錄檔的電腦的名稱,或者表示本機電腦的 "."。The name of the computer on which to look, or "." for the local computer.

傳回

與登錄中指定的來源關聯的記錄檔名稱。The name of the log associated with the specified source in the registry.

範例

下列範例會從本機電腦刪除的來源。The following example deletes a source from the local computer. 此範例會判斷其來源的記錄檔,,然後刪除記錄檔。The example determines the log from its source, and then deletes the log.

注意

多個來源可能會寫入事件記錄檔。More than one source might write to an event log. 之前刪除自訂記錄檔,請確定沒有其他寫入該記錄檔的來源。Before deleting a custom log, make sure there are no other sources writing to that log.

#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 MySample
{

    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
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 'Main
End Class 'MySample

備註

事件來源會指出項目會記錄事件。The event source indicates what logs the event. 它通常是應用程式的名稱或名稱的子元件的應用程式,如果應用程式很大。It is often the name of the application, or the name of a subcomponent of the application, if the application is large. 應用程式和服務應該寫入應用程式記錄檔或自訂的記錄檔。Applications and services should write to the Application log or a custom log. 裝置驅動程式應該寫入至系統記錄檔。Device drivers should write to the System log.

當您建立新的來源,可以只撰寫一個記錄檔一次,則系統會登錄事件記錄檔與您的應用程式做為有效的來源項目。When you create a new source, which can only write to one log at a time, the system registers your application with the event log as a valid source of entries. Source屬性可以是任何字串,但名稱不能由其他電腦上的來源。The Source property can be any string, but the name cannot be used by other sources on the computer. 嘗試建立重複Source值就會擲回例外狀況。An attempt to create a duplicated Source value throws an exception. 不過,單一的事件記錄檔可以有許多不同的來源寫入它。However, a single event log can have many different sources writing to it.

安全性

EventLogPermission
用於管理的電腦上的事件記錄檔資訊。for administering event log information on the computer. 相關聯的列舉型別: AdministerAssociated enumeration: Administer

適用於

另請參閱