views:

3421

answers:

6

Now that Silverlight 2 has finally shipped. I'm wondering if anyone has put together any logging frameworks for it, maybe something like enterprise library logging or log4net? I'm interesting in something that can perform tracing client side and also log messages to the server.

So far the only project I have found is Clog on CodeProject. Has anyone used this? What were your thoughts on it?

+3  A: 

I am about to delve into something similar myself for a product we've written. I'm considering using PostSharp for Silverlight to add client-side logging as an aspect.

I have used the NLog project with great success before under the full .NET Framework and the Compact Framework so I'll most likely take the existing framework code and add some logging targets:

  • A standard System.Diagnostics target to enable capturing using DebugView, etc.
  • An asynchronous Web service target similar to the one in NLog.
  • An isolated storage target with deferred transfer to server semantics.

I've briefly looked at Clog and it seems to suffer from one major flaw - it can't log a connection failure. So assuming your Web server is online all the time, yes it will work, but when problems occur upstream or on the server itself, the logging data is all lost and might even crash your application.

Craig Nicholson
A: 

I have ended up writing a new logging framework from scratch that addresses this flaw. I created a local queue that will get the log/trace messages and then do the filter and send them to the server. The queue then will be backed by Isolated Storage so even if the client goes off line permanently for that session the messages will be sent when it is back online.

Aaron Weiker
+3  A: 

Hi,

if you just want to output debug messages to the console. You could use the Browser's console.log mechanism. I coded a extension method for that. You can find on my blog.

Rene Schulte
A: 

I'm using a JavaScript windows and making it scriptable in Silverlight. For "production", I can turn this window off but still save the log lines into memory, then if something goes wrong, send that off to the server. That way I get the best of both worlds - Simple, real-time logging on the client for debugging and logs for remote post-mortem situations that users may encounter.

Robert Fraser
A: 

Dear Aaron Weiker,

Is it possible that you share your Framework solution with us? I currently run into the same problem as described above: I have an application which should log clientside and, if an internetconnection is available, send the Log to the Server/Database.

You would help me a lot by sharing your solution.

Thanks in advance.

Ardon.

Ardon
I'm not sure if it's still in here, but the code for this was in this solution. http://code.msdn.microsoft.com/RCE
Aaron Weiker
A: 

If you're willing to take your astronaut's helmet off for a minute, below is a lightweight logger I've written for Silverlight, for client-side logging (for use mainly with WCF operations but could be for any errors).

It was originally used in Monotouch for iPhone apps, and has been adapted for IsolateStorage. You can use the Read method to display in a textbox if needed. Tested in SL4.

/// <summary>
/// A lightweight logging class for Silverlight.
/// </summary>
public class Log
{
    /// <summary>
    /// The log file to write to. Defaults to "dd-mm-yyyy.log" e.g. "13-01-2010.log"
    /// </summary>
    public static string LogFilename { get; set; }

    /// <summary>
    /// Whether to appendthe calling method to the start of the log line.
    /// </summary>
    public static bool UseStackFrame { get; set; }

    static Log()
    {
        LogFilename = string.Format("{0}.log", DateTime.Today.ToString("dd-MM-yyyy"));
        UseStackFrame = false;
    }

    /// <summary>
    /// Reads the entire log file, or returns an empty string if it doesn't exist yet.
    /// </summary>
    /// <returns></returns>
    public static string ReadLog()
    {
        string result = "";
        IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForSite();

        if (storage.FileExists(LogFilename))
        {
            try
            {
                using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(LogFilename,FileMode.OpenOrCreate,storage))
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        result = reader.ReadToEnd();
                    }
                }
            }
            catch (IOException)
            {
                // Ignore
            }
        }

        return result;
    }

    /// <summary>
    /// Writes information (not errors) to the log file.
    /// </summary>
    /// <param name="format">A format string</param>
    /// <param name="args">Any arguments for the format string.</param>
    public static void Info(string format, params object[] args)
    {
        WriteLine(LoggingLevel.Info, format, args);
    }

    /// <summary>
    /// Writes a warning (non critical error) to the log file
    /// </summary>
    /// <param name="format">A format string</param>
    /// <param name="args">Any arguments for the format string.</param>
    public static void Warn(string format, params object[] args)
    {
        WriteLine(LoggingLevel.Warn, format, args);
    }

    /// <summary>
    /// Writes a critical or fatal error to the log file.
    /// </summary>
    /// <param name="format">A format string</param>
    /// <param name="args">Any arguments for the format string.</param>
    public static void Fatal(string format, params object[] args)
    {
        WriteLine(LoggingLevel.Fatal, format, args);
    }

    /// <summary>
    /// Writes the args to the default logging output using the format provided.
    /// </summary>
    public static void WriteLine(LoggingLevel level, string format, params object[] args)
    {
        string message = string.Format(format, args);

        // Optionally show the calling method
        if (UseStackFrame)
        {
            var name = new StackFrame(2, false).GetMethod().Name;

            string prefix = string.Format("[{0} - {1}] ", level, name);
            message = string.Format(prefix + format, args);
        }

        Debug.WriteLine(message);
        WriteToFile(message);
    }

    /// <summary>
    /// Writes a line to the current log file.
    /// </summary>
    /// <param name="message"></param>
    private static void WriteToFile(string message)
    {
        try
        {
            IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForSite();
            bool b = storage.FileExists(LogFilename);

            using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(LogFilename,FileMode.Append,storage))
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    writer.WriteLine("[{0}] {1}", DateTime.UtcNow.ToString(), message);
                }
            }
        }
        catch (IOException)
        {
            // throw new Catch22Exception();
        }
    }
}

/// <summary>
/// The type of error to log.
/// </summary>
public enum LoggingLevel
{
    /// <summary>
    /// A message containing information only.
    /// </summary>
    Info,
    /// <summary>
    /// A non-critical warning error message.
    /// </summary>
    Warn,
    /// <summary>
    /// A fatal error message.
    /// </summary>
    Fatal
}
Chris S