-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCRapLog.cs
50 lines (45 loc) · 1.06 KB
/
CRapLog.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Reflection;
namespace RapLog
{
public class CRapLog
{
readonly bool addDate;
readonly int max;
readonly string path;
public CRapLog(string path = "",int max = 100,bool addDate = true)
{
this.path = path;
this.max = max;
this.addDate = addDate;
if(String.IsNullOrEmpty(path)) {
string name = Assembly.GetExecutingAssembly().GetName().Name + ".log";
this.path = new FileInfo(name).FullName.ToString();
}
}
public void Add(string m)
{
List<string> list = new List<string>();
if (File.Exists(path))
list = File.ReadAllLines(path).ToList();
if (addDate)
list.Insert(0, $"{DateTime.Now} {m}");
else
list.Add(m);
int count = list.Count - max;
if ((count > 0) && (max > 0))
list.RemoveRange(100, count);
File.WriteAllLines(path, list);
}
public List<string> List()
{
List<string> list = new List<string>();
if (File.Exists(path))
list = File.ReadAllLines(path).ToList();
return list;
}
}
}