-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogFile.cs
More file actions
109 lines (107 loc) · 3.36 KB
/
LogFile.cs
File metadata and controls
109 lines (107 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//////////////////////////////////////////////////////////////////////
///Class and functions to write log file
/// Include functions:
/// CheckLogFolder - Check if the folder Logs is present, remove old logs
/// StartLog - Start the log file stream
/// WriteLog - Write a line to the log file
/// EndLog - Close the log file stream
///
///--version 3.0.0.1
///Becket Hui 2025/8
///--version 2.0.0.1
///Becket Hui 2024/7
///--version 1.0.1.1
///Becket Hui 2022/9
///
//////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms.VisualStyles;
using System.Windows.Interop;
using System.Windows.Media;
using VMS.TPS.Common.Model.API;
using static System.Net.WebRequestMethods;
namespace batchOptimization
{
internal class LogFile
{
private bool dirExist;
private string fileDir;
private string fileFullPath;
private StreamWriter logWriter;
private bool logExist;
public RunTask CheckLogFolder(string dirPath)
// Check if log file directory exists
{
fileDir = Path.Combine(dirPath, "Logs");
if (Directory.Exists(fileDir))
{
dirExist = true;
// Remove old logs
string[] currLogFiles = Directory.GetFiles(fileDir, "*_log.txt");
DateTime oneMonthAgo = DateTime.Now.AddMonths(-1);
foreach (string logFile in currLogFiles)
{
FileInfo logFileInfo = new FileInfo(logFile);
if (logFileInfo.CreationTime < oneMonthAgo)
{
logFileInfo.Delete();
}
}
return new RunTask(true, string.Format("Log will be saved."));
}
else
{
dirExist = false;
return new RunTask(false, string.Format("Logs folder unavailable, no log will be saved."));
}
}
public void StartLog(string usrId)
// Start the log file stream
{
if (dirExist)
{
string fileNm = string.Format("{0}_{1}_log.txt", DateTime.Now.ToString("yyyyMMddHHmmss"), usrId);
fileFullPath = Path.Combine(fileDir, fileNm);
try
{
logWriter = new StreamWriter(fileFullPath);
logWriter.AutoFlush = true;
logExist = true;
}
catch (Exception ex)
{
logExist = false;
}
}
else
{
logExist = false;
}
}
public void WriteLog(string txt)
// Write a line to the log file
{
if (logExist)
{
logWriter.WriteLine(string.Format("{0}: {1}", DateTime.Now.ToString("M/d/yy hh:mm:ss tt"), txt));
}
}
public void EndLog()
// Close the log file stream
{
if (logExist)
{
logWriter.Close();
logExist = false;
}
}
}
}