namespace ServiceShared { public static class Log { /// /// Log path /// private static string LogPath = null; /// /// Current LogType from the configuration /// private static Log.Types _LogType = Log.Types.INFO; /// /// Flag to cotrol writing queue /// private static bool LogWriting = false; /// /// Error types /// public enum Types { CRITICAL, ERROR, INFO, LOG, TRACE, DEBUG, WARNING } /// /// Database controller for trace logs /// private static Database.Controllers.Traces dbTraces = null; /// /// Sets database controller for trace logs /// /// public static void SetTraceDbController(Database.Controllers.Traces _dbTraces) { dbTraces = _dbTraces; } /// /// Sets the log path /// /// log path public static void SetLogPath(string logPath) { try { LogPath = logPath; if (!Directory.Exists(LogPath)) { Directory.CreateDirectory(LogPath); } } catch(Exception ex) { Log.Error(ex, "Log", "SetLogPath"); } } /// /// Sets the log type /// /// Log.Types public static void SetLog(Log.Types logType) { _LogType = logType; } /// /// Writes a critical log in the file and sends it via mail /// /// Exception that was throwen /// Class name where the exception was throwen /// Methode name where the exception was throwen public static void Critical(Exception ex, string className, string methodeName) { try { string msg = ex.Message + "(" + className + " => " + methodeName + ")"; Log.Write(msg, Types.CRITICAL); string subject = "[" + Types.CRITICAL.ToString() + "][" + className + "][" + methodeName + "]"; Mail.SendLogs(subject, msg); } catch (Exception ex1) { Log.Error(ex1, "ServiceShared.Log", "Critical"); } } /// /// Writes a error log in the file and (optional sends it via mail) /// /// Exception that was throwen /// Class name where the exception was throwen /// Methode name where the exception was throwen /// Flag if this log has to be sent via mail public static void Error(Exception ex, string className, string methodeName, bool sendMail = false) { if(sendMail) { Log.Critical(ex, className, methodeName); } else { Log.Write(ex.Message + "(" + className + " => " + methodeName + ")", Types.ERROR); } } /// /// Writes a warning log in the file /// /// warning message /// Class name where the exception was throwen /// Methode name where the exception was throwen public static void Warning(string message, string className, string methodeName) { Log.Write(message + "(" + className + " => " + methodeName + ")", Types.WARNING); } /// /// Creates trace log in the database /// /// Message of trace log /// PGS of results /// UDID of device /// Type of trace log public static void Trace(string message, string pgs, string udid = null, Types type = Types.TRACE) { try { if(dbTraces != null) { dbTraces.Create(new Models.Database.Traces() { PGS = pgs, UDID = udid, Message = message, TraceType = type }); } } catch (Exception ex) { Log.Write(ex.Message, Types.TRACE); } } /// /// Creates trace log in the database /// /// Traces object public static void Trace(Models.Database.Traces traces) { try { if (dbTraces != null) { dbTraces.Create(traces); } } catch (Exception ex) { Log.Write(ex.Message, Types.TRACE); } } /// /// Writes a info message to the log /// /// message that should be written in the log file public static void Info(string message) { Log.Write(message, Types.INFO); } /// /// Writes a debug message to the log /// /// message that should be written in the log file public static void Debug(string message) { Log.Write(message, Types.DEBUG); } /// /// Writes message to the file /// /// message public static void Write(string message, Types type) { try { bool write_log = false; if(type == _LogType || (_LogType == Types.CRITICAL && type == Types.ERROR) || (_LogType == Types.ERROR && type == Types.CRITICAL) || _LogType == Types.DEBUG) { write_log = true; } if(write_log) { string msg = DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss") + " [" + type.ToString() + "]: " + message + "\r\n"; string path = (!string.IsNullOrEmpty(LogPath) ? LogPath + Path.DirectorySeparatorChar : "") + DateTime.Now.ToString("yyyy_MM_dd") + ".log"; if (!LogWriting) { LogWriting = true; File.AppendAllText(path, msg); LogWriting = false; } if (type == Types.DEBUG) { Console.WriteLine(msg); } } } catch (Exception ex) { Console.Write(ex.ToString()); } } } }