I am writing a Windows service (C #) that performs a task repeatedly. I use a thread to fulfill my requirements. Now I need to save the log file in which the logs are stored relative to the operation.
My service class is as follows
public partial class CPEService : ServiceBase { static ServiceBot bot = new ServiceBot(); static ProgramLog logger = new ProgramLog();
I have a separate call to the "ProgramLog" class to handle all log-related operations. This is that class.
public class ProgramLog { string fileName = "";//Global variable to store file name #region method to handle usual log records public void log(string text)//create normal Log text { fileName = "Log\\" + DateTime.Now.Date.ToString("d").Replace('/', '_') + ".txt"; if (File.Exists(AppDomain.CurrentDomain.BaseDirectory+fileName)) { using (FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + fileName, FileMode.Append)) using (TextWriter tw = new StreamWriter(fs)) { tw.WriteLine(text); tw.Flush(); tw.Close(); fs.Close(); } } else { createFolder(); log(text); } } #endregion #region log Error record public void logError(string text, string className,int LineNumber, string Stacktrace)//create error text { fileName = "Log\\" + DateTime.Now.Date.ToString("d").Replace('/', '_') + ".txt"; if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + fileName)) { using (FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + fileName, FileMode.Append)) using (TextWriter tw = new StreamWriter(fs)) { tw.WriteLine("**************************ERROR****************************"); tw.WriteLine(text); tw.WriteLine("In Class :{0}", className); tw.WriteLine("In Line :{0}", LineNumber); tw.WriteLine("ERROR :{0}",Stacktrace); tw.WriteLine("***********************************************************"); } } else { createFolder(); logError(text,className,LineNumber,Stacktrace); } } #endregion #region create folder to store log files public void createFolder()//create a folder for Log files { try { if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "Log")) { string folderName = "Log"; Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + folderName); FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + fileName, FileMode.Create); StreamWriter sr = new StreamWriter(fs); sr.Flush(); sr.Close(); fs.Close(); } else { FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + fileName, FileMode.Create); StreamWriter sr = new StreamWriter(fs); sr.Flush(); sr.Close(); fs.Close(); } } catch (Exception e) { Console.WriteLine(e.StackTrace); } } #endregion }
According to the above class, when I start the service, it needs to create a call to the Journal folder, where it does not exist, then it creates a text file inside this folder and finally starts creating journal entries.
Although the thread is working correctly, it never touches the "ProgramLog" methods. I checked by directly calling the "loopTrough" method. then his working fine.
Please help me resolve this error.
thanks
source share