css_styles/default/default_style.css -
uploads/482/TraceWindow.zip - uploads/482/TraceWindow.zip
You've probably discovered that we don't publish a debug log on the Silverlight
client (we still publish the debug log on the server side by default). There's a
great reason: you're not allowed to write to the file system in Silverlight ...
except for Isolated Storage ... and you don't want to blow out IsoStore with log
data.
Wouldn't it be convenient to be able to log client activity and see it in
Silverlight during development? Maybe batch capture it and ship up a batch of messages to
the server periodically?
It's easier than you might think. Here's a code snippet to do the job:
private
void SubscribeToTracePublisher()
{
_messages = new ObservableCollection<TraceMessage>();
_subscriber = new TraceSubscriber();
_subscriber.Publish +=
NotificationHandler;
_subscriber.StartSubscription();
// Stuff
}
private
void NotificationHandler(object sender, PublishEventArgs e) {
_messages.Add(e.TraceMessage);
// Stuff
}
}
The DevForce TracePublisher is "waiting" for your call. We hide most of the
hook up. When you wire your handler to the subcriber object's "Publish" event
[terrible name], DevForce attaches it to the TracePublisher and _now_ the
publisher starts publishing (it won't if no one is listening). I've attached an example UserControl called TraceWindow that shows this in context. I drop it into my application view with a little XAML like this: <local:TraceWindow Height="200" Width="900" Margin="0,0,20,0"/> When I run the app, I'll see an expander button. I click that to open and I get this:
file:///C:%5CUsers%5CWard%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml"> The "A" section is how DevForce starts up on the client. Actually, on the client, you'll never see anything else from DevForce because all of the real action moves to the server.
But notice "B". That's the method that recorded something to the trace log. The message "Clicked Add" was what I said when I posted a trace message. In this example, I've tied into the Add button handler like so:
private void AddNew_Click(object sender, RoutedEventArgs e) { IdeaBlade.Core.TraceFns.WriteLine("Clicked Add"); // more stuff }
You may choose to use DebugFns instead if you don't want to see messages in Release Mode. Or you can wrap the code in compiler directives to yank the whole business out of your production version.
I'm sure you can get more creative. Enjoy! file:///C:%5CUsers%5CWard%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml -
|