using System;
using System.Diagnostics;
using System.IO;
namespace Netch.Tools
{
public class Logger
{
///
/// 互斥锁
///
private object mutex = new();
///
/// 写入日志
///
///
///
private void WriteLine(string name, string text)
{
var method = new StackTrace().GetFrame(2).GetMethod();
var content = $"[{DateTime.Now}][{method.ReflectedType.Name}.{method.Name}][{name}] {text}";
lock (mutex)
{
File.AppendAllText(this.SavePath, $"{content}\n");
}
Console.WriteLine($"[Netch]{content}");
}
///
/// 保存路径
///
public string SavePath;
///
/// 调试
///
///
public void Debug(string text)
{
this.WriteLine("DEBUG", text);
}
///
/// 信息
///
///
public void Info(string text)
{
this.WriteLine("INFO", text);
}
///
/// 警告
///
///
public void Warning(string text)
{
this.WriteLine("WARNING", text);
}
///
/// 错误
///
///
public void Error(string text)
{
this.WriteLine("ERROR", text);
}
///
/// 崩溃
///
///
public void Fatal(string text)
{
this.WriteLine("FATAL", text);
Environment.Exit(1);
}
}
}