1

How do I use Trace class to store tracing info in a file in a Silverlight client code? In server side code I change config to have tracing info directed into a file. What should I do to make tracing written to a file on the client side?

I dont use Application Blocks and I want use Trace.WriteLine.

Boppity Bop
  • 9,613
  • 13
  • 72
  • 151

1 Answers1

4

As you may know, Silverlight is running in the "almighty" sandbox. Due to this, you won't have direct file access. To solve this problem, you can write a file into the isolated storage of your application.

Side note: As far as I know, Trace.WriteLine doesn't exist in Silverlight?

To do so, write a class that represents your Trace, and implements a WriteLine method:

public static class SilverlightTrace
{
    public static void WriteLine(string message)
    {
        try
        {
            if (IsolatedStorageFile.IsEnabled)
            {
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    // Create trace file, if it doesn't exist
                    if(!store.FileExists("YourFileName.txt"))
                    {
                        var stream = store.CreateFile("YourFileName.txt");
                        stream.Close();
                    }

                    using (var writer = new StreamWriter(
                                        store.OpenFile("YourFileName.txt",
                                                       FileMode.Append,
                                                       FileAccess.Write)))
                    {
                        writer.WriteLine(message);
                        writer.Close();
                    }
                }
            }      
        }
        catch(Exception e)
        {
            // Add some error handling here
        }
    }
}
Stefan Over
  • 5,851
  • 2
  • 35
  • 61
  • 1
    yeah you are right. Trace doesnt exist.. I kinda missed it.. Isolated storage that what i meant by writing a file.. you need to remember that isolated storage quote is tiny by default. you need to extend it and keep checking and roll over if necessary. – Boppity Bop Jul 31 '15 at 15:26