Add file logging to Logger.
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -36,6 +36,9 @@ msbuild.wrn
|
|||||||
# Visual Studio 2015
|
# Visual Studio 2015
|
||||||
.vs/
|
.vs/
|
||||||
|
|
||||||
|
# Dagger log files
|
||||||
|
*.log
|
||||||
|
|
||||||
|
|
||||||
# FMOD binaries (I cannot distribute them)
|
# FMOD binaries (I cannot distribute them)
|
||||||
DaggerFramework.Fmod/*.nupkg
|
DaggerFramework.Fmod/*.nupkg
|
||||||
|
|||||||
@@ -13,11 +13,24 @@ namespace DaggerFramework.Utils
|
|||||||
/// Specifies the logging level. In release builds, the log level is <c>Error</c>. In debug, the log level is <c>Echo</c>.
|
/// Specifies the logging level. In release builds, the log level is <c>Error</c>. In debug, the log level is <c>Echo</c>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static LogLevel LogLevel = LogLevel.Error;
|
public static LogLevel LogLevel = LogLevel.Error;
|
||||||
|
/// <summary>
|
||||||
|
/// Specifies if the logger should write to file.
|
||||||
|
/// </summary>
|
||||||
|
public static bool WriteToFile = true;
|
||||||
|
|
||||||
public Logger(string className)
|
public Logger(string className)
|
||||||
{
|
{
|
||||||
_className = className;
|
_className = className;
|
||||||
|
|
||||||
|
if (WriteToFile && !_logCreated)
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(LogPath);
|
||||||
|
string logName = $"dagger-{DateFormat}-{TimeFormat}.log".Replace(':', '.');
|
||||||
|
_fileStream = File.Create(Path.Combine(LogPath, logName));
|
||||||
|
_fileWriter = new StreamWriter(_fileStream);
|
||||||
|
_logCreated = true;
|
||||||
|
}
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
LogLevel = LogLevel.Echo;
|
LogLevel = LogLevel.Echo;
|
||||||
#endif
|
#endif
|
||||||
@@ -25,54 +38,52 @@ namespace DaggerFramework.Utils
|
|||||||
|
|
||||||
public void Echo(string what)
|
public void Echo(string what)
|
||||||
{
|
{
|
||||||
if (LogLevel < LogLevel.Echo) return;
|
string message = string.Format(EchoFormat, TimeFormat, what);
|
||||||
|
|
||||||
string message = string.Format(EchoFormat, DateFormat, what);
|
|
||||||
|
|
||||||
|
LogFile(message);
|
||||||
LogConsole(what, LogLevel.Echo);
|
LogConsole(what, LogLevel.Echo);
|
||||||
OnLog?.Invoke(what, LogLevel.Echo);
|
OnLog?.Invoke(what, LogLevel.Echo);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Info(string what, [CallerMemberName] string method = "")
|
public void Info(string what, [CallerMemberName] string method = "")
|
||||||
{
|
{
|
||||||
if (LogLevel < LogLevel.Info) return;
|
|
||||||
|
|
||||||
LogLevel logType = LogLevel.Info;
|
LogLevel logType = LogLevel.Info;
|
||||||
string message = string.Format(LogFormat, DateFormat, logType.ToString(), _className, method, what);
|
string message = string.Format(LogFormat, TimeFormat, logType.ToString(), _className, method, what);
|
||||||
|
|
||||||
|
LogFile(message);
|
||||||
LogConsole(message, logType);
|
LogConsole(message, logType);
|
||||||
OnLog?.Invoke(message, logType);
|
OnLog?.Invoke(message, logType);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Warn(string what, [CallerMemberName] string method = "")
|
public void Warn(string what, [CallerMemberName] string method = "")
|
||||||
{
|
{
|
||||||
if (LogLevel < LogLevel.Warn) return;
|
|
||||||
|
|
||||||
LogLevel logType = LogLevel.Warn;
|
LogLevel logType = LogLevel.Warn;
|
||||||
string message = string.Format(LogFormat, DateFormat, logType.ToString(), _className, method, what);
|
string message = string.Format(LogFormat, TimeFormat, logType.ToString(), _className, method, what);
|
||||||
|
|
||||||
|
LogFile(message);
|
||||||
LogConsole(message, logType);
|
LogConsole(message, logType);
|
||||||
OnLog?.Invoke(message, logType);
|
OnLog?.Invoke(message, logType);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Error(string what, [CallerMemberName] string method = "")
|
public void Error(string what, [CallerMemberName] string method = "")
|
||||||
{
|
{
|
||||||
if (LogLevel < LogLevel.Error) return;
|
|
||||||
|
|
||||||
LogLevel logType = LogLevel.Error;
|
LogLevel logType = LogLevel.Error;
|
||||||
string message = string.Format(LogFormat, DateFormat, logType.ToString(), _className, method, what);
|
string message = string.Format(LogFormat, TimeFormat, logType.ToString(), _className, method, what);
|
||||||
|
|
||||||
|
LogFile(message);
|
||||||
LogConsole(message, logType);
|
LogConsole(message, logType);
|
||||||
OnLog?.Invoke(message, logType);
|
OnLog?.Invoke(message, logType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static string DateFormat => $"{DateTime.Now:HH:mm:ffff}";
|
private static string TimeFormat => $"{DateTime.Now:HH:mm:ffff}";
|
||||||
|
private static string DateFormat => $"{DateTime.Now:d:M:yyyy:}";
|
||||||
private static string LogFormat => "({0}) [{1}/{2}/{3}] {4}";
|
private static string LogFormat => "({0}) [{1}/{2}/{3}] {4}";
|
||||||
private static string EchoFormat => "({0}) {1}";
|
private static string EchoFormat => "({0}) {1}";
|
||||||
|
|
||||||
private static void LogConsole(string what, LogLevel logType)
|
private static void LogConsole(string what, LogLevel logType)
|
||||||
{
|
{
|
||||||
|
if (LogLevel < logType) return;
|
||||||
Console.ForegroundColor = ConsoleColorForLog(logType);
|
Console.ForegroundColor = ConsoleColorForLog(logType);
|
||||||
Console.WriteLine(what);
|
Console.WriteLine(what);
|
||||||
Console.ForegroundColor = ConsoleColor.White;
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
@@ -99,7 +110,17 @@ namespace DaggerFramework.Utils
|
|||||||
return color;
|
return color;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void LogFile(string message)
|
||||||
|
{
|
||||||
|
if (!WriteToFile || _fileWriter is null) return;
|
||||||
|
_fileWriter.WriteLine(message);
|
||||||
|
_fileWriter.Flush();
|
||||||
|
}
|
||||||
|
|
||||||
private readonly string _className;
|
private readonly string _className;
|
||||||
|
private static bool _logCreated;
|
||||||
|
private static FileStream? _fileStream;
|
||||||
|
private static StreamWriter? _fileWriter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum LogLevel
|
public enum LogLevel
|
||||||
|
|||||||
Reference in New Issue
Block a user