EventSourceCreationData Klasa

Definicja

Reprezentuje ustawienia konfiguracji używane do tworzenia źródła dziennika zdarzeń na komputerze lokalnym lub komputerze zdalnym.

public ref class EventSourceCreationData
public class EventSourceCreationData
type EventSourceCreationData = class
Public Class EventSourceCreationData
Dziedziczenie
EventSourceCreationData

Przykłady

Poniższy przykład kodu ustawia właściwości konfiguracji dla źródła zdarzeń z argumentów wiersza polecenia. Argumenty wejściowe określają nazwę źródła zdarzeń, nazwę dziennika zdarzeń, nazwę komputera i plik zasobu komunikatu zdarzenia. Przykładowy kod sprawdza, czy źródło nie powoduje konfliktu z istniejącym źródłem zdarzeń, a następnie tworzy nowe źródło zdarzeń dla określonego dziennika zdarzeń.

#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.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
    End Class
End Namespace 'EventLogSamples

Uwagi

EventSourceCreationData Użyj klasy , aby skonfigurować nowe źródło do zapisywania zlokalizowanych wpisów w dzienniku zdarzeń. Nie trzeba używać tej klasy do odczytywania z dziennika zdarzeń.

Ta klasa definiuje ustawienia konfiguracji dla nowego źródła zdarzeń i skojarzonego z nim dziennika zdarzeń. Skojarzony dziennik zdarzeń może znajdować się na komputerze lokalnym lub komputerze zdalnym. Aby utworzyć nowe źródło dla nowego lub istniejącego dziennika zdarzeń na komputerze lokalnym, ustaw LogName właściwości EventSourceCreationData i Source metody i EventLog.CreateEventSource(EventSourceCreationData) . Ta metoda tworzy źródło zdarzeń określone we właściwości i rejestruje je w dzienniku Source zdarzeń określonym w pliku LogName. To zachowanie jest podobne do używania EventLogInstaller klasy do rejestrowania źródła zdarzeń dla dziennika zdarzeń.

WriteEvent Użyj metod i WriteEntry do zapisywania zdarzeń w dzienniku zdarzeń. Należy określić źródło zdarzeń do zapisywania zdarzeń; przed zapisaniem pierwszego wpisu ze źródłem należy utworzyć i skonfigurować źródło zdarzeń.

Twórca nowe źródło zdarzeń podczas instalacji aplikacji. Dzięki temu system operacyjny może odświeżyć listę zarejestrowanych źródeł zdarzeń i ich konfiguracji. Jeśli system operacyjny nie odświeżył listy źródeł zdarzeń i spróbujesz zapisać zdarzenie z nowym źródłem, operacja zapisu zakończy się niepowodzeniem. Nowe źródło można skonfigurować przy użyciu metody EventLogInstallerlub CreateEventSource metody . Aby utworzyć nowe źródło zdarzeń, musisz mieć uprawnienia administracyjne na komputerze.

Możesz utworzyć źródło zdarzeń dla istniejącego dziennika zdarzeń lub nowego dziennika zdarzeń. Podczas tworzenia nowego źródła dla nowego dziennika zdarzeń system rejestruje źródło dla tego dziennika, ale dziennik nie jest tworzony, dopóki pierwszy wpis nie zostanie do niego zapisany.

Każde źródło może zapisywać tylko w jednym dzienniku zdarzeń jednocześnie; Jednak aplikacja może używać wielu źródeł do zapisywania w wielu dziennikach zdarzeń. Na przykład aplikacja może potrzebować wielu źródeł skonfigurowanych dla różnych dzienników zdarzeń lub różnych plików zasobów.

Aby zmienić szczegóły konfiguracji istniejącego źródła, należy usunąć źródło, a następnie utworzyć je przy użyciu nowej konfiguracji. Jeśli inne aplikacje lub składniki używają istniejącego źródła, utwórz nowe źródło ze zaktualizowaną konfiguracją zamiast usuwać istniejące źródło.

Źródło zdarzeń można zarejestrować za pomocą zlokalizowanych zasobów dla kategorii zdarzeń i ciągów komunikatów. Aplikacja może zapisywać wpisy dziennika zdarzeń przy użyciu identyfikatorów zasobów, zamiast określać rzeczywisty ciąg. Podgląd zdarzeń używa identyfikatora zasobu do znajdowania i wyświetlania odpowiedniego ciągu z zlokalizowanego pliku zasobów na podstawie bieżących ustawień języka. Możesz zarejestrować oddzielny plik dla kategorii zdarzeń, komunikatów i ciągów wstawiania parametrów lub zarejestrować ten sam plik zasobów dla wszystkich trzech typów ciągów. CategoryCountUżyj właściwości , , MessageResourceFileCategoryResourceFilei ParameterResourceFile , aby skonfigurować źródło do zapisywania zlokalizowanych wpisów w dzienniku zdarzeń. Jeśli aplikacja zapisuje wartości ciągów bezpośrednio w dzienniku zdarzeń, nie trzeba ustawiać tych właściwości.

Źródło musi być skonfigurowane do zapisywania zlokalizowanych wpisów lub zapisywania ciągów bezpośrednich. Metoda WriteEntry zapisuje dany ciąg bezpośrednio w dzienniku zdarzeń; nie używa lokalizowalnego pliku zasobu komunikatu. Użyj metody do zapisywania WriteEvent zdarzeń przy użyciu zlokalizowanego pliku zasobu komunikatu.

Jeśli aplikacja zapisuje wpisy przy użyciu identyfikatorów zasobów i wartości ciągów, musisz zarejestrować dwa oddzielne źródła. Na przykład skonfiguruj jedno źródło z plikami zasobów, a następnie użyj tego źródła w WriteEvent metodzie , aby zapisać wpisy przy użyciu identyfikatorów zasobów w dzienniku zdarzeń. Następnie utwórz inne źródło bez plików zasobów i użyj tego źródła w WriteEntry metodzie , aby zapisywać ciągi bezpośrednio w dzienniku zdarzeń przy użyciu tego źródła.

Konstruktory

EventSourceCreationData(String, String)

Inicjuje EventSourceCreationData nowe wystąpienie klasy z określoną nazwą źródła zdarzeń i dziennika zdarzeń.

Właściwości

CategoryCount

Pobiera lub ustawia liczbę kategorii w pliku zasobu kategorii.

CategoryResourceFile

Pobiera lub ustawia ścieżkę pliku zasobu zawierającego ciągi kategorii dla źródła.

LogName

Pobiera lub ustawia nazwę dziennika zdarzeń, do którego źródło zapisuje wpisy.

MachineName

Pobiera lub ustawia nazwę komputera, na którym ma zostać zarejestrowane źródło zdarzeń.

MessageResourceFile

Pobiera lub ustawia ścieżkę pliku zasobu komunikatu zawierającego ciągi formatowania komunikatów dla źródła.

ParameterResourceFile

Pobiera lub ustawia ścieżkę pliku zasobu zawierającego ciągi parametrów komunikatu dla źródła.

Source

Pobiera lub ustawia nazwę do zarejestrowania w dzienniku zdarzeń jako źródło zdarzeń.

Metody

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Dotyczy

Zobacz też