using System; using System.IO; using System.Windows.Forms; namespace scheduler { /// /// Class to write a log in schedulerlog.txt file on applicaiton startup path /// public static class LogWriter { public static void LogInfo(string schedulerType, string message,string methodName) { try { string path = $"{Application.StartupPath}\\Logs\\{schedulerType}"; if(!Directory.Exists(path)) { Directory.CreateDirectory(path); } path = $"{path}\\Scheduler_Log{DateTime.Now.ToString("yyyyMMdd")}.txt"; if (!File.Exists(path)) { File.Create(path).Dispose(); } using (StreamWriter w = File.AppendText(path)) { Log(message,methodName, w); } } catch (Exception ex) { //throw; } } private static void Log(string logMessage,string methodName, TextWriter txtWriter) { try { txtWriter.Write(Environment.NewLine); txtWriter.WriteLine($"Method Name:{methodName}"); txtWriter.WriteLine($"Log Date Time:{DateTime.Now}"); txtWriter.WriteLine($"Info:{logMessage}"); txtWriter.WriteLine("-------------------------------------------------------------------------------------------------------"); } catch (Exception ex) { } } public static void DeleteOldLog(string schedulerType, int days) { // Delete files older than x days in folder string[] files = Directory.GetFiles($"{Application.StartupPath}\\Logs\\{schedulerType}"); foreach (string file in files) { FileInfo fi = new FileInfo(file); if (fi.CreationTime < DateTime.Now.AddDays(days)) { fi.Delete(); } } } } }