Introducing NLog to SignalR

{  NLog+SignalR+Toastr==NLogToSignalR }

I have created a simple NLog target which uses SignalR (calling it NLogToSignalR) under the covers and delivers the log to either Hub or PersistentConnection and deliver nice messages using toastr.

Where does it fit?

Schematic

Purpose

If you use NLog as a logging tool, you can use this to deliver log messages to either same (or remote server) in real time in true toastr style

e.g. Information can be displayed as

Information

Warning

Warning

Error

Error

How does this work?

You need follow these steps:

1. Put NLog.Targets.SignalR in the same folder as your App.Config/ Web.Config file

2. If you are going to be using Hub for SignalR, use following configuration

nlogconfig

3. Deploy the Server NLog.Targets.SignalR.AspNetServer on local IIS.

4. Browse the website after deployment. (Ensure .NET Framework 4.5 is installed)

5. Start logging and watch the logs in real time.

The code is available for download from github.

Enjoy!

Getting Started with self-hosting SignalR using PersistentConnection

SignalR is a great library to help build real-time, single/multi user apps.
Although SignalR is very well documented, I wanted to write a compact sample which demonstrates Client and Server using PersistentConnection.

I have specifically chosen not to use Hub for this demo as there are many samples out there already.

Step 1
Create a console project.

Step 2
Add following nuget packages
SignalR.Hosting.Self
SignalR.Client

Step 3
To create instance of server, pass the url in the Server class’s constructor. Map the connection. Please make sure that you provide “/” at the end in the url.

You then call MapConnection method with a name of the path. This means that now your client connection becomes Url+Path.

Note MapConnection takes a type ‘T’ where T: PersistentConnection. Since PersistentConnection is an abstract class, you typically inherit from PersistentConnection and that becomes the entry point where the client calls arrives. See below a class called ‘MyConnection’

Next you create instance of Client.Connection class, Be careful, there is a connection class in the server as well so you may have to qualify it with Client.Connection here.

Client side events

‘Received’ is called when Server calls you back
‘StateChanged’ is called when the Connection state changes

SourceCode_image1

Step 4
To call server, call “Start” method on the client’s connection object which returns a task so check for the task for any problems before proceeding and then call ‘Send’ method which actually calls server. Once the Send method is executed, ‘OnReceivedAsync’ method of ‘MyConnection’ class is called. Here you can call all the client’s back by executing ‘Connection.Broadcast’ (or just use Connection.Send method calls the client that is waiting for specific signal) method

SourceCode_image2

Source code for MyConnection class

SourceCode_image3

When you run this sample, you should see output like following.

console_output

You can download sample from here