internal static void WriteLogSessionEnd() { string appName = Process.GetCurrentProcess().ProcessName; DateTime now = DateTime.Now; string msg = string.Format("Application ended."); LogEntry entry = new LogEntry(SeverityLevels.Info, msg, Assembly.GetCallingAssembly().GetName().Name); WriteLogEntry(entry); entry = new LogEntry(SeverityLevels.Automatic, "END_LOG_SESSION", Assembly.GetCallingAssembly().GetName().Name); WriteLogEntry(entry); }
private void LogException(Exception ex) { LogEntry entry = new LogEntry("Unhandled exception:", ex, Assembly.GetCallingAssembly().GetName().Name); Logger.WriteLogEntry(entry); }
internal static void WriteLogEntry(LogEntry entry) { if (entry.SeverityLevel == SeverityLevels.HeavyTrace) { Debug.WriteLine("HTRC: " + entry.Message); Debug.Flush(); } else { WriteLogString(entry.AppName, entry.ToString()); } }
private static void WriteLogSessionStart() { string appName = Process.GetCurrentProcess().ProcessName; DateTime now = DateTime.Now; sessionID = now.Ticks; LogEntry entry = new LogEntry(SeverityLevels.Automatic, "BEGIN_LOG_SESSION", Assembly.GetCallingAssembly().GetName().Name); WriteLogEntry(entry); string msg = string.Format("Application started with LogLevels: {0}.", LoggingConfiguration.ReportEnabledLevels()); entry = new LogEntry(SeverityLevels.Info, msg, Assembly.GetCallingAssembly().GetName().Name); WriteLogEntry(entry); }
private static void ThreadLoop() { LogInfo( "Logger thread loop has begun ..."); // Purge old log files on startup PurgeOldLogFiles(); while (instance.loggerThreadMustRun || instance.entries.Count > 0) { ProcessQueue(); DateTime now = DateTime.Now; if (now.Minute % 60 == 0) { // Each hour of continous uptime, purge old log files PurgeOldLogFiles(); } System.Threading.Thread.Sleep(10); } LogEntry entry = new LogEntry(SeverityLevels.Info, "Logger thread loop has terminated.", Assembly.GetCallingAssembly().GetName().Name); WriteLogEntry(entry); }
private static void PurgeOldLogFiles() { IEnumerable<string> logFiles = Directory.EnumerateFiles(LoggingConfiguration.LogFilePath, "*.log"); foreach (string logFile in logFiles) { try { if (File.Exists(logFile)) { string name = Path.GetFileNameWithoutExtension(logFile); DateTime logFileDate = GetDateFromLogFileName(name); TimeSpan diff = DateTime.Now.Subtract(logFileDate); if (diff.Days >= LoggingConfiguration.DaysToKeepLogs) { // Old file. Delete it. File.Delete(logFile); } } } catch (Exception ex) { LogEntry entry = new LogEntry(SeverityLevels.Exception, ex.Message, "Logger thread"); WriteLogEntry(entry); } } }
private void EnqueueLogEntry(LogEntry entry) { // Don't add any more entries after thread was requested to stop. if (loggerThreadMustRun) { entries.Enqueue(entry); } }
public static void StopLogger() { if (LoggingConfiguration.LoggingEnabled) { if (instance.loggerThread.IsAlive) { LogTrace("Logger thread was requested to stop..."); instance.loggerThreadMustRun = false; instance.loggerThread.Join(); LogEntry entry = new LogEntry(SeverityLevels.Info, "Logger thread has stopped.", Assembly.GetCallingAssembly().GetName().Name); WriteLogEntry(entry); } } }
public static void LogException(Exception ex, string format, params object[] args) { string message = string.Format(format, args); Debug.WriteLine("EXC: " + ErrorDispatcher.GetErrorMessageForException(ex, true)); if (LoggingConfiguration.LoggingEnabled) { LogEntry entry = new LogEntry(message, ex, Assembly.GetCallingAssembly().GetName().Name); instance.EnqueueLogEntry(entry); } }
public static void LogError(string format, params object[] args) { string message = string.Format(format, args); Debug.WriteLine("ERR: " + message); if (LoggingConfiguration.ErrorLevelEnabled) { LogEntry entry = new LogEntry(SeverityLevels.Error, message, Assembly.GetCallingAssembly().GetName().Name); instance.EnqueueLogEntry(entry); } }
public static void LogTrace_WithStackDump(string format, params object[] args) { StackTrace st = new StackTrace(); string message = string.Format(format, args); message += st.ToString(); string[] lines = message.Split('\n'); foreach (string line in lines) { Debug.WriteLine("TRC: " + line); } if (LoggingConfiguration.TraceLevelEnabled) { LogEntry entry = new LogEntry(SeverityLevels.Trace, message, Assembly.GetCallingAssembly().GetName().Name); instance.EnqueueLogEntry(entry); } }