229 lines
7.4 KiB
C#
229 lines
7.4 KiB
C#
namespace ServiceShared
|
|
{
|
|
public static class Log
|
|
{
|
|
/// <summary>
|
|
/// Log path
|
|
/// </summary>
|
|
private static string LogPath = null;
|
|
|
|
/// <summary>
|
|
/// Current LogType from the configuration
|
|
/// </summary>
|
|
private static Log.Types _LogType = Log.Types.INFO;
|
|
|
|
/// <summary>
|
|
/// Flag to cotrol writing queue
|
|
/// </summary>
|
|
private static bool LogWriting = false;
|
|
|
|
/// <summary>
|
|
/// Error types
|
|
/// </summary>
|
|
public enum Types { CRITICAL, ERROR, INFO, LOG, TRACE, DEBUG, WARNING }
|
|
|
|
/// <summary>
|
|
/// Database controller for trace logs
|
|
/// </summary>
|
|
private static Database.Controllers.Traces dbTraces = null;
|
|
|
|
/// <summary>
|
|
/// Sets database controller for trace logs
|
|
/// </summary>
|
|
/// <param name="_dbTraces"></param>
|
|
public static void SetTraceDbController(Database.Controllers.Traces _dbTraces)
|
|
{
|
|
dbTraces = _dbTraces;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the log path
|
|
/// </summary>
|
|
/// <param name="logPath">log path</param>
|
|
public static void SetLogPath(string logPath)
|
|
{
|
|
try
|
|
{
|
|
LogPath = logPath;
|
|
|
|
if (!Directory.Exists(LogPath))
|
|
{
|
|
Directory.CreateDirectory(LogPath);
|
|
}
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
Log.Error(ex, "Log", "SetLogPath");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the log type
|
|
/// </summary>
|
|
/// <param name="Log.Types">Log.Types</param>
|
|
public static void SetLog(Log.Types logType)
|
|
{
|
|
_LogType = logType;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes a critical log in the file and sends it via mail
|
|
/// </summary>
|
|
/// <param name="ex">Exception that was throwen</param>
|
|
/// <param name="className">Class name where the exception was throwen</param>
|
|
/// <param name="methodeName">Methode name where the exception was throwen</param>
|
|
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");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes a error log in the file and (optional sends it via mail)
|
|
/// </summary>
|
|
/// <param name="ex">Exception that was throwen</param>
|
|
/// <param name="className">Class name where the exception was throwen</param>
|
|
/// <param name="methodeName">Methode name where the exception was throwen</param>
|
|
/// <param name="sendMail">Flag if this log has to be sent via mail</param>
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes a warning log in the file
|
|
/// </summary>
|
|
/// <param name="message">warning message</param>
|
|
/// <param name="className">Class name where the exception was throwen</param>
|
|
/// <param name="methodeName">Methode name where the exception was throwen</param>
|
|
public static void Warning(string message, string className, string methodeName)
|
|
{
|
|
Log.Write(message + "(" + className + " => " + methodeName + ")", Types.WARNING);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates trace log in the database
|
|
/// </summary>
|
|
/// <param name="message">Message of trace log</param>
|
|
/// <param name="pgs">PGS of results</param>
|
|
/// <param name="udid">UDID of device</param>
|
|
/// <param name="type">Type of trace log</param>
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates trace log in the database
|
|
/// </summary>
|
|
/// <param name="traces">Traces object</param>
|
|
public static void Trace(Models.Database.Traces traces)
|
|
{
|
|
try
|
|
{
|
|
if (dbTraces != null)
|
|
{
|
|
dbTraces.Create(traces);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Write(ex.Message, Types.TRACE);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes a info message to the log
|
|
/// </summary>
|
|
/// <param name="message">message that should be written in the log file</param>
|
|
public static void Info(string message)
|
|
{
|
|
Log.Write(message, Types.INFO);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes a debug message to the log
|
|
/// </summary>
|
|
/// <param name="message">message that should be written in the log file</param>
|
|
public static void Debug(string message)
|
|
{
|
|
Log.Write(message, Types.DEBUG);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes message to the file
|
|
/// </summary>
|
|
/// <param name="message">message</param>
|
|
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());
|
|
}
|
|
}
|
|
}
|
|
}
|