Skip to content

Instantly share code, notes, and snippets.

@flier268
Forked from heiswayi/SimpleLogger.cs
Last active July 2, 2017 08:49
Show Gist options
  • Save flier268/c8fe6d20bf0e865115347530539b2d3b to your computer and use it in GitHub Desktop.
Save flier268/c8fe6d20bf0e865115347530539b2d3b to your computer and use it in GitHub Desktop.
C# Super simple logger class
using System;
using System.IO;
using System.Reflection;
using System.Text;
namespace HeiswayiNrird.SimpleLogger
{
public class SimpleLogger
{
private string DatetimeFormat;
private string Filename;
/// <summary>
/// Initialize a new instance of SimpleLogger class.
/// 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.
/// Default is create a fresh new log file.
/// </summary>
/// <param name="append">True to append to existing log file, False to overwrite and create new log file</param>
public SimpleLogger(bool append = false)
{
DatetimeFormat = "yyyy-MM-dd HH:mm:ss.fff";
//Filename = Assembly.GetExecutingAssembly().GetName().Name + ".log";
Filename = Assembly.GetEntryAssembly().GetName().Name + ".log";
// Log file header line
string logHeader = Filename + " is created.";
if (!File.Exists(Filename) || append == false)
{
if(!File.Exists(Filename))
File.CreateText(Filename);
WriteLine(DateTime.Now.ToString(DatetimeFormat) + " " + logHeader, false);
}
}
/// <summary>
/// Log a debug message
/// </summary>
/// <param name="text">Message</param>
public void Debug(string text)
{
WriteFormattedLog(LogLevel.DEBUG, text);
}
/// <summary>
/// Log an error message
/// </summary>
/// <param name="text">Message</param>
public void Error(string text)
{
WriteFormattedLog(LogLevel.ERROR, text);
}
/// <summary>
/// Log a fatal error message
/// </summary>
/// <param name="text">Message</param>
public void Fatal(string text)
{
WriteFormattedLog(LogLevel.FATAL, text);
}
/// <summary>
/// Log an info message
/// </summary>
/// <param name="text">Message</param>
public void Info(string text)
{
WriteFormattedLog(LogLevel.INFO, text);
}
/// <summary>
/// Log a trace message
/// </summary>
/// <param name="text">Message</param>
public void Trace(string text)
{
WriteFormattedLog(LogLevel.TRACE, text);
}
/// <summary>
/// Log a waning message
/// </summary>
/// <param name="text">Message</param>
public void Warning(string text)
{
WriteFormattedLog(LogLevel.WARNING, text);
}
/// <summary>
/// Format a log message based on log level
/// </summary>
/// <param name="level">Log level</param>
/// <param name="text">Log message</param>
private void WriteFormattedLog(LogLevel level, string text)
{
string pretext;
switch (level)
{
case LogLevel.TRACE: pretext = DateTime.Now.ToString(DatetimeFormat) + " [TRACE] "; break;
case LogLevel.INFO: pretext = DateTime.Now.ToString(DatetimeFormat) + " [INFO] "; break;
case LogLevel.DEBUG: pretext = DateTime.Now.ToString(DatetimeFormat) + " [DEBUG] "; break;
case LogLevel.WARNING: pretext = DateTime.Now.ToString(DatetimeFormat) + " [WARNING] "; break;
case LogLevel.ERROR: pretext = DateTime.Now.ToString(DatetimeFormat) + " [ERROR] "; break;
case LogLevel.FATAL: pretext = DateTime.Now.ToString(DatetimeFormat) + " [FATAL] "; break;
default: pretext = ""; break;
}
WriteLine(pretext + text);
}
/// <summary>
/// Write a line of formatted log message into a log file
/// </summary>
/// <param name="text">Formatted log message</param>
/// <param name="append">True to append, False to overwrite the file</param>
/// <exception cref="System.IO.IOException"></exception>
private void WriteLine(string text, bool append = true)
{
FileStream stream;
if(append)
{
stream=new FileStream(Filename,FileMode.Append);
}
else
{
if(!File.Exists(Filename))
File.CreateText(Filename);
stream=new FileStream(Filename,FileMode.Truncate);
}
using (StreamWriter Writer = new StreamWriter(stream,Encoding.UTF8))
{
if (text != "") Writer.WriteLine(text);
}
}
/// <summary>
/// Supported log level
/// </summary>
[Flags]
private enum LogLevel
{
TRACE,
INFO,
DEBUG,
WARNING,
ERROR,
FATAL
}
}
}
@flier268
Copy link
Author

flier268 commented Jul 2, 2017

It can be used on .net Core.
I fix append mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment