Quick Logging

    It was necessary here to track the current state of a frequently changing element. There was a suspicion that someone was resetting it. Immediately dismissed the options for writing logging through the file system: Silverlight is not allowed to write wherever he wants. Therefore, a small extension method was written that outputs debugging information to the browser console.

     public static class Extensions
      {
        public static void Log(this object obj)
        {
          HtmlWindow window = HtmlPage.Window;
          object isConsoleAvailable =
             window.Eval("typeof(console) != 'undefined' && typeof(console.log) != 'undefined'");

          if ( (isConsoleAvailable is bool) && (bool)isConsoleAvailable)
          {
            var console = (window.Eval("console.log") as ScriptObject);
            if (console != null)
            {
              console.InvokeSelf(obj);
            }
          }
        }
      }

    * This source code was highlighted with Source Code Highlighter.


    It is called very simply: on the object you just need to call the Log () method.
    I usually prepare a string and log it.

    string message = string.Format("{0}: object {1} ScrollPosition = {2}",DateTime.Now.ToShortTimeString(),((ItemsControl)d).Name,e.NewValue.ToString());
          message.Log();

    * This source code was highlighted with Source Code Highlighter.


    This is how it looks in FireFox (you need FireBug to view it):


    and here it is in IE8 (you just need to press F12 and go to the Script tab):


    This method is suitable if you need to see something in the project when debugging. For long-term logging it is better to use Clog of course . But it does not support Silverlight 3.


    Also popular now: