Updated
This commit is contained in:
@@ -16,70 +16,45 @@ namespace EonaCat.Logger.Managers
|
||||
{
|
||||
private static readonly object FileLock = new object();
|
||||
|
||||
/// <summary>
|
||||
/// Format a message with the specified header
|
||||
/// </summary>
|
||||
/// <param name="settings">Logger settings</param>
|
||||
/// <param name="logType">logtype for the formatted message</param>
|
||||
/// <param name="currentMessage">The actual message to format with the header</param>
|
||||
/// <param name="dateTime">The dateTime of the message</param>
|
||||
/// <returns></returns>
|
||||
public static string FormatMessageWithHeader(LoggerSettings settings, ELogType logType, string currentMessage, DateTime dateTime)
|
||||
internal static string FormatMessageWithHeader(LoggerSettings settings, ELogType logType, string currentMessage, DateTime dateTime)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(currentMessage))
|
||||
{
|
||||
return currentMessage;
|
||||
}
|
||||
|
||||
if (settings == null)
|
||||
{
|
||||
return "[EonaCatLogger]" + " " + currentMessage;
|
||||
}
|
||||
string header = settings?.HeaderFormat ?? "[EonaCatLogger]";
|
||||
|
||||
string header = settings.HeaderFormat;
|
||||
if (header.Contains("{ts}"))
|
||||
{
|
||||
header = header.Replace("{ts}", dateTime.ToString(settings.TimestampFormat));
|
||||
}
|
||||
header = header.Replace("{ts}", dateTime.ToString(settings?.TimestampFormat));
|
||||
|
||||
if (header.Contains("{host}"))
|
||||
{
|
||||
header = header.Replace("{host}", Dns.GetHostName());
|
||||
}
|
||||
|
||||
if (header.Contains("{thread}"))
|
||||
{
|
||||
header = header.Replace("{thread}", Thread.CurrentThread.ManagedThreadId.ToString());
|
||||
}
|
||||
|
||||
if (header.Contains("{sev}"))
|
||||
{
|
||||
header = header.Replace("{sev}", logType.ToString());
|
||||
}
|
||||
|
||||
string fullMessage = AddHeaderIfNotExists(header, currentMessage);
|
||||
string fullMessage = AddHeaderIfNotExists(settings, header, currentMessage);
|
||||
return fullMessage;
|
||||
}
|
||||
|
||||
private static string AddHeaderIfNotExists(string header, string currentMessage)
|
||||
private static string AddHeaderIfNotExists(LoggerSettings settings, string header, string currentMessage)
|
||||
{
|
||||
if (!currentMessage.Contains("[EonaCatLogger]"))
|
||||
if (settings == null || !settings.RemoveMessagePrefix)
|
||||
{
|
||||
return "[EonaCatLogger]" + " " + header + " " + currentMessage;
|
||||
if (!currentMessage.Contains("[EonaCatLogger]"))
|
||||
return "[EonaCatLogger] " + header + " " + currentMessage;
|
||||
}
|
||||
return currentMessage;
|
||||
|
||||
return header + " " + currentMessage;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Formats a given exception as a string
|
||||
/// </summary>
|
||||
/// <param name="exception">exception</param>
|
||||
/// <param name="module">The name of the module which called the code (optional)</param>
|
||||
/// <param name="method">The name of the method which waws called in code (optional)</param>
|
||||
/// <returns></returns>
|
||||
public static string FormatExceptionToMessage(this Exception exception, string module = null, string method = null)
|
||||
{
|
||||
if (exception == null) return string.Empty;
|
||||
if (exception == null)
|
||||
return string.Empty;
|
||||
|
||||
var st = new StackTrace(exception, true);
|
||||
var frame = st.GetFrame(0);
|
||||
int fileLine = frame.GetFileLineNumber();
|
||||
@@ -90,49 +65,9 @@ namespace EonaCat.Logger.Managers
|
||||
"--- Exception details provided by EonaCatLogger ---" + Environment.NewLine +
|
||||
(!string.IsNullOrEmpty(module) ? " Module : " + module + Environment.NewLine : "") +
|
||||
(!string.IsNullOrEmpty(method) ? " Method : " + method + Environment.NewLine : "") +
|
||||
" Type : " + exception.GetType().ToString() + Environment.NewLine;
|
||||
|
||||
if (exception.Data != null && exception.Data.Count > 0)
|
||||
{
|
||||
message += " Data : " + Environment.NewLine;
|
||||
foreach (DictionaryEntry curr in exception.Data)
|
||||
{
|
||||
message += " | " + curr.Key + ": " + curr.Value + Environment.NewLine;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
message += " Data : (none)" + Environment.NewLine;
|
||||
}
|
||||
|
||||
message +=
|
||||
" Inner : ";
|
||||
|
||||
if (exception.InnerException == null) message += "(null)" + Environment.NewLine;
|
||||
else
|
||||
{
|
||||
message += exception.InnerException.GetType().ToString() + Environment.NewLine;
|
||||
message +=
|
||||
" Message : " + exception.InnerException.Message + Environment.NewLine +
|
||||
" Source : " + exception.InnerException.Source + Environment.NewLine +
|
||||
" StackTrace : " + exception.InnerException.StackTrace + Environment.NewLine +
|
||||
" ToString : " + exception.InnerException.ToString() + Environment.NewLine;
|
||||
|
||||
if (exception.InnerException.Data != null && exception.InnerException.Data.Count > 0)
|
||||
{
|
||||
message += " Data : " + Environment.NewLine;
|
||||
foreach (DictionaryEntry curr in exception.Data)
|
||||
{
|
||||
message += " | " + curr.Key + ": " + curr.Value + Environment.NewLine;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
message += " Data : (none)" + Environment.NewLine;
|
||||
}
|
||||
}
|
||||
|
||||
message +=
|
||||
" Type : " + exception.GetType().ToString() + Environment.NewLine +
|
||||
" Data : " + (exception.Data != null && exception.Data.Count > 0 ? Environment.NewLine + FormatExceptionData(exception.Data) : "(none)") + Environment.NewLine +
|
||||
" Inner : " + (exception.InnerException != null ? FormatInnerException(exception.InnerException) : "(null)") + Environment.NewLine +
|
||||
" Message : " + exception.Message + Environment.NewLine +
|
||||
" Source : " + exception.Source + Environment.NewLine +
|
||||
" StackTrace : " + exception.StackTrace + Environment.NewLine +
|
||||
@@ -140,14 +75,45 @@ namespace EonaCat.Logger.Managers
|
||||
" File : " + filename + Environment.NewLine +
|
||||
" ToString : " + exception.ToString() + Environment.NewLine +
|
||||
"---";
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
private static string FormatExceptionData(IDictionary data)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
foreach (DictionaryEntry entry in data)
|
||||
{
|
||||
sb.Append(" | ")
|
||||
.Append(entry.Key)
|
||||
.Append(": ")
|
||||
.Append(entry.Value)
|
||||
.Append(Environment.NewLine);
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private static string FormatInnerException(Exception innerException)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.AppendLine(innerException.GetType().ToString())
|
||||
.AppendLine(" Message : " + innerException.Message)
|
||||
.AppendLine(" Source : " + innerException.Source)
|
||||
.AppendLine(" StackTrace : " + innerException.StackTrace)
|
||||
.AppendLine(" ToString : " + innerException.ToString())
|
||||
.Append(" Data : ")
|
||||
.AppendLine(innerException.Data != null && innerException.Data.Count > 0 ? Environment.NewLine + FormatExceptionData(innerException.Data) : "(none)");
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
internal static void SendConsole(LoggerSettings settings, ELogType logType, string message, bool writeToConsole)
|
||||
{
|
||||
if (settings == null) return;
|
||||
if (!writeToConsole) return;
|
||||
if (string.IsNullOrWhiteSpace(message)) return;
|
||||
if (settings == null || !writeToConsole || string.IsNullOrWhiteSpace(message))
|
||||
return;
|
||||
|
||||
if (settings.EnableColors)
|
||||
{
|
||||
@@ -203,10 +169,8 @@ namespace EonaCat.Logger.Managers
|
||||
{
|
||||
lock (FileLock)
|
||||
{
|
||||
if (logger == null) return;
|
||||
if (settings == null) return;
|
||||
if (!settings.EnableFileLogging) return;
|
||||
if (string.IsNullOrWhiteSpace(message)) return;
|
||||
if (logger == null || settings == null || !settings.EnableFileLogging || string.IsNullOrWhiteSpace(message))
|
||||
return;
|
||||
|
||||
int tries = 0;
|
||||
bool completed = false;
|
||||
@@ -214,33 +178,29 @@ namespace EonaCat.Logger.Managers
|
||||
{
|
||||
try
|
||||
{
|
||||
if (logType == ELogType.CRITICAL)
|
||||
switch (logType)
|
||||
{
|
||||
logger.LogCritical(message);
|
||||
}
|
||||
else if (logType == ELogType.DEBUG)
|
||||
{
|
||||
logger.LogDebug(message);
|
||||
}
|
||||
else if (logType == ELogType.ERROR)
|
||||
{
|
||||
logger.LogError(message);
|
||||
}
|
||||
else if (logType == ELogType.INFO)
|
||||
{
|
||||
logger.LogInformation(message);
|
||||
}
|
||||
else if (logType == ELogType.TRACE)
|
||||
{
|
||||
logger.LogTrace(message);
|
||||
}
|
||||
else if (logType == ELogType.TRAFFIC)
|
||||
{
|
||||
logger.LogTrace($"[TRAFFIC] {message}");
|
||||
}
|
||||
else if (logType == ELogType.WARNING)
|
||||
{
|
||||
logger.LogWarning(message);
|
||||
case ELogType.CRITICAL:
|
||||
logger.LogCritical(message);
|
||||
break;
|
||||
case ELogType.DEBUG:
|
||||
logger.LogDebug(message);
|
||||
break;
|
||||
case ELogType.ERROR:
|
||||
logger.LogError(message);
|
||||
break;
|
||||
case ELogType.INFO:
|
||||
logger.LogInformation(message);
|
||||
break;
|
||||
case ELogType.TRACE:
|
||||
logger.LogTrace(message);
|
||||
break;
|
||||
case ELogType.TRAFFIC:
|
||||
logger.LogTrace($"[TRAFFIC] {message}");
|
||||
break;
|
||||
case ELogType.WARNING:
|
||||
logger.LogWarning(message);
|
||||
break;
|
||||
}
|
||||
completed = true;
|
||||
}
|
||||
@@ -251,15 +211,14 @@ namespace EonaCat.Logger.Managers
|
||||
if (tries >= maxTries)
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static void SendToSysLogServers(LoggerSettings settings, string message)
|
||||
{
|
||||
if (settings == null) return;
|
||||
if (!settings.SendToSyslogServers) return;
|
||||
if (string.IsNullOrWhiteSpace(message)) return;
|
||||
if (settings == null || !settings.SendToSyslogServers || string.IsNullOrWhiteSpace(message))
|
||||
return;
|
||||
|
||||
if (settings.SysLogServers == null || !settings.SysLogServers.Any())
|
||||
{
|
||||
@@ -294,4 +253,5 @@ namespace EonaCat.Logger.Managers
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user