Log.cs
5.1 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
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
using System;
using System.Reflection;
using System.Diagnostics;
namespace Asa.IOModule
{
/// <summary>
/// 日志操作类
/// </summary>
internal class LogFile
{
//private string args;
private bool loop;
private System.IO.FileStream fs;
private StackTrace trace;
private StackFrame frame;
private MethodBase method;
private System.Threading.Thread tSave;
private System.Collections.Concurrent.ConcurrentQueue<string> info;
private readonly string PATH;
/// <summary>
/// 日志
/// </summary>
/// <param name="path">文件夹路径</param>
/// <param name="ip"></param>
internal LogFile(string path, string ip)
{
if (path == null) return;
//this.args = ip;
PATH = path.EndsWith("\\") ? path : path + "\\";
if (!System.IO.Directory.Exists(PATH))
System.IO.Directory.CreateDirectory(PATH);
loop = true;
tSave = new System.Threading.Thread(new System.Threading.ThreadStart(SaveLog));
info = new System.Collections.Concurrent.ConcurrentQueue<string>();
tSave.Start();
string file = string.Format("{0}{1:yyyy-MM-dd}_({2}).log", PATH, DateTime.Now, ip);
fs = System.IO.File.OpenWrite(file);
fs.Position = fs.Length;
}
/// <summary>
/// 关闭文件
/// </summary>
internal void Close()
{
loop = false;
System.Threading.Thread.Sleep(5);
if (fs != null)
fs.Close();
}
/// <summary>
/// 输出错误
/// </summary>
/// <param name="s"></param>
internal void OutError(string s)
{
if (string.IsNullOrWhiteSpace(s)) return;
trace = new StackTrace(true);
frame = trace.GetFrame(1);
string name = frame.GetFileName();
name = System.IO.Path.GetFileName(name);
string log = string.Format("[{0:HH:mm:ss.fff}] ERROR {1}({2},{3})\r\n",
DateTime.Now, name, frame.GetFileLineNumber(), frame.GetFileColumnNumber());
string[] arr = new string[trace.FrameCount - 1];
for (int i = 1; i < trace.FrameCount; i++) //0是本身Out
{
method = trace.GetFrame(i).GetMethod();
arr[arr.Length - i] = " " + method.DeclaringType.FullName + " -> " + method.ToString();
}
log += string.Join("\r\n", arr) + string.Format("\r\n {0}\r\n", s);
//byte[] array = System.Text.Encoding.UTF8.GetBytes(log);
//fs.Write(array, 0, array.Length);
info.Enqueue(log);
}
/// <summary>
/// 输出信息
/// </summary>
/// <param name="s"></param>
internal void OutInfo(string s)
{
if (string.IsNullOrWhiteSpace(s)) return;
trace = new StackTrace(true);
frame = trace.GetFrame(1);
method = trace.GetFrame(1).GetMethod();
string name = frame.GetFileName();
name = System.IO.Path.GetFileName(name);
string s1 = method.DeclaringType.FullName;
string s2 = method.Name;
string log = string.Format("[{0:HH:mm:ss.fff}] INFO {1}({2},{3}) {4} -> {5}\r\n {6}\r\n",
DateTime.Now, name, frame.GetFileLineNumber(), frame.GetFileColumnNumber(), s1, s2, s);
//byte[] array = System.Text.Encoding.UTF8.GetBytes(log);
//fs.Write(array, 0, array.Length);
info.Enqueue(log);
}
/// <summary>
/// 输出数据
/// </summary>
/// <param name="tr"></param>
/// <param name="buff"></param>
internal void OutData(string tr, byte[] buff)
{
string log = string.Format("{0:HH:mm:ss.fff} {1} ", DateTime.Now, tr);
for (int i = 0; i < buff.Length; i++)
log += buff[i].ToString("X2") + " ";
log += "\r\n";
//byte[] array = System.Text.Encoding.UTF8.GetBytes(log);
//fs.Write(array, 0, array.Length);
info.Enqueue(log);
}
/// <summary>
/// 输出数据
/// </summary>
/// <param name="s"></param>
internal void OutData(string s)
{
string log = string.Format("{0:HH:mm:ss.fff} {1}\r\n", DateTime.Now, s);
//byte[] array = System.Text.Encoding.UTF8.GetBytes(log);
//fs.Write(array, 0, array.Length);
info.Enqueue(log);
}
internal void SaveLog()
{
while (loop)
{
try
{
if (info.TryDequeue(out string result))
{
byte[] array = System.Text.Encoding.UTF8.GetBytes(result);
fs.Write(array, 0, array.Length);
}
}
catch (Exception)
{ }
System.Threading.Thread.Sleep(2);
}
}
}
}