AspNetCore.Docs/aspnet/signalr/overview/testing-and-debugging/enabling-signalr-tracing.md

11 KiB

uid title author description ms.author manager ms.date ms.topic ms.assetid ms.technology ms.prod msc.legacyurl msc.type
signalr/overview/testing-and-debugging/enabling-signalr-tracing Enabling SignalR Tracing | Microsoft Docs tfitzmac This document describes how to enable and configure tracing for SignalR servers and clients. Tracing enables you to view diagnostic information about events... aspnetcontent wpickett 08/08/2014 article 30060acb-be3e-4347-996f-3870f0c37829 dotnet-signalr .net-framework /signalr/overview/testing-and-debugging/enabling-signalr-tracing authoredcontent

Enabling SignalR Tracing

by Tom FitzMacken

This document describes how to enable and configure tracing for SignalR servers and clients. Tracing enables you to view diagnostic information about events in your SignalR application.

This topic was originally written by Patrick Fletcher.

Software versions used in the tutorial

Questions and comments

Please leave feedback on how you liked this tutorial and what we could improve in the comments at the bottom of the page. If you have questions that are not directly related to the tutorial, you can post them to the ASP.NET SignalR forum or StackOverflow.com.

When tracing is enabled, a SignalR application creates log entries for events. You can log events from both the client and the server. Tracing on the server logs connection, scaleout provider, and message bus events. Tracing on the client logs connection events. In SignalR 2.1 and later, tracing on the client logs the full content of hub invocation messages.

Contents

Enabling tracing on the server

You enable tracing on the server within the application's configuration file (either App.config or Web.config depending on the type of project.) You specify which categories of events you want to log. In the configuration file, you also specify whether to log the events to a text file, the Windows event log, or a custom log using an implementation of TraceListener.

The server event categories include the following sorts of messages:

Source Messages
SignalR.SqlMessageBus SQL Message Bus scaleout provider setup, database operation, error, and timeout events
SignalR.ServiceBusMessageBus Service bus scaleout provider topic creation and subscription, error, and messaging events
SignalR.RedisMessageBus Redis scaleout provider connection, disconnection, and error events
SignalR.ScaleoutMessageBus Scaleout messaging events
SignalR.Transports.WebSocketTransport WebSocket transport connection, disconnection, messaging, and error events
SignalR.Transports.ServerSentEventsTransport ServerSentEvents transport connection, disconnection, messaging, and error events
SignalR.Transports.ForeverFrameTransport ForeverFrame transport connection, disconnection, messaging, and error events
SignalR.Transports.LongPollingTransport LongPolling transport connection, disconnection, messaging, and error events
SignalR.Transports.TransportHeartBeat Transport connection, disconnection, and keepalive events
SignalR.ReflectedHubDescriptorProvider Hub discovery events

Logging server events to text files

The following code shows how to enable tracing for each category of event. This sample configures the application to log events to text files.

XML server code for enabling tracing

[!code-htmlMain]

In the code above, the SignalRSwitch entry specifies the TraceLevel used for events sent to the specified log. In this case, it is set to Verbose which means all debugging and tracing messages are logged.

The following output shows entries from the transports.log.txt file for an application using the above configuration file. It shows a new connection, a removed connection, and transport heartbeat events.

[!code-consoleMain]

Logging server events to the event log

To log events to the event log rather than a text file, change the values for the entries in the sharedListeners node. The following code shows how to log server events to the event log:

XML server code for logging events to the event log

[!code-xmlMain]

The events are logged in the Application log, and are available through the Event Viewer, as shown below:

Event Viewer showing SignalR logs

[!NOTE] When using the event log, set the TraceLevel to Error to keep the number of messages manageable.

Enabling tracing in the .NET client (Windows Desktop apps)

The .NET client can log events to the console, a text file, or to a custom log using an implementation of TextWriter.

To enable logging in the .NET client, set the connection's TraceLevel property to a TraceLevels value, and the TraceWriter property to a valid TextWriter instance.

Logging Desktop client events to the console

The following C# code shows how to log events in the .NET client to the console:

[!code-csharpMain]

Logging Desktop client events to a text file

The following C# code shows how to log events in the .NET client to a text file:

[!code-csharpMain]

The following output shows entries from the ClientLog.txt file for an application using the above configuration file. It shows the client connecting to the server, and the hub invoking a client method called addMessage:

[!code-consoleMain]

Enabling tracing in Windows Phone 8 clients

SignalR applications for Windows Phone apps use the same .NET client as desktop apps, but Console.Out and writing to a file with StreamWriter are not available. Instead, you need to create a custom implementation of TextWriter for tracing.

Logging Windows Phone client events to the UI

The SignalR codebase includes a Windows Phone sample that writes trace output to a TextBlock using a custom TextWriter implementation called TextBlockWriter. This class can be found in the samples/Microsoft.AspNet.SignalR.Client.WP8.Samples project. When creating an instance of TextBlockWriter, pass in the current SynchronizationContext, and a StackPanel where it will create a TextBlock to use for trace output:

[!code-csharpMain]

The trace output will then be written to a new TextBlock created in the StackPanel you passed in:

Logging Windows Phone client events to the debug console

To send output to the debug console rather than the UI, create an implementation of TextWriter that writes to the debug window, and assign it to your connection's TraceWriter property:

[!code-csharpMain]

Trace information will then be written to the debug window in Visual Studio:

Enabling tracing in the JavaScript client

To enable client-side logging on a connection, set the logging property on the connection object before you call the start method to establish the connection.

Client JavaScript code for enabling tracing to the browser console (with the generated proxy)

[!code-javascriptMain]

Client JavaScript code for enabling tracing to the browser console (without the generated proxy)

[!code-javascriptMain]

When tracing is enabled, the JavaScript client logs events to the browser console. To access the browser console, see Monitoring Transports.

The following screenshot shows a SignalR JavaScript client with tracing enabled. It shows connection and hub invocation events in the browser console:

SignalR tracing events in the browser console