2022-11-02 17:17:25 +00:00
|
|
|
|
using System.Diagnostics;
|
2016-04-27 21:51:31 +00:00
|
|
|
|
using System.IO;
|
2016-11-30 00:30:42 +00:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2025-04-01 03:47:18 +00:00
|
|
|
|
using System.Runtime.ExceptionServices;
|
|
|
|
|
|
using Flow.Launcher.Infrastructure.UserSettings;
|
2016-04-27 21:51:31 +00:00
|
|
|
|
using NLog;
|
|
|
|
|
|
using NLog.Config;
|
|
|
|
|
|
using NLog.Targets;
|
2021-06-11 04:40:07 +00:00
|
|
|
|
using NLog.Targets.Wrappers;
|
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
|
|
|
|
{
|
2016-04-27 21:51:31 +00:00
|
|
|
|
public static class Log
|
2013-12-20 11:38:10 +00:00
|
|
|
|
{
|
2025-02-24 05:17:02 +00:00
|
|
|
|
public const string DirectoryName = Constant.Logs;
|
2016-05-20 20:16:25 +00:00
|
|
|
|
|
2020-02-21 21:12:58 +00:00
|
|
|
|
public static string CurrentLogDirectory { get; }
|
2020-01-08 23:13:56 +00:00
|
|
|
|
|
2016-04-27 21:51:31 +00:00
|
|
|
|
static Log()
|
|
|
|
|
|
{
|
2025-02-24 05:46:58 +00:00
|
|
|
|
CurrentLogDirectory = DataLocation.VersionLogDirectory;
|
2020-01-08 23:13:56 +00:00
|
|
|
|
if (!Directory.Exists(CurrentLogDirectory))
|
2016-04-27 21:51:31 +00:00
|
|
|
|
{
|
2020-01-08 23:13:56 +00:00
|
|
|
|
Directory.CreateDirectory(CurrentLogDirectory);
|
2016-04-27 21:51:31 +00:00
|
|
|
|
}
|
2013-12-20 11:38:10 +00:00
|
|
|
|
|
2016-04-27 21:51:31 +00:00
|
|
|
|
var configuration = new LoggingConfiguration();
|
2021-06-11 13:39:12 +00:00
|
|
|
|
|
2021-06-25 09:52:42 +00:00
|
|
|
|
const string layout =
|
|
|
|
|
|
@"${date:format=HH\:mm\:ss.ffffK} - " +
|
2021-06-26 10:11:36 +00:00
|
|
|
|
@"${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 13:39:12 +00:00
|
|
|
|
|
2021-06-11 04:44:48 +00:00
|
|
|
|
var fileTarget = new FileTarget
|
|
|
|
|
|
{
|
2021-06-11 13:39:12 +00:00
|
|
|
|
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);
|
2021-06-11 07:08:26 +00:00
|
|
|
|
|
2021-06-11 13:39:12 +00:00
|
|
|
|
var debugTarget = new OutputDebugStringTarget
|
2021-06-11 07:08:26 +00:00
|
|
|
|
{
|
2021-06-11 13:39:12 +00:00
|
|
|
|
Layout = layout
|
2021-06-11 07:08:26 +00:00
|
|
|
|
};
|
2021-06-11 13:39:12 +00:00
|
|
|
|
|
2021-06-11 04:40:07 +00:00
|
|
|
|
configuration.AddTarget("file", fileTargetASyncWrapper);
|
2021-06-11 13:39:12 +00:00
|
|
|
|
configuration.AddTarget("debug", debugTarget);
|
|
|
|
|
|
|
2024-05-29 15:48:04 +00:00
|
|
|
|
var fileRule = new LoggingRule("*", LogLevel.Debug, fileTargetASyncWrapper)
|
|
|
|
|
|
{
|
|
|
|
|
|
RuleName = "file"
|
|
|
|
|
|
};
|
2017-01-12 19:52:37 +00:00
|
|
|
|
#if DEBUG
|
2024-05-29 15:48:04 +00:00
|
|
|
|
var debugRule = new LoggingRule("*", LogLevel.Debug, debugTarget)
|
|
|
|
|
|
{
|
|
|
|
|
|
RuleName = "debug"
|
|
|
|
|
|
};
|
2021-06-11 13:39:12 +00:00
|
|
|
|
configuration.LoggingRules.Add(debugRule);
|
2017-01-12 19:52:37 +00:00
|
|
|
|
#endif
|
2021-06-11 07:08:26 +00:00
|
|
|
|
configuration.LoggingRules.Add(fileRule);
|
2016-04-27 21:51:31 +00:00
|
|
|
|
LogManager.Configuration = configuration;
|
|
|
|
|
|
}
|
2016-11-30 00:30:42 +00:00
|
|
|
|
|
2025-03-12 04:26:29 +00:00
|
|
|
|
public static void SetLogLevel(LOGLEVEL level)
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (level)
|
|
|
|
|
|
{
|
|
|
|
|
|
case LOGLEVEL.DEBUG:
|
|
|
|
|
|
UseDebugLogLevel();
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
UseInfoLogLevel();
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
Info(nameof(Logger), $"Using log level: {level}.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void UseDebugLogLevel()
|
2024-05-29 15:48:04 +00:00
|
|
|
|
{
|
|
|
|
|
|
LogManager.Configuration.FindRuleByName("file").SetLoggingLevels(LogLevel.Debug, LogLevel.Fatal);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-12 04:26:29 +00:00
|
|
|
|
private static void UseInfoLogLevel()
|
2024-05-29 15:48:04 +00:00
|
|
|
|
{
|
|
|
|
|
|
LogManager.Configuration.FindRuleByName("file").SetLoggingLevels(LogLevel.Info, LogLevel.Fatal);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-24 00:24:20 +00:00
|
|
|
|
private static void LogFaultyFormat(string message)
|
2016-04-27 21:51:31 +00:00
|
|
|
|
{
|
2017-01-24 00:24:20 +00:00
|
|
|
|
var logger = LogManager.GetLogger("FaultyLogger");
|
|
|
|
|
|
message = $"Wrong logger message format <{message}>";
|
|
|
|
|
|
logger.Fatal(message);
|
2016-04-27 21:51:31 +00:00
|
|
|
|
}
|
2016-05-15 16:03:06 +00:00
|
|
|
|
|
2019-12-29 23:13:33 +00:00
|
|
|
|
public static void Exception(string className, string message, System.Exception exception, [CallerMemberName] string methodName = "")
|
2021-06-11 07:08:26 +00:00
|
|
|
|
{
|
2021-06-11 04:40:07 +00:00
|
|
|
|
exception = exception.Demystify();
|
2021-01-06 09:42:46 +00:00
|
|
|
|
#if DEBUG
|
2021-06-11 04:40:07 +00:00
|
|
|
|
ExceptionDispatchInfo.Capture(exception).Throw();
|
2021-01-06 09:42:46 +00:00
|
|
|
|
#else
|
2020-02-21 21:12:58 +00:00
|
|
|
|
var classNameWithMethod = CheckClassAndMessageAndReturnFullClassWithMethod(className, message, methodName);
|
|
|
|
|
|
|
|
|
|
|
|
ExceptionInternal(classNameWithMethod, message, exception);
|
2021-01-06 09:42:46 +00:00
|
|
|
|
#endif
|
2020-02-21 21:12:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static string CheckClassAndMessageAndReturnFullClassWithMethod(string className, string message,
|
|
|
|
|
|
string methodName)
|
2019-12-29 23:13:33 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(className))
|
|
|
|
|
|
{
|
|
|
|
|
|
LogFaultyFormat($"Fail to specify a class name during logging of message: {message ?? "no message entered"}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(message))
|
2020-02-21 21:12:58 +00:00
|
|
|
|
{
|
|
|
|
|
|
// todo: not sure we really need that
|
2019-12-29 23:13:33 +00:00
|
|
|
|
LogFaultyFormat($"Fail to specify a message during logging");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(methodName))
|
|
|
|
|
|
{
|
2020-02-21 21:12:58 +00:00
|
|
|
|
return className + "." + methodName;
|
2019-12-29 23:13:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-21 21:12:58 +00:00
|
|
|
|
return className;
|
2019-12-29 23:13:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-13 10:13:55 +00:00
|
|
|
|
#if !DEBUG
|
2025-04-01 03:56:51 +00:00
|
|
|
|
private static void ExceptionInternal(string classAndMethod, string message, System.Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
var logger = LogManager.GetLogger(classAndMethod);
|
|
|
|
|
|
|
|
|
|
|
|
logger.Error(e, message);
|
|
|
|
|
|
}
|
2017-01-24 00:24:20 +00:00
|
|
|
|
#endif
|
2017-01-25 00:24:00 +00:00
|
|
|
|
|
2020-02-21 21:12:58 +00:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void Info(string className, string message, [CallerMemberName] string methodName = "")
|
|
|
|
|
|
{
|
|
|
|
|
|
LogInternal(LogLevel.Info, className, message, methodName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void Warn(string className, string message, [CallerMemberName] string methodName = "")
|
|
|
|
|
|
{
|
|
|
|
|
|
LogInternal(LogLevel.Warn, className, message, methodName);
|
|
|
|
|
|
}
|
2013-12-20 11:38:10 +00:00
|
|
|
|
}
|
2025-03-12 04:01:39 +00:00
|
|
|
|
|
|
|
|
|
|
public enum LOGLEVEL
|
|
|
|
|
|
{
|
|
|
|
|
|
DEBUG,
|
|
|
|
|
|
INFO
|
|
|
|
|
|
}
|
2022-01-05 07:32:40 +00:00
|
|
|
|
}
|