2022-05-22 15:55:26 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
|
|
namespace RageCoop.Core
|
|
|
|
|
{
|
|
|
|
|
public class Loggger
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public string LogPath;
|
|
|
|
|
private StreamWriter logWriter;
|
|
|
|
|
private bool UseConsole=false;
|
2022-05-23 09:15:50 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 0:Trace, 1:Debug, 2:Info, 3:Warning, 4:Error
|
|
|
|
|
/// </summary>
|
2022-05-22 15:55:26 +08:00
|
|
|
|
public int LogLevel = 0;
|
|
|
|
|
private string Buffer="";
|
|
|
|
|
|
2022-05-23 09:15:50 +08:00
|
|
|
|
public Loggger(string path,bool overwrite=true)
|
2022-05-22 15:55:26 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LogPath=path;
|
2022-05-23 09:15:50 +08:00
|
|
|
|
if (File.Exists(path)&&overwrite) { File.Delete(path); }
|
2022-05-22 15:55:26 +08:00
|
|
|
|
Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
Flush();
|
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public Loggger()
|
|
|
|
|
{
|
|
|
|
|
UseConsole=true;
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
Flush();
|
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Info(string message)
|
|
|
|
|
{
|
|
|
|
|
if (LogLevel>2) { return; }
|
|
|
|
|
lock (Buffer)
|
|
|
|
|
{
|
|
|
|
|
string msg = string.Format("[{0}][{2}] [INF] {1}", Date(), message, Process.GetCurrentProcess().Id);
|
|
|
|
|
|
|
|
|
|
Buffer+=msg+"\r\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Warning(string message)
|
|
|
|
|
{
|
|
|
|
|
if (LogLevel>3) { return; }
|
|
|
|
|
lock (Buffer)
|
|
|
|
|
{
|
|
|
|
|
string msg = string.Format("[{0}][{2}] [WRN] {1}", Date(), message, Process.GetCurrentProcess().Id);
|
|
|
|
|
|
|
|
|
|
Buffer+=msg+"\r\n";
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Error(string message)
|
|
|
|
|
{
|
|
|
|
|
if (LogLevel>4) { return; }
|
|
|
|
|
lock (Buffer)
|
|
|
|
|
{
|
|
|
|
|
string msg = string.Format("[{0}][{2}] [ERR] {1}", Date(), message, Process.GetCurrentProcess().Id);
|
|
|
|
|
|
|
|
|
|
Buffer+=msg+"\r\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public void Error(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
if (LogLevel>4) { return; }
|
|
|
|
|
lock (Buffer)
|
|
|
|
|
{
|
|
|
|
|
string msg = string.Format("[{0}][{2}] [ERR] {1}", Date(),string.Join("\r\n",ex.Message,ex.StackTrace,ex.ToString()), Process.GetCurrentProcess().Id);
|
|
|
|
|
|
|
|
|
|
Buffer+=msg+"\r\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Debug(string message)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (LogLevel>1) { return; }
|
|
|
|
|
lock (Buffer)
|
|
|
|
|
{
|
|
|
|
|
string msg = string.Format("[{0}][{2}] [DBG] {1}", Date(), message,Process.GetCurrentProcess().Id);
|
|
|
|
|
|
|
|
|
|
Buffer+=msg+"\r\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Trace(string message)
|
|
|
|
|
{
|
2022-05-23 09:15:50 +08:00
|
|
|
|
if (LogLevel>0) { return; }
|
2022-05-22 15:55:26 +08:00
|
|
|
|
lock (Buffer)
|
|
|
|
|
{
|
|
|
|
|
string msg = string.Format("[{0}][{2}] [TRC] {1}", Date(), message, Process.GetCurrentProcess().Id);
|
|
|
|
|
|
|
|
|
|
Buffer+=msg+"\r\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string Date()
|
|
|
|
|
{
|
|
|
|
|
return DateTime.Now.ToString();
|
|
|
|
|
}
|
|
|
|
|
public void Flush()
|
|
|
|
|
{
|
|
|
|
|
lock (Buffer)
|
|
|
|
|
{
|
|
|
|
|
if (Buffer!="")
|
|
|
|
|
{
|
2022-05-23 09:15:50 +08:00
|
|
|
|
if (UseConsole)
|
2022-05-22 15:55:26 +08:00
|
|
|
|
{
|
2022-05-23 09:15:50 +08:00
|
|
|
|
Console.Write(Buffer);
|
2022-05-22 15:55:26 +08:00
|
|
|
|
Buffer="";
|
|
|
|
|
}
|
2022-05-23 09:15:50 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
logWriter=new StreamWriter(LogPath, true, Encoding.UTF8);
|
|
|
|
|
logWriter.Write(Buffer);
|
|
|
|
|
logWriter.Close();
|
|
|
|
|
Buffer="";
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
|
|
|
|
}
|
2022-05-22 15:55:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|