I have a solution consisting of a windows form application, windows service application, another windows form that simulates my service and a library full of classes used in 3 projects of the solution.
I have set a logging class with a public static method which correctly works, both the service simulator form and my main windows form can call public static method of this class and write logs to a single logfile.
I aim to put a textbox to monitor latest events in almost realtime so Since I thought it won't be safe to read the logfile when another method is writing on it (if I am right) I declared a public static string in my logging class that easly can be updated by methods of the same class (could be used from outside). I am able to use this scenario only while I am reading this public static string variable from one of the forms (main project) as soon as I log events from other form (service simulator) it gets erased (=null). Below please find examples:
Logging Method:
public class Audit { private static string LastLogString; public static string LastLogsString { get { return LastLogString; } set { LastLogString = value; } } public static void GlobalEventOccurrence(string EVENT_TEXT) { streamwriter strw = new streamwriter(THEPATH); strw.writeline(datetime.now.tostring() + EVENT_TEXT); strw.close(); LastLogString = LastLogString + "EVENT_TEXT" + "\r\n"; }
How I use it in any of other projects in the same solution:
Audit.GlobalEventOccurrence("The happened event text.");
How I wanted to retrieve variable content:
textbox1.text = Audit.LastLogsString;
Just for clarification, it works when I use section 3 in a single project, but as soon as I use line 2 by another projects, value of the variable resets to null, then logging function works.