Wednesday, August 25, 2010

Flex Logging Framework for C# (Silverlight Compatible)

I've taken some time and created a logging framework in C# that is very similar (in fact, uses close to identical logic in some cases) to that of flex. It's super flexible and easy to use.

You'll find the source at: http://github.com/mbolt35/OpenSource/tree/master/silverlight

The Bolt.AS3.Logging package contains the necessary classes/interfaces and logging targets. I've also created a very basic example here:

http://github.com/mbolt35/OpenSource/raw/master/blogspot/silverlight/Bolt/CSharp/LoggingExample.cs

Which generates the following output:

[INFO] Bolt.CSharp.LoggingExample This is a Logging Test
[DEBUG] Bolt.CSharp.LoggingExample You can log like this: Test, 1, 2, 3
[WARN] Bolt.CSharp.LoggingExample But it's preferable to use this: Test, 1, 2, 3
[ERROR] Bolt.CSharp.LoggingExample You can have multiple parameters in your log: True - that are any type (object) - Test, 1, 2, 3,
and multiline is supported: 25
[FATAL] Bolt.CSharp.LoggingExample The End...


One thing that I find extremely useful about the logging is the way in which a logging instance is defined:
/// <summary>
/// Creates a read-only static logger instance with the 
/// fully-qualified class name as the category.
/// </summary>
private static readonly ILogger log = Log.GetLogger(
    MethodBase.GetCurrentMethod().DeclaringType.FullName);

Note that the MethodBase is in the System.Reflections package.

This line of code is great because it can be templated and added to any new class you create, and without ever changing the code, the log category will be set to the fully-qualified class name of the object in which it's created!

Hope you all enjoy this code, and find it useful!

Monday, August 16, 2010

Silverlight Vector<T>...

I spent some time over the weekend porting the AS3 Vector.<T> class to C# for use with Silverlight. It uses System.Collections.Generic.List<T> as a backbone, so it leverages a lot of the existing functionality, but the main purpose is to keep the same method signatures as AS3. 

Source:
hhttp://github.com/mbolt35/OpenSource/blob/master/silverlight/Bolt/CSharp/Collections/Vector.cs

This class would most likely assist in porting large AS3 projects to Silverlight, depending on how much Vector.<T> was used in your AS3 application of course :)

Code is written under the Apache License - Enjoy!

Saturday, August 7, 2010

PowerFlasher SOS Max Logging for Silverlight...

If you are not familiar with SOS Max, it's a socket output server that is used to display logging messages sent via a client application for debugging. The beauty behind this concept is that it supports any language with socket support, and since SOS Max is written in Java, it's cross platform. At Electrotank, we use it for most all our client side projects in AS3. To do this, we use an SOSLogTarget class written by Sönke Rohde. You  can find more information for the AS3 version at:

http://soenkerohde.com/2008/08/sos-logging-target/

I was piddling around on Twitter this morning and saw that Jobe Makar had tweeted that he was going to have a go at Silverlight. The next few tweets were his remarks on some annoying issues with simply logging output to the Console in Firefox (which, off-topic, he found the solution here: http://bit.ly/9YpZF7 in the bottom post). His issues oddly inspired me to check out Silverlight myself, as I have some experience with C# and XNA, so it was coming one way or another -- why not today?

Immediately, I noted that Jobe's problem was, in fact, just as annoying as he said. So, to solve this issue on a larger scale, I wrote a very basic SOS Max logging target that works with the Silverlight C# API. The full source (with an example) can be found here:

http://github.com/mbolt35/OpenSource/raw/master/blogspot/silverlight/Bolt/CSharp/SOSLog.cs

However, because Silverlight has some pretty annoying security policies when it comes to sockets, you have to run the Silverlight application "Out of Browser." To do this:
  • In the Solution Explorer, right-click your application and select Properties.
  • Select the Silverlight tab and check the option that says "Enable running application out of the browser"

  • Then click the now enabled "Out-of-Browser Settings..." button.
  • Check the option that says "Require elevated trust when running outside the browser."
 
  • Press "OK"
    Now you should be all set to go... 



    Hope you find this useful - The source is written under the Apache License, so you can feel free to use it how you want. 

    Matt Bolt