开发者

How to share an instance of a class with many classes?

I have a c# web service. When I get a new request I create a logging instance. I have many other instances of other classes to process the request and I want them to log as well. What is the best way to share logging instance without passin开发者_Python百科g it in constructors or properties ?


Often some sort of static class / property is used to share an object without needing to pass references everywhere, for example:

public class Logger
{
    static Logger()
    {
        this.Instance - new Logger();
    }

    public static Logger Instance
    {
        get;
        private set;
    }

    // Other non-static members
}

Usage:

Logger.Instance.Log();

Often this (or at least variations on this) are referred to as the singleton pattern.

There are many variations on the above, for example the following slight variation is more common than the above in logging frameworks:

public class Logger
{
    static Logger()
    {
        this.Instance = new Logger();
    }

    public static Logger Instance
    {
        get;
        private set;
    }

    public static void Log() 
    {
        Logger.Instance.Log();
    }
}

Usage:

Logger.Log();


Singleton Pattern is an anti-pattern; I can not think of many instances where it should be used or recommended, including in this instance, for logging.

How we handle logging: A logging instance is injected into all classes that need it via constructor injection. For example:

public class MyClass
{
    private readonly ILogger _logger;

    public MyClass(ILogger logger)
    {
        _logger = logger;
    }

    public void DoStuff()
    {
        _logger.Trace("DoStuff called");
    }
}

The logging instance injected can either be a new instance each time, or if you are bent on having a single instance logger it can be created once and passed through as needed.

However: I strongly recommend the first option, using DI to construct your classes using the Composition Root pattern. I also recommend using a logging framework, such as NLog or log4net. These are configurable and easy to use with emails, event viewer, files, etc. and work in multi threaded environments.

We have our own definition of ILogger which has Trace, Warn, Info, Exception, etc., and then implemented this interface using NLog. This means that we can easily change how we log by creating a new implementation of ILogger and then making a simple DI config update. This is one logging solution that has worked really well for us so far.


Singleton Pattern.

That is, a class with a private constructor. You use a public static method to create only one instance of the class and return the object.

edit: also worth noting that you will need to put this class in its own project so that it can be referenced by all classes.


Well, you could make your logging class contain static methods which do the actual writing into memory/flushing to disk. You could also look into the Singleton Pattern


You can use the Singleton Pattern to ensure that there is only a single instance of your logging object across the system.

Passing the logging object into the Constructor or as a property will actually make your code easier to test, depending on how you implement the Singleton pattern. Logging is one of those annoying cross-cutting concerns that always has trade-offs.


Usually it's not a problem to create a new instance of your logger in each class. Log4Net logger constructor takes a type as a parameter to give you better logs.


Use a static field (I assume C# supports theses). The syntax should be similar to this:

private static final Logger logger = new Logger(...);

and just use that whenever you need to log something in the class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