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
}
System.Diagnostics
and DebugView is good (and better) for development environment, not for production environment (I cannot access to production and use DebugView) – Tosh