Enabling Tracing in Silverlight with UI Atoms

On June 19, 2010, in C#, by Akash Kava

UI Atoms 2010 v1.9 update now includes Trace API for developers to log information on screen just like normal .NET application.

Diagnostics Trace API

Microsoft .NET provided Trace API to help in troubleshoot problems which is of great help at time of development. But Silverlight developers are disappointed for not being able to trace in Silverlight apps. However running apps on multiple platform isn’t quite easy, and different user experiences on different platforms raise unexpected issues. Such issues are difficult to resolve as deployed client computer may not have development tools available to troubleshoot and see application logs.

Trace.WriteLine(“ Hello World “);

We sure miss Trace in Silverlight, so we decided to add similar diagnostics API in Silverlight.

Trace Viewer

In Silverlight, as it runs inside a browser, there is no way to see any trace information not only this, on different platforms like Mac, it may be too difficult for no technical user to install and enable remote debugging.

So we decided to include a visual component that can be put anywhere on screen, just like “Output” pane in visual studio. Its developer’s choice to fit the Trace Viewer. Its pretty simple Text View control, where in you can see lines of log automatically scrolling down as log grows.

AtomTraceView

UI Atoms’s AtomTraceView control works well with Silverlight as well as WPF, in WPF it uses inbuilt Trace functionality and displays trace information to user, and in Silverlight we have added custom API, that is used to display Trace information. Syntax is pretty simple.

<Grid>
   <Grid.RowDefintions>
       <RowDefinition/>
       <RowDefinition Height="200"/>
   <Grid.RowDefintions>

   <MyUserControl/>

   <AtomTraceView Grid.Row="1"/>

<Grid>

This way you can include AtomTraceView at the bottom of your page and it will display all traces programmed by the developer.

Limited API

However, .NET Trace API is very huge, and it is extensible, but Trace API offered by UI Atoms as of now is very limited, not by the design but by implications, Silverlight itself is very limited platform, this is just an Add-on for users to troubleshoot their apps.

Trace Statements in Silverlight

In order to write Trace statements, you have to import following namespace,

using NeuroSpeech.UIAtoms.Core;

Then you can write following statements and they will appear in AtomTraceView Control.

AtomTrace.WriteLine(" Hello World ");

AtomTrace.WriteLine( exception.ToString() );

AtomTrace.WriteLine( string.Format("{0},{1} Coordinates", x, y) );

 

The AtomTrace class exists for both Silverlight and WPF, so it becomes easier to write your logs with AtomTrace instead of Trace, so your code can be shared on both Silverlight as well as WPF.

Trace and Multithreading

Trace statements can be accessed by thread in Silverlight app, however the sequence in which it will appear can not be guaranteed because it depends upon the processor and scheduling algorithm used in thread pool of Silverlight application.

Custom Trace Listener

Just similar to .NET, we have included  AtomTraceListener class, which you can derive and customize trace information according to your need as well as it has Traced event, which you can implement and get trace notifications anywhere..

 

Future of Trace API in Silverlight

When Trace API will be included by Microsoft in future edition of Silverlight, we may plan to remove Trace API, to be consistent with the Silverlight development workflow, this API has been enabled only to write shared code between WPF and Silverlight environment. However otherwise, we plan to make tracing more rich with UI Atoms. But for now, it is very helpful for everyone.

Live Demo

Click Here to see live demo of Tracing in Silverlight.

Click Here to download demo version of UI Atoms 2010.

  • Share/Bookmark
Tagged with: