Skip to content

Commit ff207ec

Browse files
authored
Merge pull request #50 from hmlendea/logging
Implemented basic logging
2 parents 1684280 + f427b3e commit ff207ec

File tree

7 files changed

+251
-73
lines changed

7 files changed

+251
-73
lines changed

Logging/MyLogInfoKey.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using NuciLog.Core;
2+
3+
namespace PersonalLogManager.Logging
4+
{
5+
public sealed class MyLogInfoKey : LogInfoKey
6+
{
7+
MyLogInfoKey(string name) : base(name) { }
8+
9+
public static LogInfoKey Identifier => new MyLogInfoKey(nameof(Identifier));
10+
11+
public static LogInfoKey Template => new MyLogInfoKey(nameof(Template));
12+
13+
public static LogInfoKey Date => new MyLogInfoKey(nameof(Date));
14+
15+
public static LogInfoKey Time => new MyLogInfoKey(nameof(Time));
16+
17+
public static LogInfoKey TimeZone => new MyLogInfoKey(nameof(TimeZone));
18+
19+
public static LogInfoKey Localisation => new MyLogInfoKey(nameof(Localisation));
20+
21+
public static LogInfoKey Count => new MyLogInfoKey(nameof(Count));
22+
}
23+
}

Logging/MyOperation.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using NuciLog.Core;
2+
3+
namespace PersonalLogManager.Logging
4+
{
5+
public sealed class MyOperation : Operation
6+
{
7+
MyOperation(string name) : base(name) { }
8+
9+
public static Operation StorePersonalLog => new MyOperation(nameof(StorePersonalLog));
10+
11+
public static Operation GetPersonalLogs => new MyOperation(nameof(GetPersonalLogs));
12+
13+
public static Operation UpdatePersonalLog => new MyOperation(nameof(UpdatePersonalLog));
14+
15+
public static Operation DeletePersonalLog => new MyOperation(nameof(DeletePersonalLog));
16+
}
17+
}

PersonalLogManager.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<PackageReference Include="NuciAPI" Version="3.0.0" />
1111
<PackageReference Include="NuciAPI.Controllers" Version="2.1.0" />
1212
<PackageReference Include="NuciDAL" Version="2.4.1" />
13+
<PackageReference Include="NuciLog" Version="1.1.1" />
14+
<PackageReference Include="NuciLog.Core" Version="2.4.0" />
1315
<PackageReference Include="NuciSecurity.HMAC" Version="4.1.2" />
1416
<PackageReference Include="NuciText.Obfuscation" Version="1.0.0" />
1517
</ItemGroup>

Service/PersonalLogService.cs

Lines changed: 196 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
using System.Text.RegularExpressions;
55
using AutoMapper;
66
using NuciDAL.Repositories;
7+
using NuciLog.Core;
78
using PersonalLogManager.Api.Models;
89
using PersonalLogManager.DataAccess.DataObjects;
10+
using PersonalLogManager.Logging;
911
using PersonalLogManager.Service.Models;
1012
using PersonalLogManager.Service.TextBuilding;
1113

@@ -14,109 +16,236 @@ namespace PersonalLogManager.Service
1416
public class PersonalLogService(
1517
IPersonalLogTextBuilderFactory logTextBuilder,
1618
IFileRepository<PersonalLogEntity> repository,
17-
IMapper mapper) : IPersonalLogService
19+
IMapper mapper,
20+
ILogger logger) : IPersonalLogService
1821
{
1922
private readonly Random random = new();
2023

2124
public void StorePersonalLog(StoreLogRequest request)
2225
{
23-
repository.Add(new()
26+
IEnumerable<LogInfo> logInfos =
27+
[
28+
new(MyLogInfoKey.Template, request.Template),
29+
new(MyLogInfoKey.Date, request.Date),
30+
new(MyLogInfoKey.Time, request.Time),
31+
new(MyLogInfoKey.TimeZone, request.TimeZone)
32+
];
33+
34+
logger.Info(
35+
MyOperation.StorePersonalLog,
36+
OperationStatus.Started,
37+
logInfos);
38+
39+
string id = $"L{random.Next(0, 1000000000):D9}";
40+
41+
try
42+
{
43+
repository.Add(new()
44+
{
45+
Id = id,
46+
Date = request.Date,
47+
Time = request.Time,
48+
TimeZone = request.TimeZone,
49+
Template = request.Template,
50+
Data = request.Data,
51+
CreatedDT = DateTime.UtcNow.ToString("o")
52+
});
53+
54+
repository.ApplyChanges();
55+
}
56+
catch (Exception ex)
2457
{
25-
Id = $"L{random.Next(0, 1000000000):D9}",
26-
Date = request.Date,
27-
Time = request.Time,
28-
TimeZone = request.TimeZone,
29-
Template = request.Template,
30-
Data = request.Data,
31-
CreatedDT = DateTime.UtcNow.ToString("o")
32-
});
33-
34-
repository.ApplyChanges();
58+
logger.Error(
59+
MyOperation.StorePersonalLog,
60+
OperationStatus.Failure,
61+
ex,
62+
logInfos);
63+
throw;
64+
}
65+
66+
logger.Debug(
67+
MyOperation.StorePersonalLog,
68+
OperationStatus.Success,
69+
logInfos,
70+
new LogInfo(MyLogInfoKey.Identifier, id));
3571
}
3672

