IW4M-Admin/WebfrontCore/Application/Logger.cs

84 lines
1.9 KiB
C#
Raw Normal View History

2017-05-27 19:29:20 -04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace IW4MAdmin
{
class Logger : SharedLibrary.Interfaces.ILogger
{
enum LogType
{
Verbose,
Info,
Debug,
Warning,
2017-11-13 16:58:23 -05:00
Error,
Assert
2017-05-27 19:29:20 -04:00
}
string FileName;
object ThreadLock;
2017-05-27 19:29:20 -04:00
public Logger(string fn)
{
FileName = fn;
ThreadLock = new object();
if (File.Exists(fn))
File.Delete(fn);
2017-05-27 19:29:20 -04:00
}
void Write(string msg, LogType type)
{
string LogLine = $"[{DateTime.Now.ToString("HH:mm:ss")}] - {type}: {msg}";
lock (ThreadLock)
{
2017-05-27 19:29:20 -04:00
#if DEBUG
// lets keep it simple and dispose of everything quickly as logging wont be that much (relatively)
2017-05-27 19:29:20 -04:00
Console.WriteLine(LogLine);
File.AppendAllText(FileName, LogLine + Environment.NewLine);
2017-05-27 19:29:20 -04:00
#else
if (type == LogType.Error || type == LogType.Verbose)
Console.WriteLine(LogLine);
//if (type != LogType.Debug)
File.AppendAllText(FileName, LogLine + Environment.NewLine);
2017-05-27 19:29:20 -04:00
#endif
2017-06-05 22:49:26 -04:00
}
2017-05-27 19:29:20 -04:00
}
public void WriteVerbose(string msg)
{
Write(msg, LogType.Verbose);
}
public void WriteDebug(string msg)
{
Write(msg, LogType.Debug);
}
public void WriteError(string msg)
{
Write(msg, LogType.Error);
}
public void WriteInfo(string msg)
{
Write(msg, LogType.Info);
}
public void WriteWarning(string msg)
{
Write(msg, LogType.Warning);
}
2017-11-13 16:58:23 -05:00
public void WriteAssert(bool condition, string msg)
{
if (!condition)
Write(msg, LogType.Assert);
}
2017-05-27 19:29:20 -04:00
}
}