Skip to content

Instantly share code, notes, and snippets.

@coolya
Last active August 29, 2015 14:04
Show Gist options
  • Save coolya/1fccef3591086bd7ba7d to your computer and use it in GitHub Desktop.
Save coolya/1fccef3591086bd7ba7d to your computer and use it in GitHub Desktop.
High Performance Loggin
using logv.http;
using logv.ws.core;
using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using MongoDB.Bson;
using logv.ws.core.data;
namespace logv.host
{
class Logger : ILog
{
private readonly BlockingCollection<string> _consoleMessages = new BlockingCollection<string>();
private readonly BlockingCollection<LogMsg> _mongoMessages = new BlockingCollection<LogMsg>();
private string _plugin;
private LogLevel _level = LogLevel.ALL;
private IServiceLocator locator;
public Logger(string plugin, IServiceLocator locator)
{
_plugin = plugin;
this.locator = locator;
Task.Factory.StartNew(() =>
{
foreach (var message in _consoleMessages.GetConsumingEnumerable())
{
Console.WriteLine(message);
}
}, TaskCreationOptions.LongRunning);
Task.Factory.StartNew(() =>
{
var repos = locator.GetSingleton<IRepository<ObjectId>>();
foreach (var message in _mongoMessages.GetConsumingEnumerable())
{
repos.Save("Logger",new [] {message});
}
}, TaskCreationOptions.LongRunning);
}
public void Verbose(string message)
{
if ((_level & LogLevel.VERBOSE) == 0)
return;
var now = DateTime.UtcNow;
Task.Factory.StartNew(() =>
{
Log(LogLevel.VERBOSE, now, message);
});
}
public void Verbose(string message, object data)
{
if ((_level & LogLevel.VERBOSE) == 0)
return;
var now = DateTime.UtcNow;
Task.Factory.StartNew(() =>
{
_consoleMessages.Add(string.Format("[{0}]::{1}::{2}" + Environment.NewLine +
"[{0}]::::{2}", _plugin, now, message, data));
});
}
public void Verbose(string message, System.Net.HttpListenerRequest data)
{
if ((_level & LogLevel.VERBOSE) == 0)
return;
var now = DateTime.UtcNow;
var msg = message + Environment.NewLine + data.DumpHeader();
Task.Factory.StartNew(() =>
{
Log(LogLevel.VERBOSE, now, msg);
});
}
public void Verbose(System.Net.HttpListenerRequest data)
{
if ((_level & LogLevel.VERBOSE) == 0)
return;
var now = DateTime.UtcNow;
var msg = data.DumpHeader();
Task.Factory.StartNew(() =>
{
Log(LogLevel.VERBOSE, now,msg);
});
}
public void Debug(string message)
{
if ((_level & LogLevel.DEBUG) == 0)
return;
var now = DateTime.UtcNow;
Task.Factory.StartNew(() =>
{
Log(LogLevel.DEBUG, now, message);
});
}
public void Debug(string message, System.Net.HttpListenerRequest data)
{
if ((_level & LogLevel.DEBUG) == 0)
return;
var now = DateTime.UtcNow;
var msg = message + Environment.NewLine + data.DumpHeader();
Task.Factory.StartNew(() =>
{
Log(LogLevel.DEBUG, now, msg);
});
}
public void Debug(System.Net.HttpListenerRequest data)
{
if ((_level & LogLevel.DEBUG) == 0)
return;
var now = DateTime.UtcNow;
var msg = data.DumpHeader();
Task.Factory.StartNew(() =>
{
Log(LogLevel.DEBUG, now, msg);
});
}
public void Info(string message)
{
if ((_level & LogLevel.INFO) == 0)
return;
var now = DateTime.UtcNow;
Task.Factory.StartNew(() =>
{
Log(LogLevel.INFO, now, message);
});
}
public void Info(string message, System.Net.HttpListenerRequest data)
{
if ((_level & LogLevel.INFO) == 0)
return;
var now = DateTime.UtcNow;
var msg = message + Environment.NewLine + data.DumpHeader();
Task.Factory.StartNew(() =>
{
Log(LogLevel.INFO, now, msg);
});
}
public void Info(System.Net.HttpListenerRequest data)
{
if ((_level & LogLevel.INFO) == 0)
return;
var now = DateTime.UtcNow;
var msg = data.DumpHeader();
Task.Factory.StartNew(() =>
{
Log(LogLevel.INFO, now, msg);
});
}
public void Warning(string message, System.Net.HttpListenerRequest data)
{
if ((_level & LogLevel.WARNING) == 0)
return;
var now = DateTime.UtcNow;
var msg = message + Environment.NewLine + data.DumpHeader();
Task.Factory.StartNew(() =>
{
Log(LogLevel.WARNING, now, msg);
});
}
public void Warning(string message, Exception ex, System.Net.HttpListenerRequest data)
{
if ((_level & LogLevel.WARNING) == 0)
return;
var now = DateTime.UtcNow;
var msg = message + Environment.NewLine + data.DumpHeader();
Task.Factory.StartNew(() =>
{
Log(LogLevel.WARNING, now, msg, ex);
});
}
public void Warning(Exception ex, System.Net.HttpListenerRequest data)
{
if ((_level & LogLevel.WARNING) == 0)
return;
var now = DateTime.UtcNow;
var msg = data.DumpHeader();
Task.Factory.StartNew(() =>
{
Log(LogLevel.WARNING, now, msg, ex);
});
}
public void Warning(string message)
{
if ((_level & LogLevel.WARNING) == 0)
return;
var now = DateTime.UtcNow;
Task.Factory.StartNew(() =>
{
Log(LogLevel.WARNING, now, message);
});
}
public void Warning(string message, Exception ex)
{
if ((_level & LogLevel.WARNING) == 0)
return;
var now = DateTime.UtcNow;
Task.Factory.StartNew(() =>
{
Log(LogLevel.WARNING, now, message, ex);
});
}
public void Warning(Exception ex)
{
if ((_level & LogLevel.WARNING) == 0)
return;
var now = DateTime.UtcNow;
Task.Factory.StartNew(() =>
{
Log(LogLevel.WARNING, now, ex);
});
}
public void Error(string message, System.Net.HttpListenerRequest data)
{
if ((_level & LogLevel.ERROR) == 0)
return;
var now = DateTime.UtcNow;
var msg = message + Environment.NewLine + data.DumpHeader();
Task.Factory.StartNew(() =>
{
Log(LogLevel.ERROR, now, msg);
});
}
public void Error(string message, Exception ex, System.Net.HttpListenerRequest data)
{
if ((_level & LogLevel.ERROR) == 0)
return;
var now = DateTime.UtcNow;
var msg = message + Environment.NewLine + data.DumpHeader();
Task.Factory.StartNew(() =>
{
Log(LogLevel.ERROR, now, msg, ex);
});
}
public void Error(Exception ex, System.Net.HttpListenerRequest data)
{
if ((_level & LogLevel.ERROR) == 0)
return;
var now = DateTime.UtcNow;
var msg = data.DumpHeader();
Task.Factory.StartNew(() =>
{
Log(LogLevel.ERROR, now, msg, ex);
});
}
public void Error(string message)
{
if ((_level & LogLevel.ERROR) == 0)
return;
var now = DateTime.UtcNow;
Task.Factory.StartNew(() =>
{
Log(LogLevel.ERROR, now, message);
});
}
public void Error(string message, Exception ex)
{
if ((_level & LogLevel.ERROR) == 0)
return;
var now = DateTime.UtcNow;
Task.Factory.StartNew(() =>
{
Log(LogLevel.ERROR, now, message, ex);
});
}
public void Error(Exception ex)
{
if ((_level & LogLevel.ERROR) == 0)
return;
var now = DateTime.UtcNow;
Task.Factory.StartNew(() =>
{
Log(LogLevel.ERROR, now, ex);
});
}
public void Fatal(string message, System.Net.HttpListenerRequest data)
{
if ((_level & LogLevel.FATAL) == 0)
return;
var now = DateTime.UtcNow;
var msg = message + Environment.NewLine + data.DumpHeader();
Task.Factory.StartNew(() =>
{
Log(LogLevel.FATAL, now, msg);
});
}
public void Fatal(string message, Exception ex, System.Net.HttpListenerRequest data)
{
if ((_level & LogLevel.FATAL) == 0)
return;
var now = DateTime.UtcNow;
var msg = message + Environment.NewLine + data.DumpHeader();
Task.Factory.StartNew(() =>
{
Log(LogLevel.FATAL, now, msg , ex);
});
}
public void Fatal(Exception ex, System.Net.HttpListenerRequest data)
{
if ((_level & LogLevel.FATAL) == 0)
return;
var now = DateTime.UtcNow;
var message = data.DumpHeader();
Task.Factory.StartNew(() =>
{
Log(LogLevel.FATAL, now, message, ex);
});
}
public void Fatal(string message)
{
if ((_level & LogLevel.FATAL) == 0)
return;
var now = DateTime.UtcNow;
Task.Factory.StartNew(() =>
{
Log(LogLevel.FATAL, now, message);
});
}
public void Fatal(string message, Exception ex)
{
if ((_level & LogLevel.FATAL) == 0)
return;
var now = DateTime.UtcNow;
Task.Factory.StartNew(() =>
{
Log(LogLevel.FATAL, now, message, ex);
});
}
public void Fatal(Exception ex)
{
if ((_level & LogLevel.FATAL) == 0)
return;
var now = DateTime.UtcNow;
Task.Factory.StartNew(() =>
{
Log(LogLevel.FATAL, now, ex);
});
}
public void Log(LogLevel level, DateTime now, string message)
{
_consoleMessages.Add(string.Format("[{0}] {1} :: {2} -> {3}", _plugin, level, now, message));
if(level >= LogLevel.WARNING)
_mongoMessages.Add(new LogMsg() { Message = message, Date = now, Level = level, PlugIn = _plugin });
}
public void Log(LogLevel level, DateTime now, string message, object data)
{
_consoleMessages.Add(string.Format("[{0}] {1} :: {2} -> {3}" + Environment.NewLine +
"->->->->-> {4}" , _plugin, level, now, message, data));
if (level >= LogLevel.WARNING)
_mongoMessages.Add(new LogMsg() { Message = message, Date = now, Level = level, PlugIn = _plugin });
}
public void Log(LogLevel level, DateTime now, string message, Exception ex)
{
_consoleMessages.Add(string.Format("[{0}] {1} :: {2} -> {3}" + Environment.NewLine +
"->->->->-> EXCEPTION <-<-<-<-<-" + Environment.NewLine +
"->->->->-> Type: {4}" + Environment.NewLine +
"->->->->-> Source: {5}" + Environment.NewLine +
"->->->->-> Message: {6}" + Environment.NewLine +
"->->->->-> Stacktrace: {7}"
, _plugin, level, now, message, ex.GetType(), ex.Source, ex.Message, ex.StackTrace));
if (level >= LogLevel.WARNING)
_mongoMessages.Add(new LogMsg() { Message = message, Date = now, Level = level, PlugIn = _plugin, Ex = ex });
if(ex.InnerException != null)
Log(level, now, message, ex.InnerException);
}
public void Log(LogLevel level, DateTime now, Exception ex)
{
_consoleMessages.Add(string.Format("[{0}] {1} :: {2} " +
"->->->->-> EXCEPTION <-<-<-<-<-" + Environment.NewLine +
"->->->->-> Type: {3}" + Environment.NewLine +
"->->->->-> Source: {4}" + Environment.NewLine +
"->->->->-> Message: {5}" + Environment.NewLine +
"->->->->-> Stacktrace: {6}"
, _plugin, level, now, ex.GetType(), ex.Source, ex.Message, ex.StackTrace));
if (level >= LogLevel.WARNING)
_mongoMessages.Add(new LogMsg() { Date = now, Level = level, PlugIn = _plugin, Ex = ex });
if(ex.InnerException != null)
Log(level, now, ex.InnerException);
}
[Flags]
public enum LogLevel
{
None = 0,
VERBOSE = 1,
DEBUG = 2,
INFO = 4,
WARNING = 8,
ERROR = 16,
FATAL = 32,
ALL = 127
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment