EventLog.LogNameFromSourceName(String, String) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得登錄指定來源的記錄檔的名稱。
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 的值會擲回例外狀況。 不過,單一事件記錄檔可以寫入許多不同的來源。