Advertisement
Pasture101

LoggerSharp C# Logger

Oct 16th, 2017
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace LoggerSharp
  8. {
  9.     public class LoggerCore
  10.     {
  11.         private string logFilePath = "";
  12.         private bool allowsDebug = false;
  13.         private readonly string time = new DateTime().ToString("HH:mm:ss");
  14.         private LogFile LF = null;
  15.         /// <summary>
  16.         /// Creates a new instance of the LoggerCore class, while setting all values if any are present.
  17.         /// </summary>
  18.         /// <param name="path">The path to the log file.</param>
  19.         /// <param name="debug">The value that tells the logger weather it will allow debug output.</param>
  20.         public LoggerCore(string path = "", bool debug = false)
  21.         {
  22.             logFilePath = path;
  23.             allowsDebug = debug;
  24.         }
  25.         /// <summary>
  26.         /// Prints null or blank output to the console.
  27.         /// </summary>
  28.         public void Log()
  29.         {
  30.             Log(" ");
  31.         }
  32.         /// <summary>
  33.         /// Prints the specified data to the console, without formatting.
  34.         /// </summary>
  35.         /// <param name="data">The RAW data to print.</param>
  36.         public void Log(string data)
  37.         {
  38.             Console.WriteLine(data);
  39.         }
  40.         /// <summary>
  41.         /// Prints a formatted based log to the console.
  42.         /// </summary>
  43.         /// <param name="name">The name of the application that sent the data.</param>
  44.         /// <param name="status">The status of the log.</param>
  45.         /// <param name="message">The message to display in the log.</param>
  46.         public void Log(string name, int status, string message)
  47.         {
  48.             if (LF == null) LF = new LogFile(logFilePath, name);
  49.             if (name == null || name == "")
  50.             {
  51.                 var x = $"[{time}]{GetStatusTag(status)} {message}";
  52.                 if ((allowsDebug) && (status == -1 || status == -2 || status == -3 || status == -4))
  53.                 {
  54.                     Log(x);
  55.                     if (LF != null) LF.Write(x);
  56.                 }
  57.                 else
  58.                 {
  59.                     Log(x);
  60.                     if (LF != null) LF.Write(x);
  61.                 }
  62.             }
  63.             else
  64.             {
  65.                 var x = $"[{time}][{name}]{GetStatusTag(status)} {message}";
  66.                 if ((allowsDebug) && (status == -1 || status == -2 || status == -3 || status == -4))
  67.                 {
  68.                     Log(x);
  69.                     if (LF != null) LF.Write(x);
  70.                 }
  71.                 else
  72.                 {
  73.                     Log(x);
  74.                     if (LF != null) LF.Write(x);
  75.                 }
  76.             }
  77.         }
  78.         /// <summary>
  79.         /// Prints a formatted list-based log to the console.
  80.         /// </summary>
  81.         /// <param name="name">The name of the application that sent the data.</param>
  82.         /// <param name="status">The status of the log.</param>
  83.         /// <param name="message">The message to display in the log.</param>
  84.         /// <param name="data">The data to sort then display in the log.</param>
  85.         public void Log(string name, int status, string message, List<string> data)
  86.         {
  87.             string x = "";
  88.             foreach (string s in data)
  89.             {
  90.                 if (x == "" || x == null) x = "    - " + s;
  91.                 else x += "\n    - " + s;
  92.             }
  93.             Log(name, status, $"{message}\n{x}");
  94.         }
  95.  
  96.         private string GetStatusTag(int status)
  97.         {
  98.             string output = "";
  99.             switch (status)
  100.             {
  101.                 case (-4): { output = "[DEBUG][SEVERE]"; break; }
  102.                 case (-3): { output = "[DEBUG][ERROR]"; break; }
  103.                 case (-2): { output = "[DEBUG][WARNING]"; break; }
  104.                 case (-1): { output = "[DEBUG][INFO]"; break; }
  105.                 case (0): { output = "[INFO]"; break; }
  106.                 case (1): { output = "[WARNING]"; break; }
  107.                 case (2): { output = "[ERROR]"; break; }
  108.                 case (3): { output = "[SERVER]"; break; }
  109.                 default: { output = "[INFO]"; break; }
  110.             }
  111.             return output;
  112.         }
  113.     }
  114.  
  115.     /// <summary>
  116.     /// Contains methods used to create a logfile.
  117.     /// </summary>
  118.     public class LogFile
  119.     {
  120.         private string clientName = "";
  121.         private string logFilePath = "";
  122.         private readonly string timestamp = "";
  123.         /// <summary>
  124.         /// Creates a new instance of LogFile, and sets all present settings.
  125.         /// </summary>
  126.         /// <param name="path">The path of the logfile.</param>
  127.         /// <param name="name">The name to output on an error.</param>
  128.         public LogFile(string path, string name = "")
  129.         {
  130.             if (!(logFilePath == null || logFilePath == ""))
  131.             {
  132.                 timestamp = DateTime.Now.ToString("HH_mm_ss(dd-MM-yyyy)");
  133.                 logFilePath = $"{path}\\Logs\\{timestamp}.log";
  134.                 clientName = name;
  135.                 try
  136.                 {
  137.                     if (Directory.Exists(Path.GetDirectoryName(logFilePath)))
  138.                     {
  139.                         //Check For File
  140.                         try
  141.                         {
  142.                             if (!(File.Exists(logFilePath))) File.Create(logFilePath);
  143.                         }
  144.                         catch (Exception ex)
  145.                         {
  146.                             LogData($"Could not create log file! -> {ex.ToString()}");
  147.                         }
  148.                     }
  149.                     else
  150.                     {
  151.                         //Create DIR then check for file.
  152.                         Directory.CreateDirectory(Path.GetDirectoryName(logFilePath));
  153.                         try
  154.                         {
  155.                             if (!(File.Exists(logFilePath))) File.Create(logFilePath);
  156.                         }
  157.                         catch (Exception ex)
  158.                         {
  159.                             LogData($"Could not create log file! -> {ex.ToString()}");
  160.                         }
  161.                     }
  162.                 }
  163.                 catch (Exception ex)
  164.                 {
  165.                     LogData($"Could not create log file directory! -> {ex.ToString()}");
  166.                 }
  167.             }
  168.             else return;
  169.         }
  170.  
  171.         private StreamWriter writer = null;
  172.         /// <summary>
  173.         /// Writes the specified data to the pre-created log file.
  174.         /// </summary>
  175.         /// <param name="data">The data to output.</param>
  176.         public void Write(string data)
  177.         {
  178.             if (writer == null) writer = new StreamWriter(logFilePath, true);
  179.             writer.WriteLine(data);
  180.             writer.Flush();
  181.             writer.Close();
  182.         }
  183.  
  184.         private LoggerCore logger = null;
  185.         private void LogData(string data)
  186.         {
  187.             if (logger == null) logger = new LoggerCore();
  188.             if (clientName == null || clientName == "")
  189.             {
  190.                 var x = $"[LOGFILE-GENERATOR]: >>> {data}";
  191.                 logger.Log(x);
  192.             }
  193.             else
  194.             {
  195.                 var x = $"[{clientName.ToUpper()}//LOGFILE-GENERATOR]: >>> {data}";
  196.                 logger.Log(x);
  197.             }
  198.         }
  199.     }
  200.  
  201.     /// <summary>
  202.     /// Contains simple methods for logging to the console.
  203.     /// </summary>
  204.     public class Logger
  205.     {
  206.         private string clientName = "";
  207.         private LoggerCore loggerCore = null;
  208.         /// <summary>
  209.         /// Creates a new instance of the Logger class, and sets all settings if present.
  210.         /// </summary>
  211.         /// <param name="name">The name of the application that invoked the logger.</param>
  212.         /// <param name="logFilePath">The path of the log file.</param>
  213.         /// <param name="debug">The value that tells the logger weather it should use default output.</param>
  214.         public Logger(string name = "", string logFilePath = "", bool debug = false)
  215.         {
  216.             clientName = name;
  217.             if (loggerCore == null) loggerCore = new LoggerCore(logFilePath, debug);
  218.         }
  219.         /// <summary>
  220.         /// Logs a blank log to the console.
  221.         /// </summary>
  222.         public void Log()
  223.         {
  224.             loggerCore.Log();
  225.         }
  226.         /// <summary>
  227.         /// Logs a formatted log to the console.
  228.         /// </summary>
  229.         /// <param name="id">The ID of the status to display in the log.</param>
  230.         /// <param name="message">The message to display in the log.</param>
  231.         public void Log(StatusIDS id, string message)
  232.         {
  233.             loggerCore.Log(clientName, (int)id, message);
  234.         }
  235.         /// <summary>
  236.         /// Logs a list in a formatted log.
  237.         /// </summary>
  238.         /// <param name="id">The ID of the status to display in the log.</param>
  239.         /// <param name="message">The message to display in the log.</param>
  240.         /// <param name="data">The list to sort and log.</param>
  241.         public void Log(StatusIDS id, string message, List<string>data)
  242.         {
  243.             loggerCore.Log(clientName, (int)id, message, data);
  244.         }
  245.  
  246.         public enum StatusIDS : int
  247.         {
  248.             DEBUG_SEVERE = -4,
  249.             DEBUG_ERROR = -3,
  250.             DEBUG_WARNING = -2,
  251.             DEBUG_INFO = -1,
  252.             INFO = -0,
  253.             WARNING = 1,
  254.             ERROR = 2,
  255.             SEVERE = 3
  256.         };
  257.     }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement