diff --git a/Logging/MyLogInfoKey.cs b/Logging/MyLogInfoKey.cs new file mode 100644 index 0000000..df791c1 --- /dev/null +++ b/Logging/MyLogInfoKey.cs @@ -0,0 +1,23 @@ +using NuciLog.Core; + +namespace PersonalLogManager.Logging +{ + public sealed class MyLogInfoKey : LogInfoKey + { + MyLogInfoKey(string name) : base(name) { } + + public static LogInfoKey Identifier => new MyLogInfoKey(nameof(Identifier)); + + public static LogInfoKey Template => new MyLogInfoKey(nameof(Template)); + + public static LogInfoKey Date => new MyLogInfoKey(nameof(Date)); + + public static LogInfoKey Time => new MyLogInfoKey(nameof(Time)); + + public static LogInfoKey TimeZone => new MyLogInfoKey(nameof(TimeZone)); + + public static LogInfoKey Localisation => new MyLogInfoKey(nameof(Localisation)); + + public static LogInfoKey Count => new MyLogInfoKey(nameof(Count)); + } +} diff --git a/Logging/MyOperation.cs b/Logging/MyOperation.cs new file mode 100644 index 0000000..e2552d3 --- /dev/null +++ b/Logging/MyOperation.cs @@ -0,0 +1,17 @@ +using NuciLog.Core; + +namespace PersonalLogManager.Logging +{ + public sealed class MyOperation : Operation + { + MyOperation(string name) : base(name) { } + + public static Operation StorePersonalLog => new MyOperation(nameof(StorePersonalLog)); + + public static Operation GetPersonalLogs => new MyOperation(nameof(GetPersonalLogs)); + + public static Operation UpdatePersonalLog => new MyOperation(nameof(UpdatePersonalLog)); + + public static Operation DeletePersonalLog => new MyOperation(nameof(DeletePersonalLog)); + } +} diff --git a/PersonalLogManager.csproj b/PersonalLogManager.csproj index 0a63518..1f8241d 100644 --- a/PersonalLogManager.csproj +++ b/PersonalLogManager.csproj @@ -10,6 +10,8 @@ + + diff --git a/Service/PersonalLogService.cs b/Service/PersonalLogService.cs index 76943e8..b4e08c7 100644 --- a/Service/PersonalLogService.cs +++ b/Service/PersonalLogService.cs @@ -4,8 +4,10 @@ using System.Text.RegularExpressions; using AutoMapper; using NuciDAL.Repositories; +using NuciLog.Core; using PersonalLogManager.Api.Models; using PersonalLogManager.DataAccess.DataObjects; +using PersonalLogManager.Logging; using PersonalLogManager.Service.Models; using PersonalLogManager.Service.TextBuilding; @@ -14,109 +16,236 @@ namespace PersonalLogManager.Service public class PersonalLogService( IPersonalLogTextBuilderFactory logTextBuilder, IFileRepository repository, - IMapper mapper) : IPersonalLogService + IMapper mapper, + ILogger logger) : IPersonalLogService { private readonly Random random = new(); public void StorePersonalLog(StoreLogRequest request) { - repository.Add(new() + IEnumerable logInfos = + [ + new(MyLogInfoKey.Template, request.Template), + new(MyLogInfoKey.Date, request.Date), + new(MyLogInfoKey.Time, request.Time), + new(MyLogInfoKey.TimeZone, request.TimeZone) + ]; + + logger.Info( + MyOperation.StorePersonalLog, + OperationStatus.Started, + logInfos); + + string id = $"L{random.Next(0, 1000000000):D9}"; + + try + { + repository.Add(new() + { + Id = id, + Date = request.Date, + Time = request.Time, + TimeZone = request.TimeZone, + Template = request.Template, + Data = request.Data, + CreatedDT = DateTime.UtcNow.ToString("o") + }); + + repository.ApplyChanges(); + } + catch (Exception ex) { - Id = $"L{random.Next(0, 1000000000):D9}", - Date = request.Date, - Time = request.Time, - TimeZone = request.TimeZone, - Template = request.Template, - Data = request.Data, - CreatedDT = DateTime.UtcNow.ToString("o") - }); - - repository.ApplyChanges(); + logger.Error( + MyOperation.StorePersonalLog, + OperationStatus.Failure, + ex, + logInfos); + throw; + } + + logger.Debug( + MyOperation.StorePersonalLog, + OperationStatus.Success, + logInfos, + new LogInfo(MyLogInfoKey.Identifier, id)); } public GetLogResponse GetPersonalLogs(GetLogRequest request) { - IEnumerable logs = repository.GetAll(); + IEnumerable logInfos = + [ + new(MyLogInfoKey.Template, request.Template), + new(MyLogInfoKey.Date, request.Date), + new(MyLogInfoKey.Time, request.Time), + new(MyLogInfoKey.Localisation, request.Localisation), + new(MyLogInfoKey.Count, request.Count) + ]; - if (!string.IsNullOrWhiteSpace(request.Date)) - { - logs = logs.Where(log => DoesFieldMatch(log.Date, request.Date)); - } + logger.Info( + MyOperation.GetPersonalLogs, + OperationStatus.Started, + logInfos); - if (!string.IsNullOrWhiteSpace(request.Time)) + try { - logs = logs.Where(log => DoesFieldMatch(log.Time, request.Time)); - } + IEnumerable logs = repository.GetAll(); - if (!string.IsNullOrWhiteSpace(request.Template)) - { - logs = logs.Where(log => DoesFieldMatch(log.Template, request.Template)); - } + if (!string.IsNullOrWhiteSpace(request.Date)) + { + logs = logs.Where(log => DoesFieldMatch(log.Date, request.Date)); + } - if (request.Data is not null && request.Data.Count > 0) - { - foreach (string dataKey in request.Data.Keys) + if (!string.IsNullOrWhiteSpace(request.Time)) { - logs = logs.Where(log => - log.Data is not null && - log.Data.ContainsKey(dataKey) && - log.Data[dataKey] is not null && - DoesFieldMatch( - log.Data[dataKey], - request.Data[dataKey], - RegexOptions.IgnoreCase)); + logs = logs.Where(log => DoesFieldMatch(log.Time, request.Time)); + } + + if (!string.IsNullOrWhiteSpace(request.Template)) + { + logs = logs.Where(log => DoesFieldMatch(log.Template, request.Template)); + } + + if (request.Data is not null && request.Data.Count > 0) + { + foreach (string dataKey in request.Data.Keys) + { + logs = logs.Where(log => + log.Data is not null && + log.Data.ContainsKey(dataKey) && + log.Data[dataKey] is not null && + DoesFieldMatch( + log.Data[dataKey], + request.Data[dataKey], + RegexOptions.IgnoreCase)); + } } - } - return new GetLogResponse() + logger.Debug( + MyOperation.GetPersonalLogs, + OperationStatus.Success, + logInfos, + new LogInfo(MyLogInfoKey.Count, logs.Count())); + + return new GetLogResponse() + { + Logs = [.. logs + .OrderByDescending(log => log.Date) + .ThenByDescending(log => log.Time) + .ThenBy(log => log.Template) + .ThenBy(log => log.CreatedDT) + .Take(request.Count) + .Select(log => $"{log.Id} " + logTextBuilder.BuildLogText(mapper.Map(log), request.Localisation))] + }; + } + catch (Exception ex) { - Logs = [.. logs - .OrderByDescending(log => log.Date) - .ThenByDescending(log => log.Time) - .ThenBy(log => log.Template) - .ThenBy(log => log.CreatedDT) - .Take(request.Count) - .Select(log => $"{log.Id} " + logTextBuilder.BuildLogText(mapper.Map(log), request.Localisation))] - }; + logger.Error( + MyOperation.GetPersonalLogs, + OperationStatus.Failure, + ex, + logInfos); + + throw; + } } public void UpdatePersonalLog(UpdateLogRequest request) { - PersonalLogEntity personalLog = repository.Get(request.Identifier); + IEnumerable logInfos = + [ + new LogInfo(MyLogInfoKey.Identifier, request.Identifier), + new LogInfo(MyLogInfoKey.Date, request.Date), + new LogInfo(MyLogInfoKey.Time, request.Time), + new LogInfo(MyLogInfoKey.TimeZone, request.TimeZone), + new LogInfo(MyLogInfoKey.Template, request.Template) + ]; - if (request.Date is not null) - { - personalLog.Date = request.Date; - } + logger.Info( + MyOperation.UpdatePersonalLog, + OperationStatus.Started, + logInfos); - if (request.Time is not null) + try { - personalLog.Time = request.Time; - } + PersonalLogEntity personalLog = repository.Get(request.Identifier); - if (request.TimeZone is not null) - { - personalLog.TimeZone = request.TimeZone; - } + if (request.Date is not null) + { + personalLog.Date = request.Date; + } - if (request.Data is not null) - { - foreach (string parameter in request.Data.Keys) + if (request.Time is not null) { - personalLog.Data[parameter] = request.Data[parameter]; + personalLog.Time = request.Time; } - } - personalLog.UpdatedDT = DateTime.UtcNow.ToString("o"); + if (request.TimeZone is not null) + { + personalLog.TimeZone = request.TimeZone; + } + + if (request.Data is not null) + { + foreach (string parameter in request.Data.Keys) + { + personalLog.Data[parameter] = request.Data[parameter]; + } + } + + personalLog.UpdatedDT = DateTime.UtcNow.ToString("o"); + + repository.Update(personalLog); + repository.ApplyChanges(); - repository.Update(personalLog); - repository.ApplyChanges(); + logger.Debug( + MyOperation.UpdatePersonalLog, + OperationStatus.Success, + logInfos); + } + catch (Exception ex) + { + logger.Error( + MyOperation.UpdatePersonalLog, + OperationStatus.Failure, + ex, + logInfos); + + throw; + } } public void DeletePersonalLog(DeleteLogRequest request) { - repository.Remove(request.Identifier); - repository.ApplyChanges(); + IEnumerable logInfos = + [ + new(MyLogInfoKey.Identifier, request.Identifier) + ]; + + logger.Info( + MyOperation.DeletePersonalLog, + OperationStatus.Started, + logInfos); + + try + { + repository.Remove(request.Identifier); + repository.ApplyChanges(); + } + catch (Exception ex) + { + logger.Error( + MyOperation.DeletePersonalLog, + OperationStatus.Failure, + ex, + logInfos); + + throw; + } + + logger.Debug( + MyOperation.DeletePersonalLog, + OperationStatus.Success, + logInfos); } private static bool DoesFieldMatch( diff --git a/Service/TextBuilding/Localisation/EnglishTextBuilder.cs b/Service/TextBuilding/Localisation/EnglishTextBuilder.cs index 26f8328..bf01744 100644 --- a/Service/TextBuilding/Localisation/EnglishTextBuilder.cs +++ b/Service/TextBuilding/Localisation/EnglishTextBuilder.cs @@ -1223,7 +1223,7 @@ public string BuildDeviceExternalCleaningLogText(PersonalLog log) text += $" by {GetCleaningMethod(log.Data)}"; } - return text; + return text + GetLocation(log.Data); } public string BuildDeviceInternalCleaningLogText(PersonalLog log) @@ -1235,7 +1235,7 @@ public string BuildDeviceInternalCleaningLogText(PersonalLog log) text += $" by {GetCleaningMethod(log.Data)}"; } - return text; + return text + GetLocation(log.Data); } public string BuildDeviceRepairLogText(PersonalLog log) diff --git a/Service/TextBuilding/Localisation/RomanianTextBuilder.cs b/Service/TextBuilding/Localisation/RomanianTextBuilder.cs index bbb25f6..64d2dc6 100644 --- a/Service/TextBuilding/Localisation/RomanianTextBuilder.cs +++ b/Service/TextBuilding/Localisation/RomanianTextBuilder.cs @@ -1234,7 +1234,7 @@ public string BuildDeviceExternalCleaningLogText(PersonalLog log) text += $" prin {GetCleaningMethod(log.Data)}"; } - return text; + return text + GetLocation(log.Data); } public string BuildDeviceInternalCleaningLogText(PersonalLog log) @@ -1246,7 +1246,7 @@ public string BuildDeviceInternalCleaningLogText(PersonalLog log) text += $" prin {GetCleaningMethod(log.Data)}"; } - return text; + return text + GetLocation(log.Data); } public string BuildDeviceRepairLogText(PersonalLog log) diff --git a/ServiceCollectionExtensions.cs b/ServiceCollectionExtensions.cs index 9693517..41891ea 100644 --- a/ServiceCollectionExtensions.cs +++ b/ServiceCollectionExtensions.cs @@ -2,7 +2,9 @@ using Microsoft.Extensions.DependencyInjection; using NuciDAL.Repositories; - +using NuciLog; +using NuciLog.Configuration; +using NuciLog.Core; using PersonalLogManager.Configuration; using PersonalLogManager.DataAccess; using PersonalLogManager.DataAccess.DataObjects; @@ -15,17 +17,21 @@ public static class ServiceCollectionExtensions { static DataStoreSettings dataStoreSettings; static SecuritySettings securitySettings; + static NuciLoggerSettings logSettings; public static IServiceCollection AddConfigurations(this IServiceCollection services, IConfiguration configuration) { dataStoreSettings = new DataStoreSettings(); securitySettings = new SecuritySettings(); + logSettings = new NuciLoggerSettings(); configuration.Bind(nameof(DataStoreSettings), dataStoreSettings); configuration.Bind(nameof(securitySettings), securitySettings); + configuration.Bind(nameof(NuciLoggerSettings), logSettings); services.AddSingleton(dataStoreSettings); services.AddSingleton(securitySettings); + services.AddSingleton(logSettings); return services; } @@ -35,6 +41,7 @@ public static IServiceCollection AddCustomServices(this IServiceCollection servi .AddSingleton() .AddSingleton() .AddAutoMapper(typeof(DataAccessMappingProfile)) - .AddAutoMapper(typeof(ServiceMappingProfile)); + .AddAutoMapper(typeof(ServiceMappingProfile)) + .AddScoped(); } }