AStar/Logging/Logger.cs

47 lines
1.5 KiB
C#
Raw Normal View History

2022-05-05 14:46:04 +02:00
namespace Logging
2022-05-05 02:01:05 +02:00
{
public class Logger
{
private LogType logType;
private string logfilepath;
2022-05-05 16:00:56 +02:00
private loglevel level;
public Logger(LogType type, loglevel level)
2022-05-05 02:01:05 +02:00
{
this.logType = type;
this.logfilepath = "";
2022-05-05 16:00:56 +02:00
this.level = level;
2022-05-05 02:01:05 +02:00
}
2022-05-05 16:00:56 +02:00
public Logger(LogType type, loglevel level, string path)
2022-05-05 02:01:05 +02:00
{
this.logType = type;
this.logfilepath = path;
2022-05-05 16:00:56 +02:00
this.level = level;
2022-05-05 02:01:05 +02:00
}
2022-05-06 00:01:23 +02:00
public void Log(loglevel type, string message, params object[] ?replace)
2022-05-05 02:01:05 +02:00
{
2022-05-06 00:01:23 +02:00
if(type <= this.level)
2022-05-05 02:01:05 +02:00
{
2022-05-06 00:32:45 +02:00
string header = string.Format("{0} {1} {2}: ", DateTime.Now.ToLocalTime().ToShortDateString(), DateTime.Now.ToLocalTime().ToLongTimeString(), type.ToString()[0]);
2022-05-05 16:00:56 +02:00
switch (this.logType)
{
case LogType.Console:
2022-05-05 16:32:45 +02:00
#pragma warning disable CS8604
2022-05-05 16:00:56 +02:00
Console.WriteLine(string.Format(header + message, replace));
2022-05-05 16:32:45 +02:00
#pragma warning restore CS8604
2022-05-05 16:00:56 +02:00
break;
case LogType.Logfile:
2022-05-05 16:32:45 +02:00
#pragma warning disable CS8604
2022-05-05 16:00:56 +02:00
File.WriteAllText(this.logfilepath, string.Format(header + message, replace));
2022-05-05 16:32:45 +02:00
#pragma warning restore CS8604
2022-05-05 16:00:56 +02:00
break;
}
2022-05-05 02:01:05 +02:00
}
}
}
public enum LogType { Console, Logfile }
2022-05-06 00:01:23 +02:00
public enum loglevel : ushort { INFO = 0, DEBUG = 1, ERROR = 2, VERBOSE = 3 };
2022-05-05 02:01:05 +02:00
}