Skip to content

Instantly share code, notes, and snippets.

@imzjy
Created September 12, 2016 01:47
Show Gist options
  • Save imzjy/1958f68b448992504a37738825e52ca2 to your computer and use it in GitHub Desktop.
Save imzjy/1958f68b448992504a37738825e52ca2 to your computer and use it in GitHub Desktop.
.net logger without any dependencies
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace Portal.Common
{
public class Logger
{
private static Logger _defaultLogger = null;
public Logger(string fileName)
{
try
{
Trace.Listeners.Clear();
Trace.Listeners.Add(new TextWriterTraceListener(fileName));
Trace.AutoFlush = true;
}
catch (Exception)
{
throw;
}
}
public Logger()
: this("app.log")
{
}
public static Logger Default
{
get
{
if (_defaultLogger == null)
{
_defaultLogger = new Logger();
}
return _defaultLogger;
}
}
public void Info(string msg)
{
this.Info("{0}", msg);
}
public void Info(string format, params string[] msg)
{
Trace.WriteLine(string.Format("{0} [{1}] {2}",
DateTime.Now, "INFO",
string.Format(format, msg)));
}
public void InfoIf(bool condition, string format, params string[] msg)
{
Trace.WriteLineIf(condition, string.Format("{0} [{1}] {2}",
DateTime.Now, "INFO",
string.Format(format, msg)));
}
public void Warn(string format, params string[] msg)
{
Trace.WriteLine(string.Format("{0} [{1}] {2}",
DateTime.Now, "WARN",
string.Format(format, msg)));
}
public void WarnIf(bool condition, string format, params string[] msg)
{
Trace.WriteLineIf(condition, string.Format("{0} [{1}] {2}",
DateTime.Now, "WARN",
string.Format(format, msg)));
}
public void Error(string format, params string[] msg)
{
Trace.WriteLine(string.Format("{0} [{1}] {2}",
DateTime.Now, "ERRO",
string.Format(format, msg)));
}
public void ErrorIf(bool condition, string format, params string[] msg)
{
Trace.WriteLineIf(condition, string.Format("{0} [{1}] {2}",
DateTime.Now, "ERRO",
string.Format(format, msg)));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment