Skip to content

Instantly share code, notes, and snippets.

@XSockets
Last active August 29, 2015 14:04
Show Gist options
  • Save XSockets/768a5ddcb17761ab6bd5 to your computer and use it in GitHub Desktop.
Save XSockets/768a5ddcb17761ab6bd5 to your computer and use it in GitHub Desktop.
Serilog Sink for XSockets.NET, clients will receive messages based on LogEventLevel
//Custom Logger Configuration in XSockets by implementing IDefaultLogger
public class MyLogger : IDefaultLogger
{
public ILogger Logger
{
get
{
return new LoggerConfiguration().MinimumLevel.Verbose()
.WriteTo.XSockets()
.CreateLogger();
}
}
}
////////////////////////////////////////
//Write to the log:
////////////////////////////////////////
LogHelper.Log(LogEventLevel.Warning, "Hey, here is a warning for you {@warn}", someObject);
//Based on LogEventLevel each client will be able to set the level individually
//Levels are:
// Anything and everything you might want to know about a running block of code.
Verbose = 0,
// Internal system events that aren't necessarily observable from the outside.
Debug = 1,
// The lifeblood of operational intelligence - things happen.
Information = 2,
// Service is degraded or endangered.
Warning = 3,
// Functionality is unavailable, invariants are broken or data is lost.
Error = 4,
// If you have a pager, it goes off when one of these occurs.
Fatal = 5,
////////////////////////////////////////
//EXAMPLE usage from clients
////////////////////////////////////////
////////////////////////////////////////
//C#
////////////////////////////////////////
//Create a client where you use controllers 'A' and 'B'
IXSocketClient c = new XSocketClient("ws://localhost:4502", "http://xsockets.net","A","B");
//Open the connection synchronous
c.Open();
//Listen for LogEvents
c.Controller("log").On<IMessage>("logevent", log =>
{
Console.WriteLine(log.Data);
});
//Tell XSockets about the LogEventLevel you are interested in.
c.Controller("log").SetEnum("LogEventLevel","Warning");
//Note: You will receive all LogEvents equal to or higher than the level selected
////////////////////////////////////////
//JavaScript
////////////////////////////////////////
//Create a client where you use controllers 'A' and 'B'
var c = new XSocket.WebSocket('ws://localhost:4502',['A','B']);
//Listen for LogEvents
c.controller("log").logevent = function(d){
console.log(d);
});
//Tell XSockets about the LogEventLevel you are interested in.
c.controller("log").setEnum("LogEventLevel","Warning");
//Note: You will receive all LogEvents equal to or higher than the level selected
//Side note... There is one line if code in the sink that makes this work.
_controller.InvokeTo(p => (int)logEvent.Level >= (int)p.LogEventLevel ,logEvent, "logEvent");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment