Skip to content

Instantly share code, notes, and snippets.

@heiswayi
Last active March 14, 2024 00:19
Show Gist options
  • Select an option

  • Save heiswayi/69ef5413c0f28b3a58d964447c275058 to your computer and use it in GitHub Desktop.

Select an option

Save heiswayi/69ef5413c0f28b3a58d964447c275058 to your computer and use it in GitHub Desktop.

Revisions

  1. heiswayi revised this gist Oct 18, 2022. No changes.
  2. heiswayi revised this gist Jan 30, 2021. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions SimpleLogger.cs
    Original file line number Diff line number Diff line change
    @@ -104,14 +104,15 @@ private void WriteLine(string text, bool append = false)
    {
    try
    {
    if (string.IsNullOrEmpty(text))
    {
    return;
    }
    lock (fileLock)
    {
    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(logFilename, append, System.Text.Encoding.UTF8))
    {
    if (!string.IsNullOrEmpty(text))
    {
    writer.WriteLine(text);
    }
    writer.WriteLine(text);
    }
    }
    }
  3. heiswayi revised this gist Jan 30, 2021. 1 changed file with 10 additions and 6 deletions.
    16 changes: 10 additions & 6 deletions SimpleLogger.cs
    Original file line number Diff line number Diff line change
    @@ -25,6 +25,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    public class SimpleLogger
    {
    private const string FILE_EXT = ".log";
    private readonly object fileLock = new object();
    private readonly string datetimeFormat;
    private readonly string logFilename;

    @@ -41,7 +42,7 @@ public SimpleLogger()
    string logHeader = logFilename + " is created.";
    if (!System.IO.File.Exists(logFilename))
    {
    WriteLine(System.DateTime.Now.ToString(datetimeFormat) + " " + logHeader, false);
    WriteLine(System.DateTime.Now.ToString(datetimeFormat) + " " + logHeader);
    }
    }

    @@ -99,15 +100,18 @@ public void Warning(string text)
    WriteFormattedLog(LogLevel.WARNING, text);
    }

    private void WriteLine(string text, bool append = true)
    private void WriteLine(string text, bool append = false)
    {
    try
    {
    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(logFilename, append, System.Text.Encoding.UTF8))
    lock (fileLock)
    {
    if (!string.IsNullOrEmpty(text))
    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(logFilename, append, System.Text.Encoding.UTF8))
    {
    writer.WriteLine(text);
    if (!string.IsNullOrEmpty(text))
    {
    writer.WriteLine(text);
    }
    }
    }
    }
    @@ -145,7 +149,7 @@ private void WriteFormattedLog(LogLevel level, string text)
    break;
    }

    WriteLine(pretext + text);
    WriteLine(pretext + text, true);
    }

    [System.Flags]
  4. heiswayi revised this gist Aug 6, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion SimpleLogger.cs
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    /*
    MIT License
    Copyright (c) 2019 Heiswayi Nrird
    Copyright (c) 2016 Heiswayi Nrird
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
  5. heiswayi revised this gist Aug 6, 2020. No changes.
  6. heiswayi revised this gist Aug 6, 2020. No changes.
  7. heiswayi revised this gist Apr 17, 2019. 1 changed file with 24 additions and 0 deletions.
    24 changes: 24 additions & 0 deletions SimpleLogger.cs
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,27 @@
    /*
    MIT License
    Copyright (c) 2019 Heiswayi Nrird
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
    */

    public class SimpleLogger
    {
    private const string FILE_EXT = ".log";
  8. heiswayi revised this gist Oct 25, 2018. No changes.
  9. heiswayi revised this gist Oct 25, 2018. 1 changed file with 20 additions and 19 deletions.
    39 changes: 20 additions & 19 deletions SimpleLogger.cs
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    public class SimpleLogger
    {
    private const string FILE_EXT = ".log";
    private readonly string datetimeFormat;
    private readonly string logFilename;

    @@ -10,7 +11,7 @@ public class SimpleLogger
    public SimpleLogger()
    {
    datetimeFormat = "yyyy-MM-dd HH:mm:ss.fff";
    logFilename = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".log";
    logFilename = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + FILE_EXT;

    // Log file header line
    string logHeader = logFilename + " is created.";
    @@ -74,6 +75,24 @@ public void Warning(string text)
    WriteFormattedLog(LogLevel.WARNING, text);
    }

    private void WriteLine(string text, bool append = true)
    {
    try
    {
    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(logFilename, append, System.Text.Encoding.UTF8))
    {
    if (!string.IsNullOrEmpty(text))
    {
    writer.WriteLine(text);
    }
    }
    }
    catch
    {
    throw;
    }
    }

    private void WriteFormattedLog(LogLevel level, string text)
    {
    string pretext;
    @@ -105,24 +124,6 @@ private void WriteFormattedLog(LogLevel level, string text)
    WriteLine(pretext + text);
    }

    private void WriteLine(string text, bool append = true)
    {
    try
    {
    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(logFilename, append, System.Text.Encoding.UTF8))
    {
    if (text != "")
    {
    writer.WriteLine(text);
    }
    }
    }
    catch
    {
    throw;
    }
    }

    [System.Flags]
    private enum LogLevel
    {
  10. heiswayi revised this gist Apr 16, 2018. 1 changed file with 111 additions and 123 deletions.
    234 changes: 111 additions & 123 deletions SimpleLogger.cs
    Original file line number Diff line number Diff line change
    @@ -1,148 +1,136 @@
    using System;
    using System.IO;
    using System.Reflection;
    using System.Text;

    namespace HeiswayiNrird.SimpleLogger
    public class SimpleLogger
    {
    public class SimpleLogger
    private readonly string datetimeFormat;
    private readonly string logFilename;

    /// <summary>
    /// Initiate an instance of SimpleLogger class constructor.
    /// If log file does not exist, it will be created automatically.
    /// </summary>
    public SimpleLogger()
    {
    private string DatetimeFormat;
    private string Filename;
    datetimeFormat = "yyyy-MM-dd HH:mm:ss.fff";
    logFilename = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".log";

    /// <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)
    // Log file header line
    string logHeader = logFilename + " is created.";
    if (!System.IO.File.Exists(logFilename))
    {
    DatetimeFormat = "yyyy-MM-dd HH:mm:ss.fff";
    Filename = Assembly.GetExecutingAssembly().GetName().Name + ".log";

    // Log file header line
    string logHeader = Filename + " is created.";
    if (!File.Exists(Filename))
    {
    WriteLine(DateTime.Now.ToString(DatetimeFormat) + " " + logHeader, false);
    }
    else
    {
    if (append == false)
    WriteLine(DateTime.Now.ToString(DatetimeFormat) + " " + logHeader, false);
    }
    WriteLine(System.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 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 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 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 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 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>
    /// Log a WARNING 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)
    private void WriteFormattedLog(LogLevel level, string text)
    {
    string pretext;
    switch (level)
    {
    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);
    case LogLevel.TRACE:
    pretext = System.DateTime.Now.ToString(datetimeFormat) + " [TRACE] ";
    break;
    case LogLevel.INFO:
    pretext = System.DateTime.Now.ToString(datetimeFormat) + " [INFO] ";
    break;
    case LogLevel.DEBUG:
    pretext = System.DateTime.Now.ToString(datetimeFormat) + " [DEBUG] ";
    break;
    case LogLevel.WARNING:
    pretext = System.DateTime.Now.ToString(datetimeFormat) + " [WARNING] ";
    break;
    case LogLevel.ERROR:
    pretext = System.DateTime.Now.ToString(datetimeFormat) + " [ERROR] ";
    break;
    case LogLevel.FATAL:
    pretext = System.DateTime.Now.ToString(datetimeFormat) + " [FATAL] ";
    break;
    default:
    pretext = "";
    break;
    }

    /// <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)
    WriteLine(pretext + text);
    }

    private void WriteLine(string text, bool append = true)
    {
    try
    {
    try
    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(logFilename, append, System.Text.Encoding.UTF8))
    {
    using (StreamWriter Writer = new StreamWriter(Filename, append, Encoding.UTF8))
    if (text != "")
    {
    if (text != "") Writer.WriteLine(text);
    writer.WriteLine(text);
    }
    }
    catch
    {
    throw;
    }
    }

    /// <summary>
    /// Supported log level
    /// </summary>
    [Flags]
    private enum LogLevel
    catch
    {
    TRACE,
    INFO,
    DEBUG,
    WARNING,
    ERROR,
    FATAL
    throw;
    }
    }

    [System.Flags]
    private enum LogLevel
    {
    TRACE,
    INFO,
    DEBUG,
    WARNING,
    ERROR,
    FATAL
    }
    }
  11. heiswayi revised this gist Nov 16, 2017. No changes.
  12. heiswayi created this gist Dec 25, 2016.
    148 changes: 148 additions & 0 deletions SimpleLogger.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,148 @@
    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";

    // Log file header line
    string logHeader = Filename + " is created.";
    if (!File.Exists(Filename))
    {
    WriteLine(DateTime.Now.ToString(DatetimeFormat) + " " + logHeader, false);
    }
    else
    {
    if (append == false)
    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)
    {
    try
    {
    using (StreamWriter Writer = new StreamWriter(Filename, append, Encoding.UTF8))
    {
    if (text != "") Writer.WriteLine(text);
    }
    }
    catch
    {
    throw;
    }
    }

    /// <summary>
    /// Supported log level
    /// </summary>
    [Flags]
    private enum LogLevel
    {
    TRACE,
    INFO,
    DEBUG,
    WARNING,
    ERROR,
    FATAL
    }
    }
    }