📄 eventlog.cs
字号:
{
//Check to see if the log was closed
if(this.eventLogWriter.Log.Length==0)
throw new Exception("Log has already been closed");
this.eventLogWriter.Close();
this.OnLogClosed();
}
/// <summary>
/// Writes an information type entry, with the given message text, to the event log.
/// </summary>
/// <param name="message">The string to write to the event log.</param>
public void WriteEntry(string message)
{
this.CheckForValidLog();
this.eventLogWriter.WriteEntry(message);
}
/// <summary>
/// Writes an error, warning, information, success audit, or failure audit entry with the given message text to the event log.
/// </summary>
/// <param name="message">The string to write to the event log.</param>
/// <param name="type">One of the <see cref="EventLogEntryType">EventLogEntryType</see> values.</param>
public void WriteEntry(string message, EventLogEntryType type)
{
this.CheckForValidLog();
this.eventLogWriter.WriteEntry(message,type);
}
/// <summary>
/// Writes an information type entry with the given message text to the event log, using the specified event source.
/// </summary>
/// <param name="source">The source by which the application is registered. </param>
/// <param name="message">The string to write to the event log.</param>
public void WriteEntry(string source, string message)
{
this.CheckForValidLog();
if(source.Length>0)
this.eventLogWriter.WriteEntry(source, message);
else
this.eventLogWriter.WriteEntry(this.eventLogWriter.Source, message);
}
/// <summary>
/// Writes an entry with the given message text and application-defined event identifier to the event log.
/// </summary>
/// <param name="message">The string to write to the event log. </param>
/// <param name="type">One of the <see cref="EventLogEntryType">EventLogEntryType</see> values. </param>
/// <param name="eventID">The application-specific identifier for the event. </param>
public void WriteEntry(string message, EventLogEntryType type, int eventID)
{
this.CheckForValidLog();
this.eventLogWriter.WriteEntry(message,type, eventID);
}
/// <summary>
/// Writes an error, warning, information, success audit, or failure audit entry with the given message text to the event log, using the specified event source.
/// </summary>
/// <param name="source">The source by which the application is registered.</param>
/// <param name="message">The string to write to the event log.</param>
/// <param name="type">One of the <see cref="EventLogEntryType">EventLogEntryType</see> values.</param>
public void WriteEntry(string source, string message, EventLogEntryType type)
{
this.CheckForValidLog();
if(source.Length>0)
this.eventLogWriter.WriteEntry(source, message,type);
else
this.eventLogWriter.WriteEntry(this.eventLogWriter.Source, message,type);
}
/// <summary>
/// Writes an entry with the given message text, application-defined event identifier, and application-defined category to the event log.
/// </summary>
/// <param name="message">The string to write to the event log.</param>
/// <param name="type">One of the <see cref="EventLogEntryType">EventLogEntryType</see> values.</param>
/// <param name="eventID">The application-specific identifier for the event.</param>
/// <param name="category">The application-specific subcategory associated with the message.</param>
public void WriteEntry(string message, EventLogEntryType type, int eventID, short category)
{
this.CheckForValidLog();
this.eventLogWriter.WriteEntry(message,type,eventID, category);
}
/// <summary>
/// Writes an entry with the given message text and application-defined event identifier to the event log, using the specified registered event source.
/// </summary>
/// <param name="source">The source by which the application is registered on the specified computer. </param>
/// <param name="message">the string to write to the event log.</param>
/// <param name="type">One of the <see cref="EventLogEntryType">EventLogEntryType</see> values.</param>
/// <param name="eventID">The application-specific identifier for the event.</param>
public void WriteEntry(string source, string message, EventLogEntryType type, int eventID)
{
this.CheckForValidLog();
if(source.Length>0)
this.eventLogWriter.WriteEntry(source, message, type, eventID);
else
this.eventLogWriter.WriteEntry(this.eventLogWriter.Source, message, type, eventID);
}
/// <summary>
/// Writes an entry with the given message text, application-defined event identifier, and application-defined category to the event log, and appends binary data to the message.
/// </summary>
/// <param name="message">the string to write to the event log.</param>
/// <param name="type">One of the <see cref="EventLogEntryType">EventLogEntryType</see> values.</param>
/// <param name="eventID">The application-specific identifier for the event.</param>
/// <param name="category">The application-specific subcategory associated with the message. </param>
/// <param name="rawData">An array of bytes that holds the binary data associated with the entry. </param>
public void WriteEntry(string message, EventLogEntryType type, int eventID, short category, byte[] rawData)
{
this.CheckForValidLog();
this.eventLogWriter.WriteEntry(message,type, eventID, category, rawData);
}
/// <summary>
/// Writes an entry with the given message text, application-defined event identifier, and application-defined category to the event log, using the specified registered event source. The category can be used to filter events in the log.
/// </summary>
/// <param name="source">The source by which the application is registered on the specified computer. </param>
/// <param name="message">the string to write to the event log.</param>
/// <param name="type">One of the <see cref="EventLogEntryType">EventLogEntryType</see> values.</param>
/// <param name="eventID">The application-specific identifier for the event.</param>
/// <param name="category">The application-specific subcategory associated with the message. </param>
public void WriteEntry(string source, string message, EventLogEntryType type, int eventID, short category)
{
this.CheckForValidLog();
if(source.Length>0)
this.eventLogWriter.WriteEntry(source, message,type, eventID, category);
else
this.eventLogWriter.WriteEntry(this.eventLogWriter.Source, message,type, eventID, category);
}
/// <summary>
/// Writes an entry with the given message text, application-defined event identifier, and application-defined category to the event log (using the specified registered event source) and appends binary data to the message.
/// </summary>
/// <param name="source">The source by which the application is registered on the specified computer. </param>
/// <param name="message">the string to write to the event log.</param>
/// <param name="type">One of the <see cref="EventLogEntryType">EventLogEntryType</see> values.</param>
/// <param name="eventID">The application-specific identifier for the event.</param>
/// <param name="category">The application-specific subcategory associated with the message. </param>
/// <param name="rawData">An array of bytes that holds the binary data associated with the entry. </param>
public void WriteEntry(string source, string message, EventLogEntryType type, int eventID, short category, byte[] rawData)
{
this.CheckForValidLog();
if(source.Length>0)
this.eventLogWriter.WriteEntry(source,message,type,eventID, category, rawData);
else
this.eventLogWriter.WriteEntry(this.eventLogWriter.Source,message,type,eventID, category, rawData);
}
#endregion
#region Private Methods
/// <summary>
/// Checks to see if the source is set
/// </summary>
/// <returns></returns>
private void CheckForValidLog()
{
//Check to see if the log was closed
if(this.eventLogWriter.Log.Length==0)
throw new Exception("Unable to write to the log because it has been closed.");
//Check to see if the source is set.
if(this.eventLogWriter.Source.Length==0)
throw new Exception("Source must be set before writting to the log.");
}
/// <summary>
/// Creates the event log Writer object
/// </summary>
private void CreateEventLogWriter(string log, string source, string logPath, string logFileName)
{
if(this.eventLogWriterType!=EventLogWriterType.Custom)
{
//Create the log Writer
if(this.eventLogWriterType == EventLogWriterType.XML)
this.eventLogWriter = new XMLEventLogWriter(log, source, logPath, logFileName);
else
throw new NotSupportedException("Only XMLEventLogWriter is currently supported.");
}
//Begin listening for the event
this.eventLogWriter.EntryWritten+=new EntryWrittenEventHandler(eventLogWriter_EntryWritten);
this.eventLogWriter.EventLogCollectionUpdated +=new EventHandler(eventLogWriter_EventLogCollectionUpdated);
}
/// <summary>
/// Notifies any listeners that an entry was written to the log
/// </summary>
/// <param name="e">the entry item that was written to the log</param>
private void OnEntryWritten(EventLogEntry e)
{
if(this.enableRaisingEvents)
{
if(EntryWritten!=null)
EntryWritten(this,e);
}
}
/// <summary>
/// Notifies any listeners that the log was cleared
/// </summary>
private void OnLogCleared()
{
if(this.enableRaisingEvents)
{
if(LogCleared!=null)
LogCleared(this,EventArgs.Empty);
}
}
/// <summary>
/// Notifies any listeners that log display name was changed
/// </summary>
private void OnLogDisplayNameChanged()
{
if(this.enableRaisingEvents)
{
if(LogDisplayNameChanged!=null)
LogDisplayNameChanged(this,EventArgs.Empty);
}
}
/// <summary>
/// Notifies any listeners that log was closed
/// </summary>
private void OnLogClosed()
{
if(this.enableRaisingEvents)
{
if(LogClosed!=null)
LogClosed(this,EventArgs.Empty);
}
}
/// <summary>
/// Notifies any listeners that log was changed
/// </summary>
private void OnLogChanged()
{
if(this.enableRaisingEvents)
{
if(LogChanged!=null)
LogChanged(this,EventArgs.Empty);
}
}
/// <summary>
/// Notifies any listeners that source was changed
/// </summary>
private void OnSourceChanged()
{
if(this.enableRaisingEvents)
{
if(SourceChanged!=null)
SourceChanged(this,EventArgs.Empty);
}
}
/// <summary>
/// Notifies any listeners that a log was added to the event log
/// </summary>
private void OnEventLogAdded()
{
if(this.enableRaisingEvents)
{
if(EventLogAdded!=null)
EventLogAdded(this,EventArgs.Empty);
}
}
/// <summary>
/// Handler for the entry written event in the eventLogWriter
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void eventLogWriter_EntryWritten(object sender, EventLogEntry e)
{
this.OnEntryWritten(e);
}
/// <summary>
/// event listeners when the eventlog collection is updated
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void eventLogWriter_EventLogCollectionUpdated(object sender, EventArgs e)
{
this.OnEventLogAdded();
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -