First: Excuse me if the question is wrong, I don’t really know how to describe it since I’m not a pro on coding terms.
So I have written this line in the main class:
public Log log = new Log("program.log");
Which starts a session of my logger. I then use that session to write to the log everywhere in my main class code.
But I am doing the dirty work in another class, so I want to log there too. How do I access the same “log session” from that class?
Writing MainClassName.log doesn’t work.
No,
MainClassName.logwon’t work because your variable is an instance variable. It would need to be a static variable… or you’d have to have an instance ofMainClassNamethrough which to access the instance variable.However, personally I’d advise you not to use public variables at all. I would suggest using private variables for everything other than “constants” (where that can be a readonly variable of an immutable type, not just something that .NET considers a constant) – and even then in many cases.
I would also suggest the use of something like log4net – and you’re likely to benefit from using a separate logging object per class, so that you can easily switch logs from different classes on or off (or use even more fine-grained control with severities).