using System;
using System.Diagnostics;
using Neo.Afx.ComponentModel;
namespace Neo.Afx.Diagnostics
{
///
/// A logging utility.
///
/// The must be created once off before this utility is first used.
/// Use the Neo.Afx.Utils.EventSourceGenerator to do this.
///
///
public static class LogUtility
{
///
/// The log source which is "neo AFX" in the Application event log.
///
public const string AfxLogSource = "neo AFX";
#region WriteApplicationEventLog - AfxLogSource
///
/// A helper function that writes exception detail to the .
///
/// The exception to be logged.
public static void WriteApplicationEventLog(Exception e)
{
WriteApplicationEventLog(e, 0);
}
///
/// A helper function that writes exception detail to the .
///
/// The exception to be logged.
/// The application-specific identifier for the event.
public static void WriteApplicationEventLog(Exception e, int eventID)
{
WriteApplicationEventLog(EventLogEntryType.Error, eventID, e.ToStringComplete());
}
///
/// A helper function that writes a message to the .
///
/// The type of event log entry.
/// The application-specific identifier for the event.
/// The format string.
/// The arguments to the .
public static void WriteApplicationEventLog(EventLogEntryType entryType, int eventID, string format, params object[] args)
{
EventLog.WriteEntry(AfxLogSource, string.Format(format, args), entryType, eventID);
}
#endregion
#region WriteApplicationEventLog
///
/// A helper function that writes exception detail to the .
///
/// The log source for the event.
/// The exception to be logged.
public static void WriteApplicationEventLog(string logSource, Exception e)
{
WriteApplicationEventLog(logSource, e, 0);
}
///
/// A helper function that writes exception detail to the .
///
/// The log source for the event.
/// The exception to be logged.
/// The application-specific identifier for the event.
public static void WriteApplicationEventLog(string logSource, Exception e, int eventID)
{
EventLog.WriteEntry(logSource, e.ToStringComplete(), EventLogEntryType.Error, eventID);
}
///
/// A helper function that writes a message to the .
///
/// The log source for the event.
/// The type of event log entry.
/// The format string.
/// The arguments to the .
public static void WriteApplicationEventLog(string logSource, EventLogEntryType entryType, string format, params object[] args)
{
WriteApplicationEventLog(logSource, entryType, 0, format, args);
}
///
/// A helper function that writes a message to the .
///
/// The log source for the event.
/// The type of event log entry.
/// The application-specific identifier for the event.
/// The format string.
/// The arguments to the .
public static void WriteApplicationEventLog(string logSource, EventLogEntryType entryType, int eventID, string format, params object[] args)
{
EventLog.WriteEntry(logSource, string.Format(format, args), entryType, eventID);
}
#endregion
#region WriteTrace
///
/// Writes a time-stamped to the trace file.
///
/// The source of the trace.
/// The message to be written.
public static void WriteTrace(string source, string message)
{
Trace.WriteLine(
String.Format("{0} {1} - {2}", DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss"), source, message));
}
#endregion
}
}