LogService.cs
9.44 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using HHECS.Application.Enums;
using HHECS.Application.Error;
using HHECS.BllModel;
using HHECS.Dal.Repository;
using HHECS.Infrastructure.Enums;
using HHECS.Model.Entities;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
namespace HHECS.Application.Service
{
/// <summary>
/// 日志服务类
/// </summary>
public class LogService : BaseService
{
#region 日志查询
public BllResult<List<InterfaceLog>> GetInterfaceLogs(Expression<Func<InterfaceLog, bool>> exp, int pageIndex, int pageSize, out long totalCount)
{
try
{
InterfaceLogRepository interfaceLogRepository = new InterfaceLogRepository();
var result = interfaceLogRepository.Where(exp).Count(out totalCount).OrderByDescending(a => a.CreateTime).Page(pageIndex, pageSize).ToList();
return BllResultFactory.Success(result);
}
catch (Exception ex)
{
totalCount = 0;
return BllResultFactory.Error<List<InterfaceLog>>($"异常:{ex.Message}", ErrorCodeConst.DEZ0001.ToString());
}
}
public BllResult<List<InterfaceLog>> GetInterfaceLogs(Expression<Func<InterfaceLog, bool>> exp)
{
try
{
InterfaceLogRepository interfaceLogRepository = new InterfaceLogRepository();
var result = interfaceLogRepository.Where(exp).OrderByDescending(a => a.CreateTime).ToList();
return BllResultFactory.Success(result);
}
catch (Exception ex)
{
return BllResultFactory.Error<List<InterfaceLog>>($"异常:{ex.Message}", ErrorCodeConst.DEZ0001.ToString());
}
}
public BllResult<List<ContentLog>> GetContentLogs(Expression<Func<ContentLog, bool>> exp, int pageIndex, int pageSize, out long totalCount)
{
try
{
ContentLogRepository contentLogRepository = new ContentLogRepository();
var result = contentLogRepository.Where(exp).Count(out totalCount).OrderByDescending(a => a.CreateTime).Page(pageIndex, pageSize).ToList();
return BllResultFactory.Success(result);
}
catch (Exception ex)
{
totalCount = 0;
return BllResultFactory.Error<List<ContentLog>>($"异常:{ex.Message}", ErrorCodeConst.DEZ0001.ToString());
}
}
public BllResult<List<ContentLog>> GetContentLogs(Expression<Func<ContentLog, bool>> exp)
{
try
{
ContentLogRepository contentLogRepository = new ContentLogRepository();
var result = contentLogRepository.Where(exp).OrderByDescending(a => a.CreateTime).ToList();
return BllResultFactory.Success(result);
}
catch (Exception ex)
{
return BllResultFactory.Error<List<ContentLog>>($"异常:{ex.Message}", ErrorCodeConst.DEZ0001.ToString());
}
}
public BllResult<List<OperationLog>> GetOperationLogs(Expression<Func<OperationLog, bool>> exp, int pageIndex, int pageSize, out long totalCount)
{
try
{
OperationLogRepository operationLogRepository = new OperationLogRepository();
var result = operationLogRepository.Where(exp).Count(out totalCount).OrderByDescending(a => a.CreateTime).Page(pageIndex, pageSize).ToList();
return BllResultFactory.Success(result);
}
catch (Exception ex)
{
totalCount = 0;
return BllResultFactory.Error<List<OperationLog>>($"异常:{ex.Message}", ErrorCodeConst.DEZ0001.ToString());
}
}
public BllResult<List<OperationLog>> GetOperationLogs(Expression<Func<OperationLog, bool>> exp)
{
try
{
OperationLogRepository operationLogRepository = new OperationLogRepository();
var result = operationLogRepository.Where(exp).OrderByDescending(a => a.CreateTime).ToList();
return BllResultFactory.Success(result);
}
catch (Exception ex)
{
return BllResultFactory.Error<List<OperationLog>>($"异常:{ex.Message}", ErrorCodeConst.DEZ0001.ToString());
}
}
#endregion
#region 日志记录
/// <summary>
/// 记录接口日志到数据库
/// </summary>
/// <param name="interfaceName"></param>
/// <param name="request"></param>
/// <param name="response"></param>
/// <param name="flag"></param>
/// <returns></returns>
public BllResult LogInterface(string interfaceName, string request, string response, Level flag, string content, string remark)
{
InterfaceLog interfaceLog = new InterfaceLog();
interfaceLog.InterfaceName = interfaceName;
interfaceLog.Request = request;
interfaceLog.Response = response;
interfaceLog.Flag = flag.ToString();
interfaceLog.Content = content.Length > 300 ? content.Substring(0,300) : content;
interfaceLog.Remark = remark;
interfaceLog.CreateBy = Accounts.WCSInterface.ToString();
interfaceLog.CreateTime = DateTime.Now;
InterfaceLogRepository interfaceLogRepository = new InterfaceLogRepository();
interfaceLogRepository.Insert(interfaceLog);
return BllResultFactory.Success();
}
/// <summary>
/// 记录接口日志到数据库
/// </summary>
public BllResult LogInterface(string interfaceName, string url, string request, string response, Level flag, string content, string remark, string createdBy, DateTime? startTime, DateTime? endTime)
{
try
{
InterfaceLog interfaceLog = new InterfaceLog()
{
InterfaceName = interfaceName,
Url = url,
Request = request,
Response = response,
Flag = flag.ToString(),
Content = content.Length > 300 ? content.Substring(0, 300) : content,
Remark = remark,
CreateBy = createdBy,
UpdateBy = createdBy,
CreateTime = startTime,
UpdateTime = endTime
};
InterfaceLogRepository interfaceLogRepository = new InterfaceLogRepository();
interfaceLogRepository.Insert(interfaceLog);
return BllResultFactory.Success();
}
catch (Exception ex)
{
return BllResultFactory.Error<List<InterfaceLog>>($"异常:{ex.Message}", ErrorCodeConst.DEZ0001.ToString());
}
}
/// <summary>
/// 记录普通日志到数据库
/// </summary>
public BllResult LogContent(Title logTitle, string log, string userCode, Level flag)
{
try
{
ContentLog contentLog = new ContentLog()
{
Title = logTitle.ToString(),
Content = log,
Flag = flag.ToString(),
CreateTime = DateTime.Now,
CreateBy = userCode
};
ContentLogRepository contentLogRepository = new ContentLogRepository();
contentLogRepository.Insert(contentLog);
return BllResultFactory.Success();
}
catch (Exception ex)
{
return BllResultFactory.Error<List<InterfaceLog>>($"异常:{ex.Message}", ErrorCodeConst.DEZ0001.ToString());
}
}
/// <summary>
/// 记录操作日志
/// </summary>
public BllResult LogOperation(Title noticeTitle, ModuleConst moduleConst, string content, string result, string userCode)
{
try
{
OperationLog operationLog = new OperationLog()
{
Title = noticeTitle.ToString(),
Module = moduleConst.ToString(),
Content = content,
Result = result,
CreateTime = DateTime.Now,
CreateBy = userCode
};
OperationLogRepository operationLogRepository = new OperationLogRepository();
operationLogRepository.Insert(operationLog);
return BllResultFactory.Success();
}
catch (Exception ex)
{
return BllResultFactory.Error<List<InterfaceLog>>($"异常:{ex.Message}", ErrorCodeConst.DEZ0001.ToString());
}
}
/// <summary>
/// 记录业务返回日志
/// </summary>
public void LogBllResult(BllResult bllResult, Title title, string userCode)
{
LogContent(title, bllResult.Msg, userCode, bllResult.Success ? Level.Success : Level.Failure);
}
/// <summary>
/// 记录业务返回日志
/// </summary>
public void LogBllResult<T>(BllResult<T> bllResult, Title title, string userCode)
{
LogContent(title, bllResult.Msg, userCode, bllResult.Success ? Level.Success : Level.Failure);
}
#endregion
}
}