NLog.SignalR is a SignalR target for NLog, allowing you to send log messages straight to a SignalR hub in real-time.
See the included Sample at /src/NLog.Signalr.Sample.Web and /src/NLog.SignalR.Sample.Command for an example of running two clients (web and console) at the same time and having log messages appear on the web log page from both sources.
To add to your own projects do the following.
Add NLog.SignalR.dll to your project(s) via NuGet
install-package NLog.SignalR
Add the assembly and new target to NLog.config:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<extensions>
<add assembly="NLog.SignalR" />
</extensions>
<targets async="true">
<target xsi:type="SignalR"
name="signalr"
uri="http://localhost:1860"
hubName="LoggingHub"
methodName="Log"
layout="${message}"
/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="signalr" />
</rules>
</nlog>
Note: the only required property for the target is the uri. The target will default hubName, methodName, and layout properties.
There are two methods you can use for setting up the Hub in your web project. One is to set up a strongly-typed hub, using the interface provided by the NLog.SignalR component.
public class LoggingHub : Hub<ILoggingHub>
{
public void Log(LogEvent logEvent)
{
Clients.Others.Log(logEvent);
}
}
The other way is to simply set up a Hub in your web project using dynamic types.
public class LoggingHub : Hub
{
public void Log(dynamic logEvent)
{
Clients.Others.Log(logEvent);
}
}
<script>
$(function() {
var nlog = $.connection.loggingHub;
nlog.client.log = function(logEvent) {
//Put code here to handle the logEvent that is sent.
};
$.connection.hub.start().done(function() {
//Put code here that you want to execute after connecting to the Hub.
});
})
<script>
Feel free to tweet @tmeinershagen for questions or comments on the code. You can also submit a GitHub issue here.
https://github.com/toddmeinershagen/NLog.SignalR/blob/master/LICENSE