Reputation: 2256
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 ?
Upvotes: 0
Views: 250
Reputation: 27944
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();
}
Upvotes: 1
Reputation: 867
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)
{
...
Upvotes: 0