3773
public GetLogResponse GetPersonalLogs(GetLogRequest request)
3874
{
39-
IEnumerable<PersonalLogEntity> logs = repository.GetAll();
75+
IEnumerable<LogInfo> logInfos =
76+
[
77+
new(MyLogInfoKey.Template, request.Template),
78+
new(MyLogInfoKey.Date, request.Date),
79+
new(MyLogInfoKey.Time, request.Time),
80+
new(MyLogInfoKey.Localisation, request.Localisation),
81+
new(MyLogInfoKey.Count, request.Count)
82+
];
4083

41-
if (!string.IsNullOrWhiteSpace(request.Date))
42-
{
43-
logs = logs.Where(log => DoesFieldMatch(log.Date, request.Date));
44-
}
84+
logger.Info(
85+
MyOperation.GetPersonalLogs,
86+
OperationStatus.Started,
87+
logInfos);
4588

46-
if (!string.IsNullOrWhiteSpace(request.Time))
89+
try
4790
{
48-
logs = logs.Where(log => DoesFieldMatch(log.Time, request.Time));
49-
}
91+
IEnumerable<PersonalLogEntity> logs = repository.GetAll();
5092

51-
if (!string.IsNullOrWhiteSpace(request.Template))
52-
{
53-
logs = logs.Where(log => DoesFieldMatch(log.Template, request.Template));
54-
}
93+
if (!string.IsNullOrWhiteSpace(request.Date))
94+
{
95+
logs = logs.Where(log => DoesFieldMatch(log.Date, request.Date));
96+
}
5597

56-
if (request.Data is not null && request.Data.Count > 0)
57-
{
58-
foreach (string dataKey in request.Data.Keys)
98+
if (!string.IsNullOrWhiteSpace(request.Time))
5999
{
60-
logs = logs.Where(log =>
61-
log.Data is not null &&
62-
log.Data.ContainsKey(dataKey) &&
63-
log.Data[dataKey] is not null &&
64-
DoesFieldMatch(
65-
log.Data[dataKey],
66-
request.Data[dataKey],
67-
RegexOptions.IgnoreCase));
100+
logs = logs.Where(log => DoesFieldMatch(log.Time, request.Time));
101+
}
102+
103+
if (!string.IsNullOrWhiteSpace(request.Template))
104+
{
105+
logs = logs.Where(log => DoesFieldMatch(log.Template, request.Template));
106+
}
107+
108+
if (request.Data is not null && request.Data.Count > 0)
109+
{
110+
foreach (string dataKey in request.Data.Keys)
111+
{
112+
logs = logs.Where(log =>
113+
log.Data is not null &&
114+
log.Data.ContainsKey(dataKey) &&
115+
log.Data[dataKey] is not null &&
116+
DoesFieldMatch(
117+
log.Data[dataKey],
118+
request.Data[dataKey],
119+
RegexOptions.IgnoreCase));
120+
}
68121
}
69-
}
70122

71-
return new GetLogResponse()
123+
logger.Debug(
124+
MyOperation.GetPersonalLogs,
125+
OperationStatus.Success,
126+
logInfos,
127+
new LogInfo(MyLogInfoKey.Count, logs.Count()));
128+
129+
return new GetLogResponse()
130+
{
131+
Logs = [.. logs
132+
.OrderByDescending(log => log.Date)
133+
.ThenByDescending(log => log.Time)
134+
.ThenBy(log => log.Template)
135+
.ThenBy(log => log.CreatedDT)
136+
.Take(request.Count)
137+
.Select(log => $"{log.Id} " + logTextBuilder.BuildLogText(mapper.Map<PersonalLog>(log), request.Localisation))]
138+
};
139+
}
140+
catch (Exception ex)
72141
{
73-
Logs = [.. logs
74-
.OrderByDescending(log => log.Date)
75-
.ThenByDescending(log => log.Time)
76-
.ThenBy(log => log.Template)
77-
.ThenBy(log => log.CreatedDT)
78-
.Take(request.Count)
79-
.Select(log => $"{log.Id} " + logTextBuilder.BuildLogText(mapper.Map<PersonalLog>(log), request.Localisation))]
80-
};
142+
logger.Error(
143+
MyOperation.GetPersonalLogs,
144+
OperationStatus.Failure,
145+
ex,
146+
logInfos);
147+
148+
throw;
149+
}
81150
}
82151

