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

176 lines
5.7 KiB
C#
Raw Normal View History

2022-11-02 17:17:25 +00:00
using System.Diagnostics;
using System.IO;
2016-11-30 00:30:42 +00:00
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using Flow.Launcher.Infrastructure.UserSettings;
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
{
public static class Log
2013-12-20 11:38:10 +00:00
{
public const string DirectoryName = Constant.Logs;
public static string CurrentLogDirectory { get; }
2020-01-08 23:13:56 +00:00
static Log()
{
CurrentLogDirectory = DataLocation.VersionLogDirectory;
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);
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"
};
configuration.LoggingRules.Add(debugRule);
2017-01-12 19:52:37 +00:00
#endif
configuration.LoggingRules.Add(fileRule);
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)
{
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
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;
}
#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
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
}
public enum LOGLEVEL
{
DEBUG,
INFO
}
2022-01-05 07:32:40 +00:00
}