Created
December 29, 2016 16:10
-
-
Save fcojperez/1d39d171fb652178c0444f11df6652fd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Utilites | |
{ | |
/// <summary> | |
/// Basic Logging class for tracking execution programas | |
/// </summary> | |
/// <remarks> | |
/// Developer: Francisco Perez | |
/// Mail: [email protected] | |
/// Date: 29/12/2016 | |
/// </remarks> | |
public class Logging | |
{ | |
private DateTime _startLogging; | |
private string _fileNameDateTimePrefix; | |
private static Logging _loggin; | |
private string _logFileName; | |
private StreamWriter _logFile; | |
public string LogFileName { | |
get { | |
return this._logFileName; | |
} | |
} | |
public static Logging Init() { | |
if (_loggin == null) | |
{ | |
_loggin = new Logging(); | |
} | |
return _loggin; | |
} | |
public static Logging Init(string logFileName) { | |
if (_loggin == null) | |
{ | |
_loggin = new Logging(logFileName); | |
} | |
return _loggin; | |
} | |
private Logging() { | |
initVariables(); | |
_logFileName = String.Format("{0}.log", _fileNameDateTimePrefix); | |
_logFile = new StreamWriter(_logFileName); | |
} | |
private Logging(string logFileName) { | |
initVariables(); | |
_logFileName = String.Format("{0}_{1}.log", _fileNameDateTimePrefix, logFileName); | |
_logFile = new StreamWriter(_logFileName); | |
} | |
~Logging() { | |
closeLogFile(); | |
} | |
private void initVariables() { | |
_startLogging = DateTime.Now; | |
_fileNameDateTimePrefix = String.Format("{0,2}{1,2}{2,2}_{3,2}{4,2}{5,2}", _startLogging.Year.ToString("00"), _startLogging.Month.ToString("00"), _startLogging.Day.ToString("00"), _startLogging.Hour.ToString("00"), _startLogging.Minute.ToString("00"), _startLogging.Second.ToString("00")); | |
} | |
public void printTitle(string title) | |
{ | |
string[] formatedTitle = new string[] { | |
new String('*', title.Length + 4) , | |
String.Format("{0} {1} {2}", "*", title, "*"), | |
new String('*', title.Length + 4) | |
}; | |
foreach (string line in formatedTitle) | |
{ | |
Console.WriteLine(line); | |
this._logFile.WriteLine(line); | |
} | |
} | |
public void writeLineTimeStampMessage(string message) | |
{ | |
string msg = String.Format("{0} - {1}", DateTime.Now.ToString(), message); | |
Console.WriteLine(msg); | |
this._logFile.WriteLine(msg); | |
} | |
public void closeLogFile() { | |
this._logFile.Close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment