EventSourceCreationData Classe
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Rappresenta le impostazioni di configurazione utilizzate per creare un'origine del registro eventi nel computer locale o in un computer remoto.
public ref class EventSourceCreationData
public class EventSourceCreationData
type EventSourceCreationData = class
Public Class EventSourceCreationData
- Ereditarietà
-
EventSourceCreationData
Esempio
Nell'esempio di codice seguente vengono impostate le proprietà di configurazione per un'origine evento dagli argomenti della riga di comando. Gli argomenti di input specificano il nome dell'origine evento, il nome del registro eventi, il nome del computer e il file di risorse del messaggio di evento. L'esempio di codice verifica che l'origine non sia in conflitto con un'origine evento esistente e quindi crea la nuova origine evento per il registro eventi specificato.
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
Commenti
Usare la EventSourceCreationData classe per configurare una nuova origine per la scrittura di voci localizzate in un registro eventi. Non è necessario usare questa classe per leggere da un registro eventi.
Questa classe definisce le impostazioni di configurazione per una nuova origine evento e il registro eventi associato. Il registro eventi associato può trovarsi nel computer locale o in un computer remoto. Per creare una nuova origine per un registro eventi nuovo o esistente nel computer locale, impostare le LogName proprietà e Source di un oggetto EventSourceCreationData e chiamare il EventLog.CreateEventSource(EventSourceCreationData) metodo . Questo metodo crea l'origine evento specificata nella Source proprietà e la registra per il registro eventi specificato in LogName. Questo comportamento è simile all'uso della EventLogInstaller classe per registrare un'origine evento per un registro eventi.
Utilizzare i WriteEvent metodi e WriteEntry per scrivere eventi in un registro eventi. È necessario specificare un'origine evento per scrivere eventi; è necessario creare e configurare l'origine evento prima di scrivere la prima voce con l'origine.
Creare la nuova origine evento durante l'installazione dell'applicazione. Ciò consente al sistema operativo di aggiornare l'elenco delle origini eventi registrate e delle relative configurazioni. Se il sistema operativo non ha aggiornato l'elenco delle origini eventi e si tenta di scrivere un evento con la nuova origine, l'operazione di scrittura avrà esito negativo. È possibile configurare una nuova origine usando un EventLogInstalleroggetto o usando il CreateEventSource metodo . Per creare una nuova origine evento, è necessario disporre dei diritti amministrativi nel computer.
È possibile creare un'origine evento per un registro eventi esistente o un nuovo registro eventi. Quando si crea una nuova origine per un nuovo registro eventi, il sistema registra l'origine per tale log, ma il log non viene creato finché non viene scritta la prima voce.
Ogni origine può scrivere solo in un registro eventi alla volta; Tuttavia, l'applicazione può usare più origini per scrivere in più registri eventi. Ad esempio, l'applicazione potrebbe richiedere più origini configurate per registri eventi diversi o file di risorse diversi.
Per modificare i dettagli di configurazione di un'origine esistente, è necessario eliminare l'origine e quindi crearla con la nuova configurazione. Se altre applicazioni o componenti usano l'origine esistente, creare una nuova origine con la configurazione aggiornata anziché eliminare l'origine esistente.
È possibile registrare l'origine evento con risorse localizzate per la categoria di eventi e le stringhe di messaggio. L'applicazione può scrivere voci del registro eventi usando identificatori di risorsa, invece di specificare la stringa effettiva. Il Visualizzatore eventi usa l'identificatore della risorsa per trovare e visualizzare la stringa corrispondente dal file di risorse localizzato in base alle impostazioni della lingua correnti. È possibile registrare un file separato per categorie di eventi, messaggi e stringhe di inserimento di parametri oppure è possibile registrare lo stesso file di risorse per tutti e tre i tipi di stringhe. Usare le CategoryCountproprietà , MessageResourceFileCategoryResourceFile, e ParameterResourceFile per configurare l'origine per scrivere voci localizzate nel registro eventi. Se l'applicazione scrive i valori stringa direttamente nel registro eventi, non è necessario impostare queste proprietà.
L'origine deve essere configurata per la scrittura di voci localizzate o per la scrittura di stringhe dirette. Il WriteEntry metodo scrive la stringa specificata direttamente nel registro eventi e non usa un file di risorse del messaggio localizzabile. Utilizzare il WriteEvent metodo per scrivere eventi usando un file di risorse del messaggio localizzato.
Se l'applicazione scrive voci usando sia identificatori di risorsa che valori stringa, è necessario registrare due origini separate. Ad esempio, configurare un'origine con i file di risorse e quindi usare tale origine nel WriteEvent metodo per scrivere voci usando gli identificatori di risorsa nel registro eventi. Creare quindi un'origine diversa senza file di risorse e usare tale origine nel WriteEntry metodo per scrivere stringhe direttamente nel registro eventi usando tale origine.
Costruttori
| Nome | Descrizione |
|---|---|
| EventSourceCreationData(String, String) |
Inizializza una nuova istanza della EventSourceCreationData classe con un'origine evento e un nome del log eventi specificati. |
Proprietà
| Nome | Descrizione |
|---|---|
| CategoryCount |
Ottiene o imposta il numero di categorie nel file di risorse di categoria. |
| CategoryResourceFile |
Ottiene o imposta il percorso del file di risorse che contiene stringhe di categoria per l'origine. |
| LogName |
Ottiene o imposta il nome del registro eventi in cui l'origine scrive le voci. |
| MachineName |
Ottiene o imposta il nome del computer in cui registrare l'origine evento. |
| MessageResourceFile |
Ottiene o imposta il percorso del file di risorse del messaggio contenente le stringhe di formattazione dei messaggi per l'origine. |
| ParameterResourceFile |
Ottiene o imposta il percorso del file di risorse che contiene stringhe di parametri del messaggio per l'origine. |
| Source |
Ottiene o imposta il nome da registrare con il registro eventi come origine evento. |
Metodi
| Nome | Descrizione |
|---|---|
| Equals(Object) |
Determina se l'oggetto specificato è uguale all'oggetto corrente. (Ereditato da Object) |
| GetHashCode() |
Funge da funzione hash predefinita. (Ereditato da Object) |
| GetType() |
Ottiene il Type dell'istanza corrente. (Ereditato da Object) |
| MemberwiseClone() |
Crea una copia superficiale del Objectcorrente. (Ereditato da Object) |
| ToString() |
Restituisce una stringa che rappresenta l'oggetto corrente. (Ereditato da Object) |