DCSIMG
Logging with SignalR - Ran Wahle's blog

Ran Wahle's blog

Logging with SignalR

Logging with SignalR

SignalR Logo

Debugging your signalR application on both client and server
can consume time. To save time we might go to Fiddler,  or
use the Network tab on our browser’s developer tools.

signalR offers simple logging mechanism for javascript clients.
in this post I’ll briefly show how to enable this log, what it
writes when enable, and how to add some custom log records.

 

Enable logging

To enable logging you simply need to add the following

$.connection.hub.logging = true;

After enabling the log you’ll see what client events were
triggered by the server.

For example, when you call in your Hub code (server side) you
call Clients.All.sendMessage, this is what you’ll see on your
console tab (taken from Chrome console)

[12:02:21 GMT+0200 (Jerusalem Standard Time)] SignalR: Triggering client hub event 'sendMessage' on hub 'chatHub'.


Custom logging

 

Apart from simply using the console.log() function, you
can use the $.connection.hub.log() function provided
by signalR. This function won’t do anything if logging
is disabled, end write to the console log if enabled.

Let’s see an example:

  In the following code example we call sever side’s chat method
and log our call write away.

sendMessage : function () {
           viewModel.hub.server.chat(this.message());
           $.connection.hub.log("Sending " + this.message() + " to the server");
           this.message("");
       }

And this is what we’ll see on our console:

[12:59:08 GMT+0200 (Jerusalem Standard Time)] SignalR: Sending Hi fellows to the server

 

Summary

signalR logging is simple yet very handy tool to monitor our application
at development time. You may find it useful and all you have to do
when going to production is to disable the log.

Shout it

Comments

The Morning Brew - Chris Alcock » The Morning Brew #1304 said:

Pingback from  The Morning Brew - Chris Alcock  » The Morning Brew #1304

# February 27, 2013 4:32 AM

rimonabantexcellence site title said:

Pingback from  rimonabantexcellence site title

# June 7, 2013 11:42 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: