0

I have a method as

 private string sLogFormat;
        private string sErrorTime;

        public void CreateLogFile(string path, string msg)
        { 
            //sLogFormat used to create log files format :
            // dd/mm/yyyy hh:mm:ss AM/PM ==> Log Message
            sLogFormat = DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString() + " ==> ";

            //this variable used to create log filename format "
            //for example filename : ErrorLogYYYYMMDD
            string sYear = DateTime.Now.Year.ToString();
            string sMonth = DateTime.Now.Month.ToString();
            string sDay = DateTime.Now.Day.ToString();
            sErrorTime = sYear + sMonth + sDay;

            StreamWriter sw = new StreamWriter(path + sErrorTime, true);
            sw.WriteLine(sLogFormat + msg);
            sw.Flush();
            sw.Close();

        }

Another method run() that triggers somewhere the Onchanged handler(it is triggered when folder content is changed more precisely) here

 public static void Run()
        {
            // Create a new FileSystemWatcher and set its properties.
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = "C:/model_RCCMREC";

            /* Watch for changes in LastAccess and LastWrite times, and
               the renaming of files or directories. */
            watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
            //   watch wav files.
            watcher.Filter = "*.wav";

            // Add event handlers. 
            watcher.Created += new FileSystemEventHandler(OnChanged);

            // Begin watching.
            watcher.EnableRaisingEvents = true;
        }

and the OnChanged handler as

public static  void OnChanged(object source, FileSystemEventArgs e)
        {  
            try
            { 

                //access last folder(newly created one is suposedly at last position)
                var directory = new DirectoryInfo("C:/model_RCCMREC");
                var myFile = (from f in directory.GetFiles() orderby f.LastWriteTime descending select f).First();

                //2.Split the file name.
            }


            catch
            {
//I WANT TO ACCESS THE METHOD CreateLogFile(x,y) HERE

            }
        }

I cannot access the method CreateLogFile() from the OnChanged handler normally.How can i access it ?

8
  • Is the OnChanged handler in the same class as the CreateLogfile function? If so you can just make it non-static. Otherwise, you need to give it a class instance somehow... which would require seeing more of your code Commented Oct 25, 2013 at 8:46
  • Well which instance of the class do you want to call the method on? Different instances might have different log formats etc. Do you need your OnChanged handler to be a static method? Commented Oct 25, 2013 at 8:46
  • why is OnChanged static? Commented Oct 25, 2013 at 8:48
  • 1
    I would recommend avoiding statics where possible. Try to make Run non-static. If thats not possible, create a class, to encaplusate the logic of the functionality, and use new FooClass() . Commented Oct 25, 2013 at 8:54
  • 1
    Why don't you just make your logfilehandling static? Commented Oct 25, 2013 at 8:55

2 Answers 2

1

It seems that the OnChanged should be non static. Normally only a specific instance will call the OnChanged. This because the knolage that something is changes is encaptulated in the instance it self. Remove the static from your OnChanged, and you are good to go.

Or if you can't/do not want to make your OnChanged non static, you can make your CreateLogFile static, I do not see any reason to decare your variable outside of your method:

    public static void CreateLogFile(string path, string msg)
    { 
        string sLogFormat;
        string sErrorTime;

        //sLogFormat used to create log files format :
        // dd/mm/yyyy hh:mm:ss AM/PM ==> Log Message
        sLogFormat = DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString() + " ==> ";

        //this variable used to create log filename format "
        //for example filename : ErrorLogYYYYMMDD
        string sYear = DateTime.Now.Year.ToString();
        string sMonth = DateTime.Now.Month.ToString();
        string sDay = DateTime.Now.Day.ToString();
        sErrorTime = sYear + sMonth + sDay;

        StreamWriter sw = new StreamWriter(path + sErrorTime, true);
        sw.WriteLine(sLogFormat + msg);
        sw.Flush();
        sw.Close();

    }
Sign up to request clarification or add additional context in comments.

Comments

0

You got two ways:

1- If your Logger class is non-static, create a new logger object using YourLoggerClass log = new YourLoggerClass(); And do the voids

2- Make your Logger class static which allows you to easily use it anywhere:

private static string sLogFormat;
private static string sErrorTime;

public static void CreateLogFile(string path, string msg)
{
     ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.