views:

678

answers:

2

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:

Has somebody successfully implemented log4net in a silverlight application? what i want to achieve is logging to the isolated storage. i know, there's only 1mb of storage available but this limit can be increased (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 implented a log4net to isolated storage

thanks,

Christian

A: 

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
+1  A: 

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
}
Christian