Skip to content
This repository has been archived by the owner on Feb 27, 2022. It is now read-only.

serilog-archive/serilog-sinks-signalr

Repository files navigation

serilog-sinks-signalr

Build status

A Serilog sink that writes events to a SignalR Hub

Configuration from hub application

From within the SignalR server application with a hub named MyHub:

var hubContext = GlobalHost.ConnectionManager.GetHubContext<MyHub>();

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.SignalR(hubContext,
  Serilog.Events.LogEventLevel.Information,
  groupNames: new[] { "CustomGroup"}, // default is null
  userIds: new[] { "JaneD1234" }, // default is null
  excludedConnectionIds: new[] { "12345", "678910" }) // default is null
.CreateLogger();

Configuration from other clients

From any client application with a hub hosted at http://localhost:8080 and a hub implemented named MyHub:

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.SignalRClient("http://localhost:8080",
  Serilog.Events.LogEventLevel.Information,
  hub: "MyHub" // default is LogHub
  groupNames: new[] { "CustomGroup"}, // default is null
  userIds: new[] { "JaneD1234" }) // default is null
.CreateLogger();

SignalR Server

Create a hub class with any name that ends in Hub or use the default name LogHub. Then create a method named receiveLogEvent, which is capable of accepting all data from the sink.

public class MyHub : Hub
{
  public void receiveLogEvent(string[] groups, string[] userIds, Serilog.Sinks.SignalR.Data.LogEvent logEvent)
  {
    // send to all clients
    Clients.All.sendLogEvent(logEvent);
    // just the specified groups
	Clients.Groups(groups).sendLogEvent(logEvent);
    // just the specified users
    Clients.Users(users).sendLogEvent(logEvent);
  }
}

Receiving the log event

Set up a SignalR client and subscribe to the sendLogEvent method.

var connection = new HubConnection("http://localhost:8080");
var hubProxy = connection.CreateHubProxy("MyHub");

hubProxy.On<Serilog.Sinks.SignalR.Data.LogEvent>("sendLogEvent", (logEvent) =>
{
  Console.WriteLine(logEvent.RenderedMessage);
});

About

A Serilog sink that writes events to SignalR

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published