0

Sorry it's possibly a duplicate but in the other static interface they mention generics which I am not using.

I wanted a quick and simple logger for my app I though I could avoid a singleton using a static element. I would have like to keep the interface so that later I could change to another logging package. The interface being for me (I may be wrong here) a way to implement a "facade" feature.

I have the feeling I am overlooking something. A gentle push in the right direction would be greatly appreciated. (Hope it's a bit clearer)

public interface Ilogger
{
    void Log(string data, out DateTime datetime, out string uid);
}

public class Logger : Ilogger
{
    private static TraceSource AppTrace = new TraceSource("RD", SourceLevels.All);

    static Logger ()
    {
       AppTrace.Listeners.Clear();
       AppTrace.Listeners.Add(new DelimitedListTraceListener("RD.log"));
    }

    static void Log(string data, out DateTime datetime, out string uid)
    {
        datetime = DateTime.Now;
        uid = Guid.NewGuid().ToString();

        AppTrace.TraceInformation(datetime + ";" + uid + ";" + data);
    }
}
2
  • 1
    It's not really clear what you're trying to achieve, or where "static interfaces" would come into play... Please edit your question to clarify it. Commented Aug 4, 2011 at 16:35
  • You guys are quick on the trigger! Cheers Commented Aug 4, 2011 at 16:44

2 Answers 2

1

You want to combine a singleton with a facade. Sort of a service locator. i.e. create a singleton that got the same methods as your interface and then assign the interface to the facade as the singleton.

I've blogged about it.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Jgauffin, that's what I wrote at first. but then thought it would simplified the situation if I just used a static method. So there is no others (simpler) way to do it ?
1

C# does not support static inheritance or static interface implementation. static variables are just a form of a Singleton either way you look at it - just go with a Singleton or better use a DI container to inject the logger dependency.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.