Creating a exception logger window in Silverlight


In this post I will describe how to handle the exceptions in Silverlight.

Purpose: While running the Silverlight application, if any exception occurs it will go to Silverlight.js and show in the internet explorer as a javascript error. In the debugging point of view sometimes it is cumbersome to determine where actually the exception happened.

LogWindow.xaml:

This is a simple user control which is having a simple textbox:

<TextBox
AcceptsReturn="True"
TextAlignment="Left"
TextWrapping="NoWrap"
VerticalScrollBarVisibility="Visible"
x:Name="logText"
FontFamily="Courier New"
FontSize="12" IsReadOnly="True" HorizontalScrollBarVisibility="Auto" />

Logger.cs:

This class will be used to print the exception and find the stacktrace. It has an enum for the exception types viz. DEBUG,INFO,WARN,ERROR and FATAL.

This class contains a simple delegate to add text to the log window.

private delegate void AddTextDelegate(TextBox p, String text);

The static property _instance is set by the LogWindow.xaml.cs file in the load event as:

Logger._instance = logText;

So that it can print whatever exception happenes in your application.

The LogException method expects exception object as the parameter and get the exception information using the StackFrame class.

StackFrame frame = new StackFrame(true);
callerSignature =  string.Format("@{0}:{1}:{2}", frame.GetMethod(), frame.GetFileName(), frame.GetFileLineNumber());


To use this method in your catch block you just simply need to call the static method as:

Catch(Exception ex)
{
Logger.LogException(ex);
}

Please find the attachment and try to use this simple control. Thanks..
 

Up Next
    Ebook Download
    View all
    Learn
    View all