Log.cs
6.47 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace XingYe_ACS.Common
{
public class Log
{
//软件本身异常信息,便于程序调试
public string Path_Error { get; set; }
//日志信息:记录详细的信息流过程
public string Path_Log { get; set; }
public string Path_BusinessErr { get; set; }
//接口日志
public string Path_WebApi { get; set; }
public string Path_AgvVoltage { get; set; }
public Log()
{
Path_Error = Application.StartupPath + "\\ErrorMessage";
Path_Log = Application.StartupPath + "\\Log";
Path_BusinessErr = Application.StartupPath + "\\BusinessErr";
Path_WebApi = Application.StartupPath + "\\WebApi";
Path_AgvVoltage = Application.StartupPath + "\\AgvVoltage";
}
///<summary>
/// 目录检查
/// </summary>
public void DirectoryCheck()
{
//异常信息
if (!Directory.Exists(Path_Error)) Directory.CreateDirectory(Path_Error);
//Log信息
if (!Directory.Exists(Path_Log)) Directory.CreateDirectory(Path_Log);
//Log信息
if (!Directory.Exists(Path_BusinessErr)) Directory.CreateDirectory(Path_BusinessErr);
//WebApi信息
if (!Directory.Exists(Path_WebApi)) Directory.CreateDirectory(Path_WebApi);
if (!Directory.Exists(Path_AgvVoltage)) Directory.CreateDirectory(Path_AgvVoltage);
}
/// <summary>
/// 向指定文件写入信息
/// </summary>
/// <param name="FilePath">文件路径</param>
/// <param name="Appended">是否追加文件信息</param>
/// <param name="LogInfo">信息内容</param>
public void MessageWriter(string FilePath, bool Appended, string LogInfo)
{
try
{
using (StreamWriter sw = new StreamWriter(FilePath, Appended))
{
sw.WriteLine(DateTime.Now.ToString("HH:mm:ss fff") + "==" + LogInfo);
sw.Close();
}
}
catch (Exception Ex)
{
App.ExFile.MessageError("MessageWriter", Ex.Message.Replace(Environment.NewLine, ""));
}
}
/// <summary>
/// 日志信息
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="Message">信息内容</param>
public void MessageLog(string fileName, string Message)
{
try
{
DirectoryCheck();
string pathName = App.ExFile.Path_Log + "\\" + DateTime.Now.ToString("yyyy_MM_dd") + fileName + ".txt";
MessageWriter(pathName, true, Message);
}
catch (Exception Ex)
{
App.ExFile.MessageError("MessageLog", Ex.Message.Replace(Environment.NewLine, ""));
}
}
/// <summary>
/// 业务处理异常信息
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="Message">信息内容</param>
public void MessageBusinessErr(string fileName, string Message)
{
try
{
DirectoryCheck();
string pathName = App.ExFile.Path_BusinessErr + "\\" + DateTime.Now.ToString("yyyy_MM_dd") + fileName + ".txt";
MessageWriter(pathName, true, Message);
App.WinMain.TBox_BussErrMsg.Dispatcher.BeginInvoke(new Action(() =>
{
App.WinMain.TBox_BussErrMsg.AppendText(DateTime.Now.ToLongTimeString() + "::" + fileName + "::" + Message + "\n");
if (App.WinMain.TBox_BussErrMsg.LineCount > 100)
{
App.WinMain.TBox_BussErrMsg.Clear();
}
}));
}
catch (Exception Ex)
{
App.ExFile.MessageError("MessageLog", Ex.Message.Replace(Environment.NewLine, ""));
}
}
/// <summary>
/// 错误信息
/// </summary>
/// <param name="filePath">路径</param>
/// <param name="fileName">文件名</param>
/// <param name="Message">信息内容</param>
public void MessageError(string fileName, string Message)
{
DirectoryCheck();
string pathName = App.ExFile.Path_Error + "\\" + DateTime.Now.ToString("yyyy_MM_dd") + fileName + ".txt";
MessageWriter(pathName, true, Message);
App.WinMain.TBox_EMessage.Dispatcher.BeginInvoke(new Action(() =>
{
App.WinMain.TBox_EMessage.AppendText(DateTime.Now.ToLongTimeString() + "::" + fileName + "::" + Message + "\n");
App.WinMain.TBox_EMessage.PageDown();
if (App.WinMain.TBox_EMessage.LineCount > 100)
{
App.WinMain.TBox_EMessage.Clear();
}
}));
}
/// <summary>
/// WebApi接口报错信息
/// </summary>
/// <param name="fileName"></param>
/// <param name="Message"></param>
public void MessageWebApi(string fileName, string Message)
{
try
{
DirectoryCheck();
string pathName = App.ExFile.Path_WebApi + "\\" + DateTime.Now.ToString("yyyy_MM_dd") + fileName + ".txt";
MessageWriter(pathName, true, Message);
}
catch (Exception Ex)
{
App.ExFile.MessageError("MessageWebApi", Ex.Message.Replace(Environment.NewLine, ""));
}
}
/// <summary>
/// 日志信息
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="Message">信息内容</param>
public void AgvVoltageLog(string fileName, string Message)
{
try
{
DirectoryCheck();
string pathName = App.ExFile.Path_AgvVoltage + "\\" + DateTime.Now.ToString("yyyy_MM_dd") + fileName + ".txt";
MessageWriter(pathName, true, Message);
}
catch (Exception Ex)
{
App.ExFile.MessageError("MessageLog", Ex.Message.Replace(Environment.NewLine, ""));
}
}
}
}