-
-
Save heiswayi/69ef5413c0f28b3a58d964447c275058 to your computer and use it in GitHub Desktop.
| public class SimpleLogger | |
| { | |
| private const string FILE_EXT = ".log"; | |
| 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() | |
| { | |
| datetimeFormat = "yyyy-MM-dd HH:mm:ss.fff"; | |
| logFilename = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + FILE_EXT; | |
| // Log file header line | |
| string logHeader = logFilename + " is created."; | |
| if (!System.IO.File.Exists(logFilename)) | |
| { | |
| 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 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 WARNING message | |
| /// </summary> | |
| /// <param name="text">Message</param> | |
| 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; | |
| switch (level) | |
| { | |
| 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; | |
| } | |
| WriteLine(pretext + text); | |
| } | |
| [System.Flags] | |
| private enum LogLevel | |
| { | |
| TRACE, | |
| INFO, | |
| DEBUG, | |
| WARNING, | |
| ERROR, | |
| FATAL | |
| } | |
| } |
I see your code has dependencies on System.IO classes such as File and StreamWriter and System.DateTime. Would it improve the code if you inject these dependencies as interfaces via the constructor?
The append parameter of private void WriteLine(string text, bool append = true) is never used in the code. It will always default to true.
The way the constructor is using the append parameter is not clear. You might consider something like this where the local instance of append is set to false to force the creation of the log file if it does not exist:
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))
{
append =false; // force the file to be created
}
if (append == false)
{
WriteLine(DateTime.Now.ToString(DatetimeFormat) + " " + logHeader, false);
}
}
Neat, just fine for simple usage to 0-external-deps apps. Just a small suggestion regarding multithreaded environment:
private readonly object fileLock = new object();
...
private void WriteLine(string text, bool append = true)
{
if (string.IsNullOrEmpty(text))
{
return;
}
lock (fileLock)
{
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(logFilename, append, System.Text.Encoding.UTF8))
{
writer.WriteLine(text);
}
}
}Just to avoid exception during multiple access.
Thanks, I needed a simple logger and it is exactly what I wanted.
exelent let me test it