3

First, I know about Clog, and I do not want to implement this piece. The reason? We can't maintain severeal logging 'frameworks'.

So to my question:

Is it possible to implement log4net in a Silverlight application? What I want to achieve is logging to the Isolated Storage. I know, there's only 1 MB of storage available, but this limit can be increased (the user has to accept this, I know too). By the way, please don't provide me alternatives. I do only want to know if somebody implemented a log4net to isolated storage.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Christian Casutt
  • 2,334
  • 4
  • 29
  • 38
  • "please don't provide me alternatives" - a strange stance to take! – Mitch Wheat Mar 03 '10 at 08:06
  • 1
    @Mitch: a somewhat reliable local storage with Silverlight - are there really any alternatives to isolated storage? – Peter Lillevold Mar 03 '10 at 08:21
  • @Peter: I was refering to "only want to know if somebody implented a log4net to isolated storage" – Mitch Wheat Mar 03 '10 at 08:32
  • similar to : http://stackoverflow.com/questions/1579260/silverlight-event-log-in-isolated-storage – Mitch Wheat Mar 03 '10 at 08:34
  • @Mitch, i posted this question in the hope that i get some smart input.. honestly, i didn't expect someone to respond with useless comments.. and seriously, i dont know how the community will benefit from these comments.. well, at least in the end we got a link to another post which does exactly 'provide an alternative'.. – Christian Casutt Mar 04 '10 at 05:44
  • @Christian: I guess you get what you pay for... – Mitch Wheat Mar 04 '10 at 06:12

3 Answers3

3

Here's what I've done..

using System.IO.IsolatedStorage;
using System.IO;

namespace Solution.Silverlight.Classes
{
    public static class Logging
    {
        public static void Log(string message, LOGLEVEL logLevel)
        {
            try
            {
                using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (Stream stream = new IsolatedStorageFileStream("Solution.Silverlight.log", FileMode.Append, FileAccess.Write, store))
                    {
                        StreamWriter writer = new StreamWriter(stream);
                        switch (logLevel)
                        {
                            case LOGLEVEL.INFO:
                                writer.Write(String.Format("{0:u} [INFO] {1}{2}", DateTime.Now, message,Environment.NewLine));
                                break;
                            case LOGLEVEL.WARNING:
                                writer.Write(String.Format("{0:u} [WARNING] {1}{2}", DateTime.Now, message, Environment.NewLine));
                                break;
                            case LOGLEVEL.ERROR:
                                writer.Write(String.Format("{0:u} [ERROR] {1}{2}", DateTime.Now, message, Environment.NewLine));
                                break;
                            case LOGLEVEL.FATAL:
                                writer.Write(String.Format("{0:u} [FATAL] {1}{2}", DateTime.Now, message, Environment.NewLine));
                                break;
                            default:
                                break;
                        }
                        writer.Close();
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
}


public enum LOGLEVEL
{
    INFO,
    WARNING,
    ERROR,
    FATAL
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Christian Casutt
  • 2,334
  • 4
  • 29
  • 38
  • 6
    No need for the switch statement. You can just replace the whole thing with: writer.Write(String.Format("{0:u} [{1}] {2}{3}", DateTime.Now, logLevel.ToString().ToUpper(), message,Environment.NewLine)); – Mike Chamberlain Feb 09 '11 at 00:59
  • I am afraid to use your code because my app creates multiple threads depending upon how many users are connected to it, its a web server implementation on wp8, opening and closing isolatedstorage file might not be good idea on each log call. – Mubashar Apr 01 '14 at 13:15
1

I cannot imagine that it is possible. You would have to download the log4net source and try to compile it against the silverlight runtime. I suppose it may be possible to adapt parts of the code and make it build in silverlight, but that sounds like a lot of hard work. You are probably better off rolling your own solution, or using CLog (whoops).

Henrik Söderlund
  • 4,286
  • 3
  • 26
  • 26
0

The Logging Application Block of Microsoft Enterprise Library 5.0 is now available for Silverlight. Take a look at the Silverlight Integration Pack and the corresponding demo.

Grigori Melnik
  • 4,067
  • 2
  • 34
  • 40