Advertisement
hubert17

Simple Logger .NET C# ASP.NET CORE

Feb 14th, 2018
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.66 KB | None | 0 0
  1. using Microsoft.AspNetCore.Hosting;
  2. using System;
  3. using System.IO;
  4.  
  5. public interface ISimpleLogger
  6. {
  7.     void LogDebug(string text);
  8.     void LogError(string text);
  9.     void LogInformation(string text);
  10.     void LogTrace(string text);
  11.     void LogWarning(string text);
  12.  
  13. }
  14.  
  15. public class SimpleLogger : ISimpleLogger
  16. {
  17.     private string DatetimeFormat;
  18.     private string Filename;
  19.  
  20.     private IHostingEnvironment _env;
  21.  
  22.     /// <summary>
  23.     /// Initialize a new instance of SimpleLogger class.
  24.     /// Log file will be created automatically if not yet exists, else it can be either a fresh new file or append to the existing file.
  25.     /// Default is create a fresh new log file.
  26.     /// </summary>
  27.     /// <param name="append">True to append to existing log file, False to overwrite and create new log file</param>
  28.     public SimpleLogger(IHostingEnvironment env)
  29.     {
  30.         _env = env;
  31.  
  32.         DatetimeFormat = "yyyy-MM-dd HH:mm:ss.fff";
  33.         string Date = DateTime.Now.ToString("yyyyMMdd");
  34.         // Filename = Path.Combine(_env.ContentRootPath, @"Logs\log-" + Date + ".txt");
  35.         Filename = Path.Combine(_env.WebRootPath, @"Logs\log-" + Date + ".txt");
  36.  
  37.         if (!File.Exists(Filename))
  38.         {
  39.             // Log file header line
  40.             string logHeader = Filename + " is created." + Environment.NewLine;
  41.             WriteLine(DateTime.Now.ToString(DatetimeFormat) + " " + logHeader, false);
  42.         }
  43.     }
  44.  
  45.     /// <summary>
  46.     /// Log a debug message
  47.     /// </summary>
  48.     /// <param name="text">Message</param>
  49.     public void LogDebug(string text)
  50.     {
  51.         WriteFormattedLog(LogLevel.DEBUG, text);
  52.     }
  53.  
  54.     /// <summary>
  55.     /// Log an error message
  56.     /// </summary>
  57.     /// <param name="text">Message</param>
  58.     public void LogError(string text)
  59.     {
  60.         WriteFormattedLog(LogLevel.ERROR, text);
  61.     }
  62.  
  63.     /// <summary>
  64.     /// Log a fatal error message
  65.     /// </summary>
  66.     /// <param name="text">Message</param>
  67.     public void LogFatal(string text)
  68.     {
  69.         WriteFormattedLog(LogLevel.FATAL, text);
  70.     }
  71.  
  72.     /// <summary>
  73.     /// Log an info message
  74.     /// </summary>
  75.     /// <param name="text">Message</param>
  76.     public void LogInformation(string text)
  77.     {
  78.         WriteFormattedLog(LogLevel.INFO, text);
  79.     }
  80.  
  81.     /// <summary>
  82.     /// Log a trace message
  83.     /// </summary>
  84.     /// <param name="text">Message</param>
  85.     public void LogTrace(string text)
  86.     {
  87.         WriteFormattedLog(LogLevel.TRACE, text);
  88.     }
  89.  
  90.     /// <summary>
  91.     /// Log a waning message
  92.     /// </summary>
  93.     /// <param name="text">Message</param>
  94.     public void LogWarning(string text)
  95.     {
  96.         WriteFormattedLog(LogLevel.WARNING, text);
  97.     }
  98.  
  99.     /// <summary>
  100.     /// Format a log message based on log level
  101.     /// </summary>
  102.     /// <param name="level">Log level</param>
  103.     /// <param name="text">Log message</param>
  104.     private void WriteFormattedLog(LogLevel level, string text)
  105.     {
  106.         string pretext;
  107.         switch (level)
  108.         {
  109.             case LogLevel.TRACE: pretext = DateTime.Now.ToString(DatetimeFormat) + " [TRACE]   "; break;
  110.             case LogLevel.INFO: pretext = DateTime.Now.ToString(DatetimeFormat) + " [INFO]    "; break;
  111.             case LogLevel.DEBUG: pretext = DateTime.Now.ToString(DatetimeFormat) + " [DEBUG]   "; break;
  112.             case LogLevel.WARNING: pretext = DateTime.Now.ToString(DatetimeFormat) + " [WARNING] "; break;
  113.             case LogLevel.ERROR: pretext = DateTime.Now.ToString(DatetimeFormat) + " [ERROR]   "; break;
  114.             case LogLevel.FATAL: pretext = DateTime.Now.ToString(DatetimeFormat) + " [FATAL]   "; break;
  115.             default: pretext = ""; break;
  116.         }
  117.  
  118.         WriteLine(pretext + text + Environment.NewLine);
  119.     }
  120.  
  121.     /// <summary>
  122.     /// Write a line of formatted log message into a log file
  123.     /// </summary>
  124.     /// <param name="text">Formatted log message</param>
  125.     /// <param name="append">True to append, False to overwrite the file</param>
  126.     /// <exception cref="System.IO.IOException"></exception>
  127.     private void WriteLine(string text, bool append = true)
  128.     {
  129.         if (!string.IsNullOrEmpty(text))
  130.         {
  131.             try
  132.             {
  133.                 if (append)
  134.                     File.AppendAllText(Filename, text);
  135.                 else
  136.                     File.WriteAllText(Filename, text);
  137.             }
  138.             catch { }
  139.         }
  140.     }
  141.  
  142.     /// <summary>
  143.     /// Supported log level
  144.     /// </summary>
  145.     [Flags]
  146.     private enum LogLevel
  147.     {
  148.         TRACE,
  149.         INFO,
  150.         DEBUG,
  151.         WARNING,
  152.         ERROR,
  153.         FATAL
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement