Up Test, log and debug code samples
DevForce 2012 Resource Center » Samples » Test, log and debug code samples » Code sample: Trace viewer (Silverlight)

Code sample: Trace viewer (Silverlight)

Last modified on September 20, 2012 11:15

You can use a simple user control for a "real time" display of trace messages in your Silverlight application.


Problem

A debug log is not generated for a Silverlight client application but you'd like to capture the trace messages to help diagnose problems.

Solution

You can still capture the tracing and debugging messages your application generates.  This sample shows a simple trace viewer user control which displays these messages in a grid.    

Here's what the viewer looks like when "dropped" into an existing window - a simple real-time display of tracing and debugging messages generated on the client.  Note that messages from the server will not be shown by this viewer.

slviewer.JPG

Here's the XAML for the control...

XAML
<UserControl x:Class="IdeaBlade.Samples.TraceWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  
    Width="Auto" Height="Auto">

    <Grid x:Name="LayoutRoot" Margin="20,20,20,20" >
      <data:DataGrid x:Name="_dataGrid"
         HorizontalAlignment="Left"
         VerticalAlignment="Top"
         AutoGenerateColumns="True"
         MinWidth="250"
         MinHeight="100"
         Background="#FFB5BAB5"
         Margin="0,0,20,0"
         IsReadOnly="True"
         />
    </Grid>
</UserControl>

...and the code behind:

C#
using System;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using IdeaBlade.Core;

namespace IdeaBlade.Samples {

 /// <summary>
 /// Sample trace subscriber.  You can drop the TraceViewer UserControl onto a page
 /// to display tracing information from the Silverlight application in a grid.
 /// </summary>
 /// <remarks>
 /// To use the TraceSubscriber: 1) listen for its Publish event,
 /// and 2) call StartSubscription() to have tracing messages sent to you.  
 /// You can also call StopSubscription() to temporarily or permanently stop receiving messages.
 /// </remarks>
 public partial class TraceWindow : UserControl {

   public TraceWindow() {

      InitializeComponent();

      _messages = new ObservableCollection<TraceMessage>();
      _subscriber = new TraceSubscriber();
      _subscriber.Publish +=
       new EventHandler<PublishEventArgs>(_subscriber_Publish);
      _subscriber.StartSubscription();

      _dataGrid.ItemsSource = _messages;
    }

   private void _subscriber_Publish(object sender, PublishEventArgs e) {
      _messages.Add(e.TraceMessage);
     if (_dataGrid.Columns.Count > 0) {
        _dataGrid.ScrollIntoView(e.TraceMessage, _dataGrid.Columns[0]);
      }
    }

    TraceSubscriber _subscriber;
    ObservableCollection<TraceMessage> _messages;
  }   
}
VB
Imports System
Imports System.Collections.ObjectModel
Imports System.Windows.Controls
Imports IdeaBlade.Core

Namespace IdeaBlade.Samples

 ''' <summary>
 ''' Sample trace subscriber.  You can drop the TraceViewer UserControl onto a page
 ''' to display tracing information from the Silverlight application in a grid.
 ''' </summary>
 ''' <remarks>
 ''' To use the TraceSubscriber: 1) listen for its Publish event,
 ''' and 2) call StartSubscription() to have tracing messages sent to you.
 ''' You can also call StopSubscription() to temporarily or permanently stop receiving messages.
 ''' </remarks>
 Partial Public Class TraceWindow
   Inherits UserControl

   Public Sub New()

      InitializeComponent()

      _messages = New ObservableCollection(Of TraceMessage)()
      _subscriber = New TraceSubscriber()
     AddHandler _subscriber.Publish, AddressOf _subscriber_Publish
      _subscriber.StartSubscription()

      _dataGrid.ItemsSource = _messages
   End Sub

   Private Sub _subscriber_Publish(ByVal sender As Object, _
     ByVal e As PublishEventArgs)
      _messages.Add(e.TraceMessage)
     If _dataGrid.Columns.Count > 0 Then
        _dataGrid.ScrollIntoView(e.TraceMessage, _dataGrid.Columns(0))
     End If
   End Sub

   Private _subscriber As TraceSubscriber
   Private _messages As ObservableCollection(Of TraceMessage)
 End Class
End Namespace

To add the user control to a page or another control, just add and style as needed: 

XAML
<local:TraceWindow />

Prerequisites

Created by DevForce on February 23, 2011 09:37

This wiki is licensed under a Creative Commons 2.0 license. XWiki Enterprise 3.2 - Documentation. Copyright © 2015 IdeaBlade