forked from HomamAlamier/API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.cs
More file actions
66 lines (66 loc) · 1.77 KB
/
Copy pathLog.cs
File metadata and controls
66 lines (66 loc) · 1.77 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
namespace Logger
{
public class Log : IDisposable
{
Queue<string> queue;
Thread io_manager;
FileStream fs;
bool stop = false;
int lnc = 1;
public Log(string name, string path)
{
int x = 0;
while (File.Exists(path + name + x + ".txt"))
{
x++;
}
fs = new FileStream(path + name + x + ".txt", FileMode.OpenOrCreate);
queue = new Queue<string>();
io_manager = new Thread(io_manage);
io_manager.Start();
}
public void WriteLine(string line)
{
queue.Enqueue(line);
}
public void Dispose()
{
stop = true;
io_manager.Abort();
}
string lineNum()
{
string str = "";
int max = int.MaxValue.ToString().Length;
int val = lnc.ToString().Length;
for (int i = 0; i < max - val; i++)
{
str += "0";
}
str += lnc.ToString();
return str;
}
private void io_manage(object obj)
{
while (!stop)
{
if (queue.Count > 0)
{
string str = queue.Dequeue();
str = $"[{lineNum()}] in {DateTime.Now.ToString()} : {str}\r\n";
byte[] bt = Encoding.UTF8.GetBytes(str);
Console.WriteLine(str);
lnc++;
fs.Write(bt, 0, bt.Length);
fs.Flush();
}
Thread.Sleep(1000);
}
}
}
}