Código:
using System;
using System.IO;
namespace AikaProject.Login.Logger
{
public static class Logger
{
private static string name = "logs/AikaLogin/" + DateTime.Now.ToString("yyyy-MM-dd--HH-mm-ss") + ".log";
private static object Sync = new object();
public static void Error(string text)
{
lock (Sync)
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}]");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(" [ ERROR ] ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(text);
Console.WriteLine();
Save(text);
}
}
public static void Aviso(string text)
{
{
lock (Sync)
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}]");
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write(" [ AVISO ] ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(text);
Console.WriteLine();
Save(text);
}
}
}
public static void Connect(string text)
{
lock (Sync)
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}]");
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.Write(" [ CONEXÃO ] ");
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write(text);
Console.WriteLine();
Save(text);
}
}
public static void Info(string text)
{
lock (Sync)
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}]");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write(" [ INFO ] ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(text);
Console.WriteLine();
Save(text);
}
}
public static void Server(string text)
{
{
lock (Sync)
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}]");
Console.ForegroundColor = ConsoleColor.DarkMagenta;
Console.Write(" [ SERVER ] ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(text);
Console.WriteLine();
Save(text);
}
}
}
private static void Save(string text)
{
using StreamWriter stream = new StreamWriter(name, true);
try
{
if (stream != null)
stream.WriteLine(text);
}
catch { }
finally
{
stream.Dispose();
//stream.Flush();
stream.Close();
}
}
public static void CheckDirectory()
{
if (!Directory.Exists("logs/AikaLogin"))
Directory.CreateDirectory("logs/AikaLogin");
}
}
}