EventSourceCreationData(String, String) 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化一個以指定事件來源和事件日誌名稱的新類別實例 EventSourceCreationData 。
public:
EventSourceCreationData(System::String ^ source, System::String ^ logName);
public EventSourceCreationData(string? source, string logName);
public EventSourceCreationData(string source, string logName);
new System.Diagnostics.EventSourceCreationData : string * string -> System.Diagnostics.EventSourceCreationData
Public Sub New (source As String, logName As String)
參數
- source
- String
名稱是用來註冊事件日誌作為條目來源。
- logName
- String
記錄來源條目所記錄的日誌名稱。
範例
以下程式碼範例判斷事件來源 SampleApplicationSource 是否已註冊於本地電腦。 若事件來源不存在,範例會設定該來源的訊息資源檔並建立新的事件來源。 最後,程式碼範例設定事件日誌的在地顯示名稱,使用資源識別碼值與DisplayNameMsgId資源檔案路徑。messageFile
static void CreateEventSourceSample1(string messageFile)
{
string myLogName;
string sourceName = "SampleApplicationSource";
// Create the event source if it does not exist.
if(!EventLog.SourceExists(sourceName))
{
// Create a new event source for the custom event log
// named "myNewLog."
myLogName = "myNewLog";
EventSourceCreationData mySourceData = new EventSourceCreationData(sourceName, myLogName);
// Set the message resource file that the event source references.
// All event resource identifiers correspond to text in this file.
if (!System.IO.File.Exists(messageFile))
{
Console.WriteLine("Input message resource file does not exist - {0}",
messageFile);
messageFile = "";
}
else
{
// Set the specified file as the resource
// file for message text, category text, and
// message parameter strings.
mySourceData.MessageResourceFile = messageFile;
mySourceData.CategoryResourceFile = messageFile;
mySourceData.CategoryCount = CategoryCount;
mySourceData.ParameterResourceFile = messageFile;
Console.WriteLine("Event source message resource file set to {0}",
messageFile);
}
Console.WriteLine("Registering new source for event log.");
EventLog.CreateEventSource(mySourceData);
}
else
{
// Get the event log corresponding to the existing source.
myLogName = EventLog.LogNameFromSourceName(sourceName,".");
}
// Register the localized name of the event log.
// For example, the actual name of the event log is "myNewLog," but
// the event log name displayed in the Event Viewer might be
// "Sample Application Log" or some other application-specific
// text.
EventLog myEventLog = new EventLog(myLogName, ".", sourceName);
if (messageFile.Length > 0)
{
myEventLog.RegisterDisplayName(messageFile, DisplayNameMsgId);
}
}
Public Shared Sub CreateEventSourceSample1(ByVal messageFile As String)
Dim myLogName As String
Dim sourceName As String = "SampleApplicationSource"
' Create the event source if it does not exist.
If Not EventLog.SourceExists(sourceName)
' Create a new event source for the custom event log
' named "myNewLog."
myLogName = "myNewLog"
Dim mySourceData As EventSourceCreationData = New EventSourceCreationData(sourceName, myLogName)
' Set the message resource file that the event source references.
' All event resource identifiers correspond to text in this file.
If Not System.IO.File.Exists(messageFile)
Console.WriteLine("Input message resource file does not exist - {0}", _
messageFile)
messageFile = ""
Else
' Set the specified file as the resource
' file for message text, category text and
' message parameters strings.
mySourceData.MessageResourceFile = messageFile
mySourceData.CategoryResourceFile = messageFile
mySourceData.CategoryCount = CategoryCount
mySourceData.ParameterResourceFile = messageFile
Console.WriteLine("Event source message resource file set to {0}", _
messageFile)
End If
Console.WriteLine("Registering new source for event log.")
EventLog.CreateEventSource(mySourceData)
Else
' Get the event log corresponding to the existing source.
myLogName = EventLog.LogNameFromSourceName(sourceName,".")
End If
' Register the localized name of the event log.
' For example, the actual name of the event log is "myNewLog," but
' the event log name displayed in the Event Viewer might be
' "Sample Application Log" or some other application-specific
' text.
Dim myEventLog As EventLog = New EventLog(myLogName, ".", sourceName)
If messageFile.Length > 0
myEventLog.RegisterDisplayName(messageFile, DisplayNameMsgId)
End If
End Sub
程式碼範例使用以下訊息文字檔,內建於資源函式庫 EventLogMsgs.dll。 訊息文字檔是建立訊息資源檔案的來源。 訊息文字檔定義了類別、事件訊息及參數插入字串的資源識別碼與文字。
; // EventLogMsgs.mc
; // ********************************************************
; // Use the following commands to build this file:
; // mc -s EventLogMsgs.mc
; // rc EventLogMsgs.rc
; // link /DLL /SUBSYSTEM:WINDOWS /NOENTRY /MACHINE:x86 EventLogMsgs.Res
; // ********************************************************
; // - Event categories -
; // Categories must be numbered consecutively starting at 1.
; // ********************************************************
MessageId=0x1
Severity=Success
SymbolicName=INSTALL_CATEGORY
Language=English
Installation
.
MessageId=0x2
Severity=Success
SymbolicName=QUERY_CATEGORY
Language=English
Database Query
.
MessageId=0x3
Severity=Success
SymbolicName=REFRESH_CATEGORY
Language=English
Data Refresh
.
; // - Event messages -
; // *********************************
MessageId = 1000
Severity = Success
Facility = Application
SymbolicName = AUDIT_SUCCESS_MESSAGE_ID_1000
Language=English
My application message text, in English, for message id 1000, called from %1.
.
MessageId = 1001
Severity = Warning
Facility = Application
SymbolicName = AUDIT_FAILED_MESSAGE_ID_1001
Language=English
My application message text, in English, for message id 1001, called from %1.
.
MessageId = 1002
Severity = Success
Facility = Application
SymbolicName = GENERIC_INFO_MESSAGE_ID_1002
Language=English
My generic information message in English, for message id 1002.
.
MessageId = 1003
Severity = Warning
Facility = Application
SymbolicName = GENERIC_WARNING_MESSAGE_ID_1003
Language=English
My generic warning message in English, for message id 1003, called from %1.
.
MessageId = 1004
Severity = Success
Facility = Application
SymbolicName = UPDATE_CYCLE_COMPLETE_MESSAGE_ID_1004
Language=English
The update cycle is complete for %%5002.
.
MessageId = 1005
Severity = Warning
Facility = Application
SymbolicName = SERVER_CONNECTION_DOWN_MESSAGE_ID_1005
Language=English
The refresh operation did not complete because the connection to server %1 could not be established.
.
; // - Event log display name -
; // ********************************************************
MessageId = 5001
Severity = Success
Facility = Application
SymbolicName = EVENT_LOG_DISPLAY_NAME_MSGID
Language=English
Sample Event Log
.
; // - Event message parameters -
; // Language independent insertion strings
; // ********************************************************
MessageId = 5002
Severity = Success
Facility = Application
SymbolicName = EVENT_LOG_SERVICE_NAME_MSGID
Language=English
SVC_UPDATE.EXE
.
備註
要寫入事件日誌,必須先建立事件日誌的事件來源。 要註冊一個新的來源,先初始化實 EventSourceCreationData 例,設定應用程式的實例屬性,然後呼叫該 EventLog.CreateEventSource(EventSourceCreationData) 方法。 原始碼註冊後,你可以用 WriteEvent or WriteEntry 方法從原始碼寫入條目。
你可以用在地化資源註冊事件來源,針對你的事件類別和訊息字串。 你的應用程式可以使用資源識別碼來撰寫事件日誌條目,而不必指定實際字串。 事件檢視器會根據目前語言設定,利用資源識別碼從本地化資源檔案中尋找並顯示對應字串。 你可以為事件類別、訊息和參數插入字串註冊一個獨立檔案,或者同時註冊同一個資源檔案來管理這三種字串。 利用 CategoryCount、 CategoryResourceFile、 MessageResourceFile和 ParameterResourceFile 屬性設定原始碼寫入事件日誌的在地化條目。 如果你的應用程式直接將字串值寫入事件日誌,就不需要設定這些屬性。
原始碼必須設定用於寫入本地化條目或直接字串。 如果你的應用程式同時使用資源識別碼和字串值來寫入條目,你必須註冊兩個獨立的來源。 例如,設定一個來源的資源檔案,然後在該 WriteEvent 方法中使用該來源,將資源識別碼寫入事件日誌。 接著建立一個沒有資源檔案的來源,並用該來源 WriteEntry 直接寫入事件日誌。
下表顯示了 的 EventSourceCreationData初始性質值。
| 房產 | 初始值 |
|---|---|
| Source | 參數 source 。 |
| LogName | 參數 logName 。 |
| MachineName | 本地電腦(「.」)。 |
| CategoryCount | 零 |
| CategoryResourceFile |
null(Visual Basic 中的 Nothing)。 |
| MessageResourceFile |
null(Visual Basic 中的 Nothing)。 |
| ParameterResourceFile |
null(Visual Basic 中的 Nothing)。 |