0

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:

  1. 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";
        }
    
  2. How I use it in any of other projects in the same solution:

    Audit.GlobalEventOccurrence("The happened event text.");
    
  3. 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.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Eagle
  • 3
  • 3
  • You cannot share values like that. There are other ways to do it though. Write the text to file, registry, database etc. – DavidG Jul 14 '14 at 15:15
  • [Duplicate](http://stackoverflow.com/questions/15174330/how-to-share-variable-between-two-c-sharp-projects?rq=1) covers why it is not possible to share static value between multiple processes (which what you are trying to do with "from more than one project". – Alexei Levenkov Jul 14 '14 at 15:19

0 Answers0