Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- namespace LoggerSharp
- {
- public class LoggerCore
- {
- private string logFilePath = "";
- private bool allowsDebug = false;
- private readonly string time = new DateTime().ToString("HH:mm:ss");
- private LogFile LF = null;
- /// <summary>
- /// Creates a new instance of the LoggerCore class, while setting all values if any are present.
- /// </summary>
- /// <param name="path">The path to the log file.</param>
- /// <param name="debug">The value that tells the logger weather it will allow debug output.</param>
- public LoggerCore(string path = "", bool debug = false)
- {
- logFilePath = path;
- allowsDebug = debug;
- }
- /// <summary>
- /// Prints null or blank output to the console.
- /// </summary>
- public void Log()
- {
- Log(" ");
- }
- /// <summary>
- /// Prints the specified data to the console, without formatting.
- /// </summary>
- /// <param name="data">The RAW data to print.</param>
- public void Log(string data)
- {
- Console.WriteLine(data);
- }
- /// <summary>
- /// Prints a formatted based log to the console.
- /// </summary>
- /// <param name="name">The name of the application that sent the data.</param>
- /// <param name="status">The status of the log.</param>
- /// <param name="message">The message to display in the log.</param>
- public void Log(string name, int status, string message)
- {
- if (LF == null) LF = new LogFile(logFilePath, name);
- if (name == null || name == "")
- {
- var x = $"[{time}]{GetStatusTag(status)} {message}";
- if ((allowsDebug) && (status == -1 || status == -2 || status == -3 || status == -4))
- {
- Log(x);
- if (LF != null) LF.Write(x);
- }
- else
- {
- Log(x);
- if (LF != null) LF.Write(x);
- }
- }
- else
- {
- var x = $"[{time}][{name}]{GetStatusTag(status)} {message}";
- if ((allowsDebug) && (status == -1 || status == -2 || status == -3 || status == -4))
- {
- Log(x);
- if (LF != null) LF.Write(x);
- }
- else
- {
- Log(x);
- if (LF != null) LF.Write(x);
- }
- }
- }
- /// <summary>
- /// Prints a formatted list-based log to the console.
- /// </summary>
- /// <param name="name">The name of the application that sent the data.</param>
- /// <param name="status">The status of the log.</param>
- /// <param name="message">The message to display in the log.</param>
- /// <param name="data">The data to sort then display in the log.</param>
- public void Log(string name, int status, string message, List<string> data)
- {
- string x = "";
- foreach (string s in data)
- {
- if (x == "" || x == null) x = " - " + s;
- else x += "\n - " + s;
- }
- Log(name, status, $"{message}\n{x}");
- }
- private string GetStatusTag(int status)
- {
- string output = "";
- switch (status)
- {
- case (-4): { output = "[DEBUG][SEVERE]"; break; }
- case (-3): { output = "[DEBUG][ERROR]"; break; }
- case (-2): { output = "[DEBUG][WARNING]"; break; }
- case (-1): { output = "[DEBUG][INFO]"; break; }
- case (0): { output = "[INFO]"; break; }
- case (1): { output = "[WARNING]"; break; }
- case (2): { output = "[ERROR]"; break; }
- case (3): { output = "[SERVER]"; break; }
- default: { output = "[INFO]"; break; }
- }
- return output;
- }
- }
- /// <summary>
- /// Contains methods used to create a logfile.
- /// </summary>
- public class LogFile
- {
- private string clientName = "";
- private string logFilePath = "";
- private readonly string timestamp = "";
- /// <summary>
- /// Creates a new instance of LogFile, and sets all present settings.
- /// </summary>
- /// <param name="path">The path of the logfile.</param>
- /// <param name="name">The name to output on an error.</param>
- public LogFile(string path, string name = "")
- {
- if (!(logFilePath == null || logFilePath == ""))
- {
- timestamp = DateTime.Now.ToString("HH_mm_ss(dd-MM-yyyy)");
- logFilePath = $"{path}\\Logs\\{timestamp}.log";
- clientName = name;
- try
- {
- if (Directory.Exists(Path.GetDirectoryName(logFilePath)))
- {
- //Check For File
- try
- {
- if (!(File.Exists(logFilePath))) File.Create(logFilePath);
- }
- catch (Exception ex)
- {
- LogData($"Could not create log file! -> {ex.ToString()}");
- }
- }
- else
- {
- //Create DIR then check for file.
- Directory.CreateDirectory(Path.GetDirectoryName(logFilePath));
- try
- {
- if (!(File.Exists(logFilePath))) File.Create(logFilePath);
- }
- catch (Exception ex)
- {
- LogData($"Could not create log file! -> {ex.ToString()}");
- }
- }
- }
- catch (Exception ex)
- {
- LogData($"Could not create log file directory! -> {ex.ToString()}");
- }
- }
- else return;
- }
- private StreamWriter writer = null;
- /// <summary>
- /// Writes the specified data to the pre-created log file.
- /// </summary>
- /// <param name="data">The data to output.</param>
- public void Write(string data)
- {
- if (writer == null) writer = new StreamWriter(logFilePath, true);
- writer.WriteLine(data);
- writer.Flush();
- writer.Close();
- }
- private LoggerCore logger = null;
- private void LogData(string data)
- {
- if (logger == null) logger = new LoggerCore();
- if (clientName == null || clientName == "")
- {
- var x = $"[LOGFILE-GENERATOR]: >>> {data}";
- logger.Log(x);
- }
- else
- {
- var x = $"[{clientName.ToUpper()}//LOGFILE-GENERATOR]: >>> {data}";
- logger.Log(x);
- }
- }
- }
- /// <summary>
- /// Contains simple methods for logging to the console.
- /// </summary>
- public class Logger
- {
- private string clientName = "";
- private LoggerCore loggerCore = null;
- /// <summary>
- /// Creates a new instance of the Logger class, and sets all settings if present.
- /// </summary>
- /// <param name="name">The name of the application that invoked the logger.</param>
- /// <param name="logFilePath">The path of the log file.</param>
- /// <param name="debug">The value that tells the logger weather it should use default output.</param>
- public Logger(string name = "", string logFilePath = "", bool debug = false)
- {
- clientName = name;
- if (loggerCore == null) loggerCore = new LoggerCore(logFilePath, debug);
- }
- /// <summary>
- /// Logs a blank log to the console.
- /// </summary>
- public void Log()
- {
- loggerCore.Log();
- }
- /// <summary>
- /// Logs a formatted log to the console.
- /// </summary>
- /// <param name="id">The ID of the status to display in the log.</param>
- /// <param name="message">The message to display in the log.</param>
- public void Log(StatusIDS id, string message)
- {
- loggerCore.Log(clientName, (int)id, message);
- }
- /// <summary>
- /// Logs a list in a formatted log.
- /// </summary>
- /// <param name="id">The ID of the status to display in the log.</param>
- /// <param name="message">The message to display in the log.</param>
- /// <param name="data">The list to sort and log.</param>
- public void Log(StatusIDS id, string message, List<string>data)
- {
- loggerCore.Log(clientName, (int)id, message, data);
- }
- public enum StatusIDS : int
- {
- DEBUG_SEVERE = -4,
- DEBUG_ERROR = -3,
- DEBUG_WARNING = -2,
- DEBUG_INFO = -1,
- INFO = -0,
- WARNING = 1,
- ERROR = 2,
- SEVERE = 3
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement