unfied log format validation and log format example

This commit is contained in:
bao-qian 2017-01-25 00:24:00 +00:00
parent c361d8d16b
commit 052048ad4c

View File

@ -41,14 +41,25 @@ namespace Wox.Infrastructure.Logger
logger.Fatal(message); logger.Fatal(message);
} }
public static void Error(string message) private static bool FormatValid(string message)
{ {
var parts = message.Split('|'); var parts = message.Split('|');
if (parts.Length == 3 && !string.IsNullOrWhiteSpace(parts[1]) && !string.IsNullOrWhiteSpace(parts[2])) var valid = parts.Length == 3 && !string.IsNullOrWhiteSpace(parts[1]) && !string.IsNullOrWhiteSpace(parts[2]);
return valid;
}
/// <param name="message">example: "|prefix|unprefixed" </param>
public static void Error(string message)
{ {
var logger = LogManager.GetLogger(parts[1]); if (FormatValid(message))
{
var parts = message.Split('|');
var prefix = parts[1];
var unprefixed = parts[2];
var logger = LogManager.GetLogger(prefix);
System.Diagnostics.Debug.WriteLine($"ERROR|{message}"); System.Diagnostics.Debug.WriteLine($"ERROR|{message}");
logger.Error(parts[2]); logger.Error(unprefixed);
} }
else else
{ {
@ -56,26 +67,29 @@ namespace Wox.Infrastructure.Logger
} }
} }
/// <param name="message">example: "|prefix|unprefixed" </param>
[MethodImpl(MethodImplOptions.Synchronized)] [MethodImpl(MethodImplOptions.Synchronized)]
public static void Exception(string message, System.Exception e) public static void Exception(string message, System.Exception e)
{ {
#if DEBUG #if DEBUG
throw e; throw e;
#else #else
var parts = message.Split('|'); if (FormatValid(message))
if (parts.Length == 3 && !string.IsNullOrWhiteSpace(parts[1]) && !string.IsNullOrWhiteSpace(parts[2]))
{ {
var logger = LogManager.GetLogger(parts[1]); var parts = message.Split('|');
var prefix = parts[1];
var unprefixed = parts[2];
var logger = LogManager.GetLogger(prefix);
System.Diagnostics.Debug.WriteLine($"ERROR|{message}"); System.Diagnostics.Debug.WriteLine($"ERROR|{message}");
logger.Error("-------------------------- Begin exception --------------------------"); logger.Error("-------------------------- Begin exception --------------------------");
logger.Error(parts[2]); logger.Error(unprefixed);
do do
{ {
logger.Error($"Exception message:\n <{e.Message}>"); logger.Error($"Exception message:\n <{e.Message}>");
logger.Error($"Exception stack trace:\n<{e.StackTrace}>"); logger.Error($"Exception stack trace:\n <{e.StackTrace}>");
e = e.InnerException; e = e.InnerException;
} while (e != null); } while (e != null);
@ -88,14 +102,18 @@ namespace Wox.Infrastructure.Logger
#endif #endif
} }
/// <param name="message">example: "|prefix|unprefixed" </param>
public static void Debug(string message) public static void Debug(string message)
{ {
var parts = message.Split('|'); if (FormatValid(message))
if (parts.Length == 3 && !string.IsNullOrWhiteSpace(parts[1]) && !string.IsNullOrWhiteSpace(parts[2]))
{ {
var logger = LogManager.GetLogger(parts[1]); var parts = message.Split('|');
var prefix = parts[1];
var unprefixed = parts[2];
var logger = LogManager.GetLogger(prefix);
System.Diagnostics.Debug.WriteLine($"DEBUG|{message}"); System.Diagnostics.Debug.WriteLine($"DEBUG|{message}");
logger.Debug(parts[2]); logger.Debug(unprefixed);
} }
else else
{ {
@ -103,14 +121,18 @@ namespace Wox.Infrastructure.Logger
} }
} }
/// <param name="message">example: "|prefix|unprefixed" </param>
public static void Info(string message) public static void Info(string message)
{ {
var parts = message.Split('|'); if (FormatValid(message))
if (parts.Length == 3 && !string.IsNullOrWhiteSpace(parts[1]) && !string.IsNullOrWhiteSpace(parts[2]))
{ {
var logger = LogManager.GetLogger(parts[1]); var parts = message.Split('|');
var prefix = parts[1];
var unprefixed = parts[2];
var logger = LogManager.GetLogger(prefix);
System.Diagnostics.Debug.WriteLine($"INFO|{message}"); System.Diagnostics.Debug.WriteLine($"INFO|{message}");
logger.Info(parts[2]); logger.Info(unprefixed);
} }
else else
{ {
@ -118,14 +140,18 @@ namespace Wox.Infrastructure.Logger
} }
} }
/// <param name="message">example: "|prefix|unprefixed" </param>
public static void Warn(string message) public static void Warn(string message)
{ {
var parts = message.Split('|'); if (FormatValid(message))
if (parts.Length == 3 && !string.IsNullOrWhiteSpace(parts[1]) && !string.IsNullOrWhiteSpace(parts[2]))
{ {
var logger = LogManager.GetLogger(parts[1]); var parts = message.Split('|');
var prefix = parts[1];
var unprefixed = parts[2];
var logger = LogManager.GetLogger(prefix);
System.Diagnostics.Debug.WriteLine($"WARN|{message}"); System.Diagnostics.Debug.WriteLine($"WARN|{message}");
logger.Warn(parts[2]); logger.Warn(unprefixed);
} }
else else
{ {