Silverlight Logging framework and/or best practices
Asked Answered
C

6

25

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?

Channel answered 23/10, 2008 at 6:29 Comment(0)
S
4

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.

Slavin answered 27/10, 2008 at 22:57 Comment(1)
System.Diagnostics and DebugView is good (and better) for development environment, not for production environment (I cannot access to production and use DebugView)Tosh
S
14

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
}
Solano answered 7/7, 2010 at 11:8 Comment(1)
Read method to display in a textbox ? another alternatives using IsolatedStorageFile ?Tosh
B
6

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.

    // http://kodierer.blogspot.com.es/2009/05/silverlight-logging-extension-method.html
    public static string Log(string message)
    {
        var msgLog = "";
        try
        {

            HtmlWindow window = HtmlPage.Window;

            //only log if a console is available
            var isConsoleAvailable = (bool)window.Eval("typeof(console) != 'undefined' && typeof(console.log) != 'undefined'");

            if (!isConsoleAvailable) return "isConsoleAvailable " + isConsoleAvailable;

            var createLogFunction = (bool)window.Eval("typeof(ssplog) == 'undefined'");
            if (createLogFunction)
            {
                // Load the logging function into global scope:
                string logFunction = @"function ssplog(msg) { console.log(msg); }";
                string code = string.Format(@"if(window.execScript) {{ window.execScript('{0}'); }} else {{ eval.call(null, '{0}'); }}", logFunction);
                window.Eval(code);
            }

            // Prepare the message
            DateTime dateTime = DateTime.Now;
            string output = string.Format("{0} - {1} - {2}", dateTime.ToString("u"), "DEBUG", message);

            // Invoke the logging function:
            var logger = window.Eval("ssplog") as ScriptObject;
            logger.InvokeSelf(output);
        }
        catch (Exception ex)
        {
            msgLog = "Error Log " + ex.Message;
        }
        return msgLog;

    }
Blanchard answered 25/5, 2009 at 6:40 Comment(0)
S
4

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.

Slavin answered 27/10, 2008 at 22:57 Comment(1)
System.Diagnostics and DebugView is good (and better) for development environment, not for production environment (I cannot access to production and use DebugView)Tosh
H
2

You can use this one too: http://silverlightlogging.codeplex.com/

Homograft answered 24/11, 2010 at 9:4 Comment(0)
C
0

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.

Channel answered 3/11, 2008 at 15:41 Comment(0)
C
0

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.

Casey answered 14/9, 2009 at 3:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.