-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLogFiles.cs
176 lines (153 loc) · 4.8 KB
/
LogFiles.cs
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace LOG
{
/// <summary>
///
/// </summary>
public class LogFiles
{
private string logFileName = "LOG";
private string logFileExt = "log";
private ListBox lb = null;
private long maxSize = 3500000;
private TextWriter logTextWriter = null;
private string logFile = null;
/// <summary>
///
/// </summary>
/// <param name="box"></param>
/// <param name="fileName"></param>
public LogFiles(ListBox box, string fileName)
{
this.lb = box;
if (fileName.Contains("."))
{
this.logFileName = fileName.Substring(0, fileName.IndexOf("."));
this.logFileExt = fileName.Substring(fileName.IndexOf(".")+1, fileName.Length - (fileName.IndexOf(".")+1));
}
else
{ this.logFileName = fileName; }
this.CreateLog();
}
/// <summary>
///
/// </summary>
/// <param name="fileName"></param>
public LogFiles(string fileName)
{
this.logFileName = fileName;
this.CreateLog();
}
/// <summary>
/// constructor - no initialization
/// </summary>
public LogFiles() { this.CreateLog(); }
/// <summary>
///
/// </summary>
/// <param name="msg"></param>
public void WriteLog(string msg)
{
try
{
FileInfo fileInfo = new FileInfo(logFile);
if (fileInfo.Length > maxSize)
{
logTextWriter.WriteLine("CREATELOG");
logTextWriter.Flush();
logTextWriter.Close();
CreateLog();
}
logTextWriter.WriteLine(DateTime.Now.ToString("yyyyMMMdd_hh.mm.ss.ffftt") + " " + msg);
logTextWriter.Flush();
}
catch (Exception e)
{ MessageBox.Show("WRITELOG:" + e.ToString()); }
}
/// <summary>
///
/// </summary>
public void CloseLog()
{
try
{
if (logTextWriter != null)
{ logTextWriter.Close(); }
}
catch (Exception e)
{ MessageBox.Show("WRITELOG:" + e.ToString()); }
}
/// <summary>
///
/// </summary>
/// <param name="msg"></param>
public void LogListBox(string msg)
{
if (msg == null) { msg = "NULL MESSAGE"; }
int maxLines = (lb.Size.Height / lb.ItemHeight);
if (lb.Items.Count >= maxLines)
{
lb.Items.RemoveAt(0);
lb.Items.Add(msg);
}
else
{
lb.Items.Add(msg);
}
WriteLog(msg);
}
/// <summary>
///
/// </summary>
public void CleanLog()
{
string logpath = Application.StartupPath.ToString() + @"\LOG";
if (System.IO.Directory.Exists(logpath))
{
string[] files = System.IO.Directory.GetFiles(logpath);
foreach (string s in files)
{
FileInfo fi = new FileInfo(s);
if (DateTime.Compare(fi.LastWriteTime, DateTime.Today.Subtract(TimeSpan.FromDays(10))) < 0)
{
try
{
fi.Delete();
}
catch (System.IO.IOException e)
{
WriteLog("CLEAN: " + e.ToString());
}
}
}
}
}
private void CreateLog()
{
logFile = Application.StartupPath.ToString() + @"\LOG";
// if logfile directory does not exists, create it
bool blnDirectoryExists = Directory.Exists(logFile);
if (!blnDirectoryExists)
{
try
{ Directory.CreateDirectory(logFile); }
catch (Exception e)
{ MessageBox.Show("CREATELOG: " + e.ToString()); }
}
logFile += @"\" + logFileName + "_";
logFile += DateTime.Now.ToString("yyyyMMMdd_hh.mm.ss");
logFile += "." + logFileExt;
bool blnFileExists = File.Exists(logFile);
if (!blnFileExists)
{
try
{ logTextWriter = new StreamWriter(logFile); }
catch (Exception e)
{ MessageBox.Show("CREATELOG: " + e.ToString()); }
}
}
}
}