Skip to content

Instantly share code, notes, and snippets.

@timefrancesco
Created July 24, 2014 22:52
Show Gist options
  • Save timefrancesco/5928e241691df69e1655 to your computer and use it in GitHub Desktop.
Save timefrancesco/5928e241691df69e1655 to your computer and use it in GitHub Desktop.
A simple Logging Class for C# and Xamarin
/* A simple Logging class, use with caution.
Usage: Call InitLog to define the logging level
There are 3 parameters:
consoleLevel = the level for logs written in the console (for debugging)
fileLevel = the level for the logs that are written to a file
defaultLevel = the default level of log when no log level is directly specified from the calling method
*/
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Threading;
using System.IO;
namespace Logging
{
public class Log
{
public enum LoggerLevel
{
LOG_NONE = -1,
LOG_ERROR = 0,
LOG_WARNING = 1,
LOG_INFORMATION = 2,
}
//XAMARIN RELATED
public static string _logFile = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)+ "/log";
private static Log _instance;
public string m_strPath = "";
private string _filePath = "";
private LoggerLevel _consoleLevel = 0;
private LoggerLevel _fileLevel = 0;
private LoggerLevel _defaultLevel = 0;
private object m_csWrite = new object();
private Log (string strPath, string strNomeFile)
{
m_strPath = strPath;
_filePath = strNomeFile;
}
public static Log Instance
{
get
{
if (_instance == null)
_instance = new Log(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache),_logFile );
return _instance;
}
}
public void WriteLine(string text,params object[] args)
{
Write(string.Format(text, args), _defaultLevel);
}
public void WriteLine(string text,Log.LoggerLevel level, params object[] args )
{
Write(string.Format(text, args), level);
}
public void WriteLine(string text)
{
Write(text,_defaultLevel);
}
public void WriteLine (string strMsgLog, Log.LoggerLevel level)
{
Write(strMsgLog,level);
}
private void Write(string strMsgLog, Log.LoggerLevel level)
{
if (level <= _consoleLevel)
Console.WriteLine(strMsgLog);
if (level <= _fileLevel)
{
lock (m_csWrite)
{
//ThreadPool.QueueUserWorkItem(delegate {
try
{
StreamWriter sw = new StreamWriter(_filePath, true);
string strLogFormat = DateTime.Now.ToString("d-MM-yy HH:mm:ss:fff") + " > ";
sw.WriteLine(strLogFormat + strMsgLog);
sw.Flush();
sw.Close();
}
catch (Exception ex)
{
string msg = ex.Message;
}
//});
}
}
}
public string GetLogDirPath()
{
return m_strPath;
}
public void ChangeLogLevel(Log.LoggerLevel consoleLevel, Log.LoggerLevel fileLevel, Log.LoggerLevel defaultLevel)
{
_consoleLevel = consoleLevel;
_fileLevel = fileLevel;
_defaultLevel = defaultLevel;
}
/// <summary>
/// Inits the log.
/// </summary>
/// <param name="consoleLevel">The level of the logs written to the console</param>
/// <param name="fileLevel">The level of the logs written to the file</param>
/// <param name="defaultLevel">The level of the logs written when no level is specified</param>
/// <param name="append">If set to <c>true</c> it append the logs to the file.</param>
public void InitLog (Log.LoggerLevel consoleLevel, Log.LoggerLevel fileLevel, Log.LoggerLevel defaultLevel, bool append)
{
_consoleLevel = consoleLevel;
_fileLevel = fileLevel;
_defaultLevel = defaultLevel;
if (!append) {
if (File.Exists(_filePath))
File.Delete(_filePath);
}
}
public string ReadLogFile ()
{
string log = string.Empty;
if (File.Exists (_logFile)) {
log = File.ReadAllText(_logFile);
}
return log;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment