Silverlight: Log4Net to Isolated Storage
Asked Answered
F

3

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.

Forelimb answered 3/3, 2010 at 8:1 Comment(6)
"please don't provide me alternatives" - a strange stance to take!Masakomasan
@Mitch: a somewhat reliable local storage with Silverlight - are there really any alternatives to isolated storage?Candelariacandelario
@Peter: I was refering to "only want to know if somebody implented a log4net to isolated storage"Masakomasan
similar to : #1579760Masakomasan
@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'..Forelimb
@Christian: I guess you get what you pay for...Masakomasan
P
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).

Pathic answered 3/3, 2010 at 15:55 Comment(0)
F
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
}
Forelimb answered 7/3, 2010 at 9:4 Comment(2)
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));Westernmost
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.Eisenberg
P
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).

Pathic answered 3/3, 2010 at 15:55 Comment(0)
P
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.

Parsimonious answered 9/4, 2011 at 18:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.