Flow.Launcher/Flow.Launcher.Infrastructure/Logger/Log.cs

215 lines
7.1 KiB
C#
Raw Permalink Normal View History

2019-08-20 11:29:30 +00:00
using System.Diagnostics;
using System.IO;
2016-11-30 00:30:42 +00:00
using System.Runtime.CompilerServices;
using NLog;
using NLog.Config;
using NLog.Targets;
2020-04-21 09:12:17 +00:00
using Flow.Launcher.Infrastructure.UserSettings;
2021-06-11 04:40:07 +00:00
using JetBrains.Annotations;
using NLog.Fluent;
2021-06-11 04:40:07 +00:00
using NLog.Targets.Wrappers;
using System.Runtime.ExceptionServices;
using System.Text;
2013-12-20 11:38:10 +00:00
2020-04-21 09:12:17 +00:00
namespace Flow.Launcher.Infrastructure.Logger
2013-12-20 11:38:10 +00:00
{
public static class Log
2013-12-20 11:38:10 +00:00
{
public const string DirectoryName = "Logs";
public static string CurrentLogDirectory { get; }
2020-01-08 23:13:56 +00:00
static Log()
{
CurrentLogDirectory = Path.Combine(DataLocation.DataDirectory(), DirectoryName, Constant.Version);
2020-01-08 23:13:56 +00:00
if (!Directory.Exists(CurrentLogDirectory))
{
2020-01-08 23:13:56 +00:00
Directory.CreateDirectory(CurrentLogDirectory);
}
2013-12-20 11:38:10 +00:00
var configuration = new LoggingConfiguration();
2021-06-25 09:52:42 +00:00
const string layout =
@"${date:format=HH\:mm\:ss.ffffK} - " +
@"${level:uppercase=true:padding=-5} - ${logger} - ${message:l}" +
@"${onexception:${newline}" +
2021-06-25 09:52:42 +00:00
@"EXCEPTION OCCURS\: ${exception:format=tostring}${newline}}";
2021-06-11 04:44:48 +00:00
var fileTarget = new FileTarget
{
FileName = CurrentLogDirectory.Replace(@"\", "/") + "/${shortdate}.txt",
Layout = layout
2021-06-11 04:44:48 +00:00
};
2021-06-11 04:40:07 +00:00
var fileTargetASyncWrapper = new AsyncTargetWrapper(fileTarget);
var debugTarget = new OutputDebugStringTarget
{
Layout = layout
};
2021-06-11 04:40:07 +00:00
configuration.AddTarget("file", fileTargetASyncWrapper);
configuration.AddTarget("debug", debugTarget);
2017-01-12 19:52:37 +00:00
#if DEBUG
var fileRule = new LoggingRule("*", LogLevel.Debug, fileTargetASyncWrapper);
var debugRule = new LoggingRule("*", LogLevel.Debug, debugTarget);
configuration.LoggingRules.Add(debugRule);
2017-01-12 19:52:37 +00:00
#else
var fileRule = new LoggingRule("*", LogLevel.Info, fileTargetASyncWrapper);
2017-01-12 19:52:37 +00:00
#endif
configuration.LoggingRules.Add(fileRule);
LogManager.Configuration = configuration;
}
2016-11-30 00:30:42 +00:00
2017-01-24 00:24:20 +00:00
private static void LogFaultyFormat(string message)
{
2017-01-24 00:24:20 +00:00
var logger = LogManager.GetLogger("FaultyLogger");
message = $"Wrong logger message format <{message}>";
logger.Fatal(message);
}
2016-05-15 16:03:06 +00:00
private static bool FormatValid(string message)
2015-01-11 13:52:30 +00:00
{
2017-01-24 00:24:20 +00:00
var parts = message.Split('|');
var valid = parts.Length == 3 && !string.IsNullOrWhiteSpace(parts[1]) && !string.IsNullOrWhiteSpace(parts[2]);
return valid;
}
public static void Exception(string className, string message, System.Exception exception, [CallerMemberName] string methodName = "")
{
2021-06-11 04:40:07 +00:00
exception = exception.Demystify();
#if DEBUG
2021-06-11 04:40:07 +00:00
ExceptionDispatchInfo.Capture(exception).Throw();
#else
var classNameWithMethod = CheckClassAndMessageAndReturnFullClassWithMethod(className, message, methodName);
ExceptionInternal(classNameWithMethod, message, exception);
#endif
}
private static string CheckClassAndMessageAndReturnFullClassWithMethod(string className, string message,
string methodName)
{
if (string.IsNullOrWhiteSpace(className))
{
LogFaultyFormat($"Fail to specify a class name during logging of message: {message ?? "no message entered"}");
}
if (string.IsNullOrWhiteSpace(message))
{
// todo: not sure we really need that
LogFaultyFormat($"Fail to specify a message during logging");
}
if (!string.IsNullOrWhiteSpace(methodName))
{
return className + "." + methodName;
}
return className;
}
private static void ExceptionInternal(string classAndMethod, string message, System.Exception e)
{
var logger = LogManager.GetLogger(classAndMethod);
var messageBuilder = new StringBuilder();
logger.Error(e, message);
}
private static void LogInternal(string message, LogLevel level)
{
if (FormatValid(message))
2017-01-24 00:24:20 +00:00
{
var parts = message.Split('|');
var prefix = parts[1];
var unprefixed = parts[2];
var logger = LogManager.GetLogger(prefix);
logger.Log(level, unprefixed);
2017-01-24 00:24:20 +00:00
}
else
2016-11-30 00:30:42 +00:00
{
2017-01-24 00:24:20 +00:00
LogFaultyFormat(message);
}
2016-11-30 00:30:42 +00:00
}
/// <param name="message">example: "|prefix|unprefixed" </param>
/// <param name="e">Exception</param>
2017-01-24 00:24:20 +00:00
public static void Exception(string message, System.Exception e)
2016-05-15 16:03:06 +00:00
{
2021-06-11 04:40:07 +00:00
e = e.Demystify();
2017-01-24 00:24:20 +00:00
#if DEBUG
2021-06-11 04:40:07 +00:00
ExceptionDispatchInfo.Capture(e).Throw();
2017-01-24 00:24:20 +00:00
#else
if (FormatValid(message))
2015-11-11 00:42:49 +00:00
{
var parts = message.Split('|');
var prefix = parts[1];
var unprefixed = parts[2];
ExceptionInternal(prefix, unprefixed, e);
2017-01-24 00:24:20 +00:00
}
else
{
LogFaultyFormat(message);
}
#endif
2014-03-23 09:43:46 +00:00
}
/// <param name="message">example: "|prefix|unprefixed" </param>
public static void Error(string message)
2016-11-30 00:39:03 +00:00
{
LogInternal(message, LogLevel.Error);
}
public static void Error(string className, string message, [CallerMemberName] string methodName = "")
{
LogInternal(LogLevel.Error, className, message, methodName);
}
private static void LogInternal(LogLevel level, string className, string message, [CallerMemberName] string methodName = "")
{
var classNameWithMethod = CheckClassAndMessageAndReturnFullClassWithMethod(className, message, methodName);
var logger = LogManager.GetLogger(classNameWithMethod);
logger.Log(level, message);
}
public static void Debug(string className, string message, [CallerMemberName] string methodName = "")
{
LogInternal(LogLevel.Debug, className, message, methodName);
}
/// <param name="message">example: "|prefix|unprefixed" </param>
public static void Debug(string message)
{
LogInternal(message, LogLevel.Debug);
2016-11-30 00:39:03 +00:00
}
public static void Info(string className, string message, [CallerMemberName] string methodName = "")
{
LogInternal(LogLevel.Info, className, message, methodName);
}
/// <param name="message">example: "|prefix|unprefixed" </param>
2017-01-24 00:24:20 +00:00
public static void Info(string message)
2014-03-23 09:43:46 +00:00
{
LogInternal(message, LogLevel.Info);
2014-03-23 09:43:46 +00:00
}
public static void Warn(string className, string message, [CallerMemberName] string methodName = "")
{
LogInternal(LogLevel.Warn, className, message, methodName);
}
/// <param name="message">example: "|prefix|unprefixed" </param>
2017-01-24 00:24:20 +00:00
public static void Warn(string message)
2014-03-23 09:43:46 +00:00
{
LogInternal(message, LogLevel.Warn);
2014-03-23 09:43:46 +00:00
}
2013-12-20 11:38:10 +00:00
}
2022-01-05 07:32:40 +00:00
}