using System; using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.Text; //namespace SmartPSS.Classes //{ public static class ApplicationEventLog { static ApplicationEventLog() { log = new EventLog(); log.Log = "Application"; log.MachineName = "."; } public static bool UseAutoEventSource { get { return mUseAutoEventSource; } set { mUseAutoEventSource = value; } } private static bool mUseAutoEventSource = true; public static string EventSource { get { if (UseAutoEventSource) { try { Assembly assembly = Assembly.GetCallingAssembly(); object[] attributes = assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), false); if (attributes.Length <= 0) { return assembly.FullName; } else { AssemblyProductAttribute productAttribute = attributes[0] as AssemblyProductAttribute; return productAttribute.Product; } } catch { return "Unknown"; } } else { return mEventSource; } } set { mEventSource = value; } } private static string mEventSource = string.Empty; public static bool WriteEntry(string message, EventLogEntryType eventLogEntryType) { try { log.Source = EventSource; log.WriteEntry(message, eventLogEntryType); return true; } catch { //Either the event log is full or we are working on an older OS //that does not support Event Logging capability return false; } } public static bool WriteException(Exception ex) { try { log.Source = EventSource; log.WriteEntry("Exception Type: " + ex.GetType().FullName + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace ,EventLogEntryType.Error); return true; } catch { //Either the event log is full or we are working on an older OS //that does not support Event Logging capability return false; } } public static bool WriteException(Exception ex, string message) { try { log.Source = EventSource; log.WriteEntry("Exception Type: " + ex.GetType().FullName + Environment.NewLine + message + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace ,EventLogEntryType.Error); return true; } catch { //Either the event log is full or we are working on an older OS //that does not support Event Logging capability return false; } } public static bool WriteError(string message) { try { log.Source = EventSource; log.WriteEntry(message, EventLogEntryType.Error); return true; } catch { //Either the event log is full or we are working on an older OS //that does not support Event Logging capability return false; } } public static bool WriteWarning(string message) { try { log.Source = EventSource; log.WriteEntry(message, EventLogEntryType.Warning); return true; } catch { //Either the event log is full or we are working on an older OS //that does not support Event Logging capability return false; } } public static bool WriteInformation(string message) { try { log.Source = EventSource; log.WriteEntry(message, EventLogEntryType.Information); return true; } catch { //Either the event log is full or we are working on an older OS //that does not support Event Logging capability return false; } } private static EventLog log; } //}