I have a non-static class that wraps around Log4Net. This is really handy as it links up to a further application I have that displays logs of different processes that occur in a variety of software that I write (to allow me to see where errors occur, batch processes fail etc.)
This is all well and good, but if I want to log an entire process, I have to do a series of things:
This means that if a process runs outside of the class I have to mess about passing the Logging object around which is a pain and looks messy.
How can I essentially just declare the object in such a way that It is globally accessible by all the classes......and making it static isn't really an option, I don't think.
.....This could be a waste of time but I like opinions.
Why do you believe that making "it" static isn't an option? I suspect you're confusing the ideas of a static class and a static variable which maintains a reference to an instance of a non-static class.
public class Foo
{
// Instantiate here, or somewhere else
private static LogWrapper logWrapper = new LogWrapper(...);
public static LogWrapper LogWrapper { get { return logWrapper; } }
}
Just because the class is non-static doesn't mean you can't have a singleton instance of it which is globally available. Normally not such a good idea, but not too bad for logging.
If you do go down the singleton route, you may want to consider reading my article on implementing a singleton in C#.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With