Skip to content

Instantly share code, notes, and snippets.

@chaliy
Created October 17, 2010 17:00
Show Gist options
  • Save chaliy/631025 to your computer and use it in GitHub Desktop.
Save chaliy/631025 to your computer and use it in GitHub Desktop.
Small Silverlight wrapper for FireBug or anything else that support console.
using System;
using System.Windows.Browser;
/// <summary>
/// Small wrapper for FireBug, or anything else that support console.
/// BTW IE9 supports Log, Info and Error.
/// </summary>
/// <example>
/// // Time feature of the FireBug
/// using (HtmlConsole.Time("Create and set RootVisual"))
/// {
/// this.RootVisual = new MainPage();
/// }
/// </example>
public static class HtmlConsole
{
public static void Log(string msg)
{
Write("log", msg);
}
public static void Debug(string msg)
{
Write("debug", msg);
}
public static void Info(string msg)
{
Write("info", msg);
}
public static void Warn(string msg)
{
Write("warn", msg);
}
public static void Error(string msg)
{
Write("error", msg);
}
public static IDisposable Time(string msg)
{
HtmlPage.Window.Eval("if (console) console.time('" + EncodeText(msg) + "')");
return new ActionDisposable(() => HtmlPage.Window.Eval("if (console) console.timeEnd('" + EncodeText(msg) + "')"));
}
private class ActionDisposable : IDisposable
{
private readonly Action _a;
public ActionDisposable(Action a)
{
_a = a;
}
public void Dispose()
{
if (_a != null) _a();
}
}
private static void Write(string sev, string msg)
{
HtmlPage.Window.Eval("if (console) console." + sev + "('" + EncodeText(msg) + "')");
}
private static string EncodeText(string msg)
{
return msg.Replace("'", "''");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment