EventSourceCreationData EventSourceCreationData EventSourceCreationData EventSourceCreationData Class

定義

表示用於在本機電腦或遠端電腦上建立事件記錄檔來源的組態設定。Represents the configuration settings used to create an event log source on the local computer or a remote computer.

public ref class EventSourceCreationData
public class EventSourceCreationData
type EventSourceCreationData = class
Public Class EventSourceCreationData
繼承
EventSourceCreationDataEventSourceCreationDataEventSourceCreationDataEventSourceCreationData

範例

下列程式碼範例會從命令列引數設定事件來源的配置屬性。The following code example sets the configuration properties for an event source from command-line arguments. 輸入引數會指定事件來源名稱、事件記錄檔名稱、電腦名稱稱和事件訊息資源檔。The input arguments specify the event source name, event log name, computer name, and event message resource file. 此程式碼範例會驗證來源不會與現有的事件來源衝突, 然後為指定的事件記錄檔建立新的事件來源。The code example verifies that the source does not conflict with an existing event source, and then creates the new event source for the specified event log.

#using <System.dll>

using namespace System;
using namespace System::Globalization;
using namespace System::Diagnostics;

[STAThread]
int main()
{
   array<String^>^args = Environment::GetCommandLineArgs();
   
   EventSourceCreationData ^ mySourceData = gcnew EventSourceCreationData( "","" );
   bool registerSource = true;
   
   // Process input parameters.
   if ( args->Length > 1 )
   {
      
      // Require at least the source name.
      mySourceData->Source = args[ 1 ];
      if ( args->Length > 2 )
      {
         mySourceData->LogName = args[ 2 ];
      }

      if ( args->Length > 3 )
      {
         mySourceData->MachineName = args[ 3 ];
      }

      if ( (args->Length > 4) && (args[ 4 ]->Length > 0) )
      {
         mySourceData->MessageResourceFile = args[ 4 ];
      }
   }
   else
   {
      
      // Display a syntax help message.
      Console::WriteLine( "Input:" );
      Console::WriteLine( " source [event log] [machine name] [resource file]" );
      registerSource = false;
   }

   
   // Set defaults for parameters missing input.
   if ( mySourceData->MachineName->Length == 0 )
   {
      
      // Default to the local computer.
      mySourceData->MachineName = ".";
   }

   if ( mySourceData->LogName->Length == 0 )
   {
      
      // Default to the Application log.
      mySourceData->LogName = "Application";
   }

   
   // Determine if the source exists on the specified computer.
   if (  !EventLog::SourceExists( mySourceData->Source, mySourceData->MachineName ) )
   {
      
      // The source does not exist.  
      // Verify that the message file exists
      // and the event log is local.
      if ( (mySourceData->MessageResourceFile != nullptr) && (mySourceData->MessageResourceFile->Length > 0) )
      {
         if ( mySourceData->MachineName->Equals( "." ) )
         {
            if (  !System::IO::File::Exists( mySourceData->MessageResourceFile ) )
            {
               Console::WriteLine( "File {0} not found - message file not set for source.", mySourceData->MessageResourceFile );
               registerSource = false;
            }
         }
         else
         {
            
            // For simplicity, do not allow setting the message
            // file for a remote event log.  To set the message
            // for a remote event log, and for source registration,
            // the file path should be specified with system-wide
            // environment variables that are valid on the remote
            // computer, such as
            // "%SystemRoot%\system32\myresource.dll".
            Console::WriteLine( "Message resource file ignored for remote event log." );
            registerSource = false;
         }
      }
   }
   else
   {
      
      // Do not register the source, it already exists.
      registerSource = false;
      
      // Get the event log corresponding to the existing source.
      String^ sourceLog;
      sourceLog = EventLog::LogNameFromSourceName( mySourceData->Source, mySourceData->MachineName );
      
      // Determine if the event source is registered for the 
      // specified log.
      if ( sourceLog->ToUpper( CultureInfo::InvariantCulture ) != mySourceData->LogName->ToUpper( CultureInfo::InvariantCulture ) )
      {
         
         // An existing source is registered 
         // to write to a different event log.
         Console::WriteLine( "Warning: source {0} is already registered to write to event log {1}", mySourceData->Source, sourceLog );
      }
      else
      {
         
         // The source is already registered 
         // to write to the specified event log.
         Console::WriteLine( "Source {0} already registered to write to event log {1}", mySourceData->Source, sourceLog );
      }
   }

   if ( registerSource )
   {
      
      // Register the new event source for the specified event log.
      Console::WriteLine( "Registering new source {0} for event log {1}.", mySourceData->Source, mySourceData->LogName );
      EventLog::CreateEventSource( mySourceData );
      Console::WriteLine( "Event source was registered successfully!" );
   }
}

using System;
using System.Globalization;
using System.Diagnostics;

namespace EventLogSamples
{
    class CreateSourceSample
    {
        [STAThread]
        static void Main(string[] args)
        {
            EventSourceCreationData mySourceData = new EventSourceCreationData("", "");
            bool registerSource = true;

            // Process input parameters.
            if (args.Length > 0)
            {
                // Require at least the source name.

                mySourceData.Source = args[0];

                if (args.Length > 1)
                {
                    mySourceData.LogName = args[1];
                }

                if (args.Length > 2)
                {
                    mySourceData.MachineName = args[2];
                }
                if ((args.Length > 3) && (args[3].Length > 0))
                {
                    mySourceData.MessageResourceFile = args[3];
                }
            }
            else 
            {
                // Display a syntax help message.
                Console.WriteLine("Input:");
                Console.WriteLine(" source [event log] [machine name] [resource file]");

                registerSource = false;
            }

            // Set defaults for parameters missing input.
            if (mySourceData.MachineName.Length == 0)
            {
                // Default to the local computer.
                mySourceData.MachineName = ".";
            }
            if (mySourceData.LogName.Length == 0)
            {
                // Default to the Application log.
                mySourceData.LogName = "Application";
            }

            // Determine if the source exists on the specified computer.
            if (!EventLog.SourceExists(mySourceData.Source, 
                                       mySourceData.MachineName))
            {
                // The source does not exist.  

                // Verify that the message file exists
                // and the event log is local.

                if ((mySourceData.MessageResourceFile != null) &&
                    (mySourceData.MessageResourceFile.Length > 0))
                {
                    if (mySourceData.MachineName == ".") 
                    {
                        if (!System.IO.File.Exists(mySourceData.MessageResourceFile))
                        {
                            Console.WriteLine("File {0} not found - message file not set for source.",
                                mySourceData.MessageResourceFile);
                            registerSource = false;
                        }
                    }
                    else 
                    {
                        // For simplicity, do not allow setting the message
                        // file for a remote event log.  To set the message
                        // for a remote event log, and for source registration,
                        // the file path should be specified with system-wide
                        // environment variables that are valid on the remote
                        // computer, such as
                        // "%SystemRoot%\system32\myresource.dll".

                        Console.WriteLine("Message resource file ignored for remote event log.");
                        registerSource = false;
                    }
                }
            }
            else
            {
                // Do not register the source, it already exists.
                registerSource = false;

                // Get the event log corresponding to the existing source.
                string sourceLog;
                sourceLog = EventLog.LogNameFromSourceName(mySourceData.Source,
                                mySourceData.MachineName);

                // Determine if the event source is registered for the 
                // specified log.

                if (sourceLog.ToUpper(CultureInfo.InvariantCulture) != mySourceData.LogName.ToUpper(CultureInfo.InvariantCulture)) 
                {
                    // An existing source is registered 
                    // to write to a different event log.
                    Console.WriteLine("Warning: source {0} is already registered to write to event log {1}",
                        mySourceData.Source, sourceLog);
                }
                else 
                {
                    // The source is already registered 
                    // to write to the specified event log.

                    Console.WriteLine("Source {0} already registered to write to event log {1}",
                        mySourceData.Source, sourceLog);
                }
            }
        

            if (registerSource)
            {
                // Register the new event source for the specified event log.

                Console.WriteLine("Registering new source {0} for event log {1}.",
                    mySourceData.Source, mySourceData.LogName);
                EventLog.CreateEventSource(mySourceData);
                Console.WriteLine("Event source was registered successfully!");
            }
        }
    }
}
Imports System
Imports System.Globalization
Imports System.Diagnostics

Namespace EventLogSamples
    Class CreateSourceSample

        Public Shared Sub Main(ByVal args() As String)
        
            Dim mySourceData As EventSourceCreationData = new EventSourceCreationData("", "")
            Dim registerSource As Boolean = True

            ' Process input parameters.
            If args.Length > 0
                ' Require at least the source name.

                mySourceData.Source = args(0)

                If args.Length > 1
   
                    mySourceData.LogName = args(1)
    
                End If
                If args.Length > 2
   
                    mySourceData.MachineName = args(2)
    
                End If
                If args.Length > 3 AndAlso args(3).Length > 0
   
                    mySourceData.MessageResourceFile = args(3)
    
                End If

            Else 
                ' Display a syntax help message.
                Console.WriteLine("Input:")
                Console.WriteLine(" source [event log] [machine name] [resource file]")

                registerSource = False
            End If

            ' Set defaults for parameters missing input.
            If mySourceData.MachineName.Length = 0
            
                ' Default to the local computer.
                mySourceData.MachineName = "."
            End If
            If mySourceData.LogName.Length = 0
                ' Default to the Application log.
                mySourceData.LogName = "Application"
            End If

            ' Determine if the source exists on the specified computer.
            If Not EventLog.SourceExists(mySourceData.Source, _
                                       mySourceData.MachineName)
                ' The source does not exist.  

                ' Verify that the message file exists
                ' and the event log is local.
                If mySourceData.MessageResourceFile <> Nothing AndAlso _
                   mySourceData.MessageResourceFile.Length > 0
                   
                    If mySourceData.MachineName = "."
                        If Not System.IO.File.Exists(mySourceData.MessageResourceFile)

                            Console.WriteLine("File {0} not found - message file not set for source.", _
                                mySourceData.MessageResourceFile)
                            registerSource = False
                        End If
                    Else
                        ' For simplicity, do not allow setting the message
                        ' file for a remote event log.  To set the message
                        ' for a remote event log, and for source registration,
                        ' the file path should be specified with system-wide
                        ' environment variables that are valid on the remote
                        ' computer, such as
                        ' "%SystemRoot%\system32\myresource.dll".

                        Console.WriteLine("Message resource file ignored for remote event log.")
                        registerSource = False

                    End If
                End If
            Else 
                ' Do not register the source, it already exists.
                registerSource = False

                ' Get the event log corresponding to the existing source.
                Dim sourceLog As string
                sourceLog = EventLog.LogNameFromSourceName(mySourceData.Source, _
                                mySourceData.MachineName)

                ' Determine if the event source is registered for the 
                ' specified log.

                If sourceLog.ToUpper(CultureInfo.InvariantCulture) <> mySourceData.LogName.ToUpper(CultureInfo.InvariantCulture)

                    ' An existing source is registered 
                    ' to write to a different event log.

                    Console.WriteLine("Warning: source {0} is already registered to write to event log {1}", _
                        mySourceData.Source, sourceLog)
                Else 
                    ' The source is already registered 
                    ' to write to the specified event log.

                    Console.WriteLine("Source {0} already registered to write to event log {1}", _
                        mySourceData.Source, sourceLog)

                End If
            End If

            If registerSource
                ' Register the new event source for the specified event log.

                Console.WriteLine("Registering new source {0} for event log {1}.", _
                    mySourceData.Source, mySourceData.LogName)
                EventLog.CreateEventSource(mySourceData)
                Console.WriteLine("Event source was registered successfully!")
            End If

        End Sub 'Main
    End Class 'CreateSourceSample
End Namespace 'EventLogSamples

備註

您可以使用類別來設定新的來源, 以便將當地語系化的專案寫入事件記錄檔。 EventSourceCreationDataUse the EventSourceCreationData class to configure a new source for writing localized entries to an event log. 您不需要使用這個類別來讀取事件記錄檔。It is not necessary to use this class to read from an event log.

這個類別會定義新事件來源及其關聯事件記錄檔的設定。This class defines the configuration settings for a new event source and its associated event log. 相關聯的事件記錄檔可以位於本機電腦或遠端電腦上。The associated event log can be on the local computer or a remote computer. 若要在本機電腦上建立新的或現有事件記錄檔的新來源, 請LogName設定Source的和屬性EventSourceCreationData , 並呼叫EventLog.CreateEventSource(EventSourceCreationData)方法。To create a new source for a new or existing event log on the local computer, set the LogName and Source properties of an EventSourceCreationData and call the EventLog.CreateEventSource(EventSourceCreationData) method. 這個方法會建立您在Source屬性中指定的事件來源, 並針對中LogName指定的事件記錄檔進行註冊。This method creates the event source you specify in the Source property and registers it for the event log specified in LogName. 這個行為類似于使用EventLogInstaller類別來註冊事件記錄檔的事件來源。This behavior is similar to using the EventLogInstaller class to register an event source for an event log.

WriteEvent使用和WriteEntry方法, 將事件寫入事件記錄檔。Use the WriteEvent and WriteEntry methods to write events to an event log. 您必須指定要寫入事件的事件來源;在寫入具有來源的第一個專案之前, 您必須先建立和設定事件來源。You must specify an event source to write events; you must create and configure the event source before writing the first entry with the source.

在應用程式安裝期間建立新的事件來源。Create the new event source during the installation of your application. 這可讓作業系統重新整理其已註冊事件來源的清單及其設定。This allows time for the operating system to refresh its list of registered event sources and their configurations. 如果作業系統尚未重新整理其事件來源清單, 而且您嘗試使用新的來源寫入事件, 寫入作業將會失敗。If the operating system has not refreshed its list of event sources, and you attempt to write an event with the new source, the write operation will fail. 您可以使用EventLogInstaller來設定新的來源, 或CreateEventSource使用方法。You can configure a new source using an EventLogInstaller, or using the CreateEventSource method. 您必須具有電腦的系統管理許可權, 才能建立新的事件來源。You must have administrative rights on the computer to create a new event source.

您可以為現有的事件記錄檔或新的事件記錄檔建立事件來源。You can create an event source for an existing event log or a new event log. 當您建立新事件記錄檔的新來源時, 系統會註冊該記錄檔的來源, 但在寫入第一個專案之前, 不會建立記錄檔。When you create a new source for a new event log, the system registers the source for that log, but the log is not created until the first entry is written to it.

每個來源一次只能寫入一個事件記錄檔;不過, 您的應用程式可以使用多個來源寫入多個事件記錄檔。Each source can only write to one event log at a time; however, your application can use multiple sources to write to multiple event logs. 例如, 您的應用程式可能需要針對不同的事件記錄檔或不同的資源檔設定多個來源。For example, your application might need multiple sources configured for different event logs or different resource files.

若要變更現有來源的設定詳細資料, 您必須刪除來源, 然後使用新的設定加以建立。To change the configuration details of an existing source, you must delete the source and then create it with the new configuration. 如果其他應用程式或元件使用現有的來源, 請使用更新的設定來建立新的來源, 而不是刪除現有的來源。If other applications or components use the existing source, create a new source with the updated configuration rather than deleting the existing source.

您可以使用您的事件類別目錄和訊息字串的當地語系化資源來註冊事件來源。You can register the event source with localized resources for your event category and message strings. 您的應用程式可以使用資源識別碼來寫入事件記錄檔專案, 而不是指定實際的字串。Your application can write event log entries using resource identifiers, rather than specifying the actual string. 事件檢視器會使用資源識別碼, 根據目前的語言設定, 從當地語系化的資源檔中尋找和顯示對應的字串。The Event Viewer uses the resource identifier to find and display the corresponding string from the localized resource file based on current language settings. 您可以為事件類別目錄、訊息和參數插入字串註冊個別的檔案, 也可以為這三種字串類型註冊相同的資源檔。You can register a separate file for event categories, messages, and parameter insertion strings, or you can register the same resource file for all three types of strings. CategoryCount使用、 CategoryResourceFileMessageResourceFile和屬性來設定來源,以將當地語系化的專案寫入事件記錄檔。ParameterResourceFileUse the CategoryCount, CategoryResourceFile, MessageResourceFile, and ParameterResourceFile properties to configure the source to write localized entries to the event log. 如果您的應用程式將字串值直接寫入事件記錄檔, 您就不需要設定這些屬性。If your application writes string values directly to the event log, you do not need to set these properties.

來源必須設定為寫入當地語系化的專案或撰寫直接字串。The source must be configured either for writing localized entries or for writing direct strings. 方法WriteEntry會將指定的字串直接寫入事件記錄檔, 而不會使用可當地語系化的訊息資源檔。The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. WriteEvent使用方法, 利用當地語系化的訊息資源檔來寫入事件。Use the WriteEvent method to write events using a localized message resource file.

如果您的應用程式使用資源識別碼和字串值來寫入專案, 您必須註冊兩個不同的來源。If your application writes entries using both resource identifiers and string values, you must register two separate sources. 例如, 設定一個具有資源檔的來源, 然後在WriteEvent方法中使用該來源, 將使用資源識別碼的專案寫入事件記錄檔。For example, configure one source with resource files, and then use that source in the WriteEvent method to write entries using resource identifiers to the event log. 然後建立不含資源檔的不同來源, 然後在WriteEntry方法中使用該來源, 將字串直接寫入使用該來源的事件記錄檔。Then create a different source without resource files and use that source in the WriteEntry method to write strings directly to the event log using that source.

建構函式

EventSourceCreationData(String, String) EventSourceCreationData(String, String) EventSourceCreationData(String, String) EventSourceCreationData(String, String)

會利用指定的事件來源和事件記錄檔名稱,初始化 EventSourceCreationData 類別的新執行個體。Initializes a new instance of the EventSourceCreationData class with a specified event source and event log name.

屬性

CategoryCount CategoryCount CategoryCount CategoryCount

取得或設定分類資源檔中的分類數目。Gets or sets the number of categories in the category resource file.

CategoryResourceFile CategoryResourceFile CategoryResourceFile CategoryResourceFile

取得或設定資源檔路徑,這個資源檔包含來源的分類字串。Gets or sets the path of the resource file that contains category strings for the source.

LogName LogName LogName LogName

取得或設定要讓來源寫入項目的事件記錄檔名稱。Gets or sets the name of the event log to which the source writes entries.

MachineName MachineName MachineName MachineName

取得或設定要註冊事件來源的電腦名稱。Gets or sets the name of the computer on which to register the event source.

MessageResourceFile MessageResourceFile MessageResourceFile MessageResourceFile

取得或設定訊息資源檔路徑,這個資源檔包含來源的訊息格式化字串。Gets or sets the path of the message resource file that contains message formatting strings for the source.

ParameterResourceFile ParameterResourceFile ParameterResourceFile ParameterResourceFile

取得或設定資源檔路徑,這個資源檔包含來源的訊息參數字串。Gets or sets the path of the resource file that contains message parameter strings for the source.

Source Source Source Source

取得或設定要使用事件記錄檔登錄為事件來源的名稱。Gets or sets the name to register with the event log as an event source.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

安全性

SecurityPermission
用於以完全信任的EventSourceCreationData方式呼叫的任何成員。for calling any member of EventSourceCreationData with full trust. 相關聯的列舉:UnrestrictedAssociated enumeration: Unrestricted

適用於

另請參閱