267 lines
8.4 KiB
C#
267 lines
8.4 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Text.Json;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using ConsoleTemplate.Contexts;
|
|||
|
using Microsoft.Extensions.Configuration;
|
|||
|
|
|||
|
namespace ConsoleTemplate.Services
|
|||
|
{
|
|||
|
internal class LogService
|
|||
|
{
|
|||
|
private LogType[] _logtype;
|
|||
|
private IDBContext _db;
|
|||
|
public LogService(IConfiguration config, IDBContext dbcontext)
|
|||
|
{
|
|||
|
_db = dbcontext;
|
|||
|
_logtype = config.GetSection("Logger").Get<LogType[]>();
|
|||
|
}
|
|||
|
|
|||
|
public LogService(LogType[] logtype)
|
|||
|
{
|
|||
|
_logtype = logtype;
|
|||
|
}
|
|||
|
|
|||
|
public void Debug(string s, string trace = "", string cmd = "")
|
|||
|
{
|
|||
|
WriteLog("Debug", s, "", trace, cmd);
|
|||
|
}
|
|||
|
|
|||
|
public void Debug(string s, string toko, string trace = "", string cmd = "")
|
|||
|
{
|
|||
|
WriteLog("Debug", s, toko, trace, cmd);
|
|||
|
}
|
|||
|
|
|||
|
public void Info(string s, string trace = "", string cmd = "")
|
|||
|
{
|
|||
|
WriteLog("Info", s, "", trace, cmd);
|
|||
|
}
|
|||
|
|
|||
|
public void Info(string s, string toko, string trace = "", string cmd = "")
|
|||
|
{
|
|||
|
WriteLog("Info", s, toko, trace, cmd);
|
|||
|
}
|
|||
|
|
|||
|
public void Warning(string s, string trace = "", string cmd = "")
|
|||
|
{
|
|||
|
WriteLog("Warn", s, "", trace, cmd);
|
|||
|
}
|
|||
|
|
|||
|
public void Warning(string s, string toko, string trace = "", string cmd = "")
|
|||
|
{
|
|||
|
WriteLog("Warn", s, toko, trace, cmd);
|
|||
|
}
|
|||
|
|
|||
|
public void Error(string s, string trace = "", string cmd = "")
|
|||
|
{
|
|||
|
WriteLog("Error", s, "", trace, cmd);
|
|||
|
}
|
|||
|
|
|||
|
public void Error(string s, string toko, string trace = "", string cmd = "")
|
|||
|
{
|
|||
|
WriteLog("Error", s, toko, trace, cmd);
|
|||
|
}
|
|||
|
|
|||
|
private void WriteLog(string tipe, string s, string toko, string trace = "", string cmd = "")
|
|||
|
{
|
|||
|
foreach (LogType l in _logtype)
|
|||
|
{
|
|||
|
if (
|
|||
|
(l.MinimumLevel.ToLower() == "error" && tipe.ToLower() == "error") ||
|
|||
|
(l.MinimumLevel.ToLower() == "warn" && (tipe.ToLower() == "warn" || tipe.ToLower() == "warn")) ||
|
|||
|
(l.MinimumLevel.ToLower() == "info" && tipe.ToLower() != "debug") ||
|
|||
|
(l.MinimumLevel.ToLower() == "debug")
|
|||
|
)
|
|||
|
{
|
|||
|
switch (l.Name.ToLower())
|
|||
|
{
|
|||
|
case "console":
|
|||
|
ConsoleLog(s, toko, tipe, trace);
|
|||
|
break;
|
|||
|
|
|||
|
case "text":
|
|||
|
TextLog(l.Path, s, toko, tipe, trace, cmd);
|
|||
|
break;
|
|||
|
|
|||
|
case "json":
|
|||
|
JsonLog(l.Path, s, toko, tipe, trace, cmd);
|
|||
|
break;
|
|||
|
|
|||
|
default:
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private static void ConsoleLog(string s, string toko, string tipe, string trace = "")
|
|||
|
{
|
|||
|
Console.Write($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} ");
|
|||
|
if (tipe.ToLower() == "info")
|
|||
|
{
|
|||
|
Console.BackgroundColor = ConsoleColor.Green;
|
|||
|
Console.ForegroundColor = ConsoleColor.White;
|
|||
|
Console.Write($"[INFO ]");
|
|||
|
Console.ResetColor();
|
|||
|
if (toko == "")
|
|||
|
Console.WriteLine($" {s}");
|
|||
|
else
|
|||
|
Console.WriteLine($" ({toko}) {s}");
|
|||
|
}
|
|||
|
else if (tipe.ToLower() == "warn")
|
|||
|
{
|
|||
|
Console.BackgroundColor = ConsoleColor.Yellow;
|
|||
|
Console.ForegroundColor = ConsoleColor.Black;
|
|||
|
Console.Write($"[WARN ]");
|
|||
|
Console.ResetColor();
|
|||
|
if (toko == "")
|
|||
|
Console.WriteLine($" {s}");
|
|||
|
else
|
|||
|
Console.WriteLine($" ({toko}) {s}");
|
|||
|
}
|
|||
|
else if (tipe.ToLower() == "error")
|
|||
|
{
|
|||
|
Console.BackgroundColor = ConsoleColor.Red;
|
|||
|
Console.ForegroundColor = ConsoleColor.White;
|
|||
|
Console.Write($"[ERROR]");
|
|||
|
Console.ResetColor();
|
|||
|
if (toko == "")
|
|||
|
Console.WriteLine($" {s}");
|
|||
|
else
|
|||
|
Console.WriteLine($" ({toko}) {s}");
|
|||
|
if (trace != "")
|
|||
|
Console.WriteLine(Environment.NewLine + trace);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Console.WriteLine($"[DEBUG] {s}");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private static void TextLog(string path, string s, string toko, string level, string trace = "", string cmd = "")
|
|||
|
{
|
|||
|
if (path is null)
|
|||
|
{
|
|||
|
path = Environment.CurrentDirectory + "\\logs";
|
|||
|
}
|
|||
|
|
|||
|
string filename;
|
|||
|
if (toko == "")
|
|||
|
{
|
|||
|
filename = @$"{path}\log-{DateTime.Now:MMM-yyyy}.log";
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
filename = @$"{path}\toko\{toko} log-{DateTime.Now:MMM-yyyy}.log";
|
|||
|
}
|
|||
|
|
|||
|
bool written = false;
|
|||
|
|
|||
|
Directory.CreateDirectory(path);
|
|||
|
Directory.CreateDirectory(path + "\\toko");
|
|||
|
|
|||
|
s = System.Text.RegularExpressions.Regex.Replace(s, @"\p{C}+", ">").Replace(Environment.NewLine, ">");
|
|||
|
trace = System.Text.RegularExpressions.Regex.Replace(trace, @"\p{C}+", ">").Replace(Environment.NewLine, ">");
|
|||
|
cmd = System.Text.RegularExpressions.Regex.Replace(cmd, @"\p{C}+", ">").Replace(Environment.NewLine, ">");
|
|||
|
|
|||
|
level = level.PadRight(5).ToUpper();
|
|||
|
|
|||
|
while (!written)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
using (StreamWriter sw = System.IO.File.AppendText(filename))
|
|||
|
{
|
|||
|
sw.WriteLine(@$"{DateTime.Now:dd-MM-yy HH:mm:ss}|[{level}]|{s}|{trace}|{cmd}");
|
|||
|
}
|
|||
|
written = true;
|
|||
|
}
|
|||
|
catch (Exception)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private static void DBLog(string s, string toko, string level, string trace = "", string cmd = "")
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
private static void JsonLog(string path, string s, string toko, string level, string trace = "", string cmd = "")
|
|||
|
{
|
|||
|
if (path is null)
|
|||
|
{
|
|||
|
path = Environment.CurrentDirectory + "\\logs";
|
|||
|
}
|
|||
|
|
|||
|
string filename;
|
|||
|
if (toko == "")
|
|||
|
{
|
|||
|
filename = @$"{path}\log-{DateTime.Now:MMM-yyyy}.json";
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
filename = @$"{path}\toko\{toko} log-{DateTime.Now:MMM-yyyy}.json";
|
|||
|
}
|
|||
|
bool written = false;
|
|||
|
|
|||
|
Directory.CreateDirectory(path);
|
|||
|
Directory.CreateDirectory(path + "\\toko");
|
|||
|
|
|||
|
while (!written)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
List<JsonLog> loglist;
|
|||
|
if (File.Exists(filename))
|
|||
|
{
|
|||
|
string jsondata = System.IO.File.ReadAllText(filename);
|
|||
|
loglist = JsonSerializer.Deserialize<List<JsonLog>>(jsondata);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
loglist = new List<JsonLog>();
|
|||
|
}
|
|||
|
|
|||
|
loglist.Add(new JsonLog()
|
|||
|
{
|
|||
|
Time = DateTime.Now,
|
|||
|
Mesage = s,
|
|||
|
Toko = toko,
|
|||
|
Level = level,
|
|||
|
Trace = trace,
|
|||
|
Cmd = cmd,
|
|||
|
});
|
|||
|
File.WriteAllText(filename, JsonSerializer.Serialize(loglist));
|
|||
|
written = true;
|
|||
|
}
|
|||
|
catch (Exception)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
class LogType
|
|||
|
{
|
|||
|
public string Name { get; set; }
|
|||
|
public string MinimumLevel { get; set; }
|
|||
|
public string Path { get; set; }
|
|||
|
}
|
|||
|
|
|||
|
class JsonLog
|
|||
|
{
|
|||
|
public DateTime Time { get; set; }
|
|||
|
public string Toko { get; set; }
|
|||
|
public string Level { get; set; }
|
|||
|
public string Mesage { get; set; }
|
|||
|
public string Trace { get; set; }
|
|||
|
public string Cmd { get; set; }
|
|||
|
}
|
|||
|
}
|