83152
public void UpdatePersonalLog(UpdateLogRequest request)
84153
{
85-
PersonalLogEntity personalLog = repository.Get(request.Identifier);
154+
IEnumerable<LogInfo> logInfos =
155+
[
156+
new LogInfo(MyLogInfoKey.Identifier, request.Identifier),
157+
new LogInfo(MyLogInfoKey.Date, request.Date),
158+
new LogInfo(MyLogInfoKey.Time, request.Time),
159+
new LogInfo(MyLogInfoKey.TimeZone, request.TimeZone),
160+
new LogInfo(MyLogInfoKey.Template, request.Template)
161+
];
86162

87-
if (request.Date is not null)
88-
{
89-
personalLog.Date = request.Date;
90-
}
163+
logger.Info(
164+
MyOperation.UpdatePersonalLog,
165+
OperationStatus.Started,
166+
logInfos);
91167

92-
if (request.Time is not null)
168+
try
93169
{
94-
personalLog.Time = request.Time;
95-
}
170+
PersonalLogEntity personalLog = repository.Get(request.Identifier);
96171

97-
if (request.TimeZone is not null)
98-
{
99-
personalLog.TimeZone = request.TimeZone;
100-
}
172+
if (request.Date is not null)
173+
{
174+
personalLog.Date = request.Date;
175+
}
101176

102-
if (request.Data is not null)
103-
{
104-
foreach (string parameter in request.Data.Keys)
177+
if (request.Time is not null)
105178
{
106-
personalLog.Data[parameter] = request.Data[parameter];
179+
personalLog.Time = request.Time;
107180
}
108-
}
109181

110-
personalLog.UpdatedDT = DateTime.UtcNow.ToString("o");
182+
if (request.TimeZone is not null)
183+
{
184+
personalLog.TimeZone = request.TimeZone;
185+
}
186+
187+
if (request.Data is not null)
188+
{
189+
foreach (string parameter in request.Data.Keys)
190+
{
191+
personalLog.Data[parameter] = request.Data[parameter];
192+
}
193+
}
194+
195+
personalLog.UpdatedDT = DateTime.UtcNow.ToString("o");
196+
197+
repository.Update(personalLog);
198+
repository.ApplyChanges();
111199

112-
repository.Update(personalLog);
113-
repository.ApplyChanges();
200+
logger.Debug(
201+
MyOperation.UpdatePersonalLog,
202+
OperationStatus.Success,
203+
logInfos);
204+
}
205+
catch (Exception ex)
206+
{
207+
logger.Error(
208+
MyOperation.UpdatePersonalLog,
209+
OperationStatus.Failure,
210+
ex,
211+
logInfos);
212+
213+
throw;
214+
}
114215
}
115216

116217
public void DeletePersonalLog(DeleteLogRequest request)
117218
{
118-
repository.Remove(request.Identifier);
119-
repository.ApplyChanges();
219+
IEnumerable<LogInfo> logInfos =
220+
[
221+
new(MyLogInfoKey.Identifier, request.Identifier)
222+
];
223+
224+
logger.Info(
225+
MyOperation.DeletePersonalLog,
226+
OperationStatus.Started,
227+
logInfos);
228+
229+
try
230+
{
231+
repository.Remove(request.Identifier);
232+
repository.ApplyChanges();
233+
}
234+
catch (Exception ex)
235+
{
236+
logger.Error(
237+
MyOperation.DeletePersonalLog,
238+
OperationStatus.Failure,
239+
ex,
240+
logInfos);
241+
242+
throw;
243+
}
244+
245+
logger.Debug(
246+
MyOperation.DeletePersonalLog,
247+
OperationStatus.Success,
248+
logInfos);
120249
}
121250

122251
private static bool DoesFieldMatch(

Service/TextBuilding/Localisation/EnglishTextBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,7 @@ public string BuildDeviceExternalCleaningLogText(PersonalLog log)
12231223
text += $" by {GetCleaningMethod(log.Data)}";
12241224
}
12251225

1226-
return text;
1226+
return text + GetLocation(log.Data);
12271227
}
12281228

12291229
public string BuildDeviceInternalCleaningLogText(PersonalLog log)
@@ -1235,7 +1235,7 @@ public string BuildDeviceInternalCleaningLogText(PersonalLog log)
12351235
text += $" by {GetCleaningMethod(log.Data)}";
12361236
}
12371237

1238-
return text;
1238+
return text + GetLocation(log.Data);
12391239
}
12401240

12411241
public string BuildDeviceRepairLogText(PersonalLog log)

0 commit comments

Comments
 (0)