-
Notifications
You must be signed in to change notification settings - Fork 2
/
LogFiles.cs
152 lines (130 loc) · 4.52 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
using System;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace LOG
{
/// <summary>
/// version 2009.12.29
/// </summary>
public class LogFiles
{
private string logFileName = "LOG";
private string logFileExt = "log";
private string logFile = null;
private ListBox lb = null;
private TextWriter logTextWriter = null;
private ushort ctr = 0;
internal void CreateLog(string fileName, ListBox box)
{
if (box != null) { 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; }
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
{
TextWriter tw = new StreamWriter(logFile);
logTextWriter = TextWriter.Synchronized(tw);
}
catch (Exception e)
{ MessageBox.Show("CREATELOG: " + e.ToString()); }
}
}
/// <summary>
///
/// </summary>
internal void CloseLog()
{
try
{
if (logTextWriter != null)
{ logTextWriter.Close(); }
}
catch (Exception e)
{ MessageBox.Show("WRITELOG:" + e.ToString()); }
}
/// <summary>
///
/// </summary>
/// <param name="msg"></param>
internal void WriteLog(string msg)
{
if (msg == null) { msg = "NULL MESSAGE"; }
try
{
if (lb != null)
{
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); }
}
Console.WriteLine(msg);
if (ctr > 65000)
{
logTextWriter.WriteLine("CREATELOG triggered by line count");
logTextWriter.Flush();
logTextWriter.Close();
CreateLog(this.logFileName + "." + this.logFileExt, lb);
ctr = 0;
}
logTextWriter.WriteLine(DateTime.Now.ToString("yyyyMMdd\tHH:mm:ss.fff\t") + " " + msg);
logTextWriter.Flush();
ctr++;
}
catch (Exception e)
{ logTextWriter.WriteLine("WRITELOG:" + e.ToString()); }
}
/// <summary>
/// delete all log files older than daysSaved
/// </summary>
internal void CleanLog(int daysSaved)
{
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(daysSaved))) < 0)
{
try
{
fi.Delete();
}
catch (System.IO.IOException e)
{
// WriteLog("CLEAN: " + e.ToString());
MessageBox.Show("CLEAN: " + e.ToString());
}
}
}
}
}
}
}