diff --git a/WatchList.Core/Logger/AggregateLogging.cs b/WatchList.Core/Logger/AggregateLogging.cs new file mode 100644 index 00000000..953f7705 --- /dev/null +++ b/WatchList.Core/Logger/AggregateLogging.cs @@ -0,0 +1,45 @@ +using Microsoft.Extensions.Logging; + +namespace WatchList.Core.Logger +{ + public class AggregateLogging : List, ILogger + { + private readonly LogLevel _logLevel; + + public AggregateLogging() + : base() + { + } + + public AggregateLogging(int capacity) + : base(capacity) + { + } + + public AggregateLogging(IEnumerable collection) + : base(collection) + { + } + + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) + { + foreach (var loggin in this) + { + loggin.Log(logLevel, eventId, state, exception, formatter); + } + } + + public bool IsEnabled(LogLevel logLevel) => _logLevel <= logLevel; + + public IDisposable? BeginScope(TState state) + where TState : notnull + => new Disposable(); + + private sealed class Disposable : IDisposable + { + public void Dispose() + { + } + } + } +} diff --git a/WatchList.Core/Logger/ConsoleLogger.cs b/WatchList.Core/Logger/ConsoleLogger.cs new file mode 100644 index 00000000..4f11f034 --- /dev/null +++ b/WatchList.Core/Logger/ConsoleLogger.cs @@ -0,0 +1,35 @@ +using Microsoft.Extensions.Logging; + +namespace WatchList.Core.Logger +{ + public sealed class ConsoleLogger : ILogger + { + private readonly LogLevel _logLevel; + + public ConsoleLogger(LogLevel logLevel) => _logLevel = logLevel; + + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) + { + if (!IsEnabled(logLevel)) + { + return; + } + + var message = formatter(state, exception); + Console.WriteLine("[{0} {1:G}] {2}", logLevel, DateTime.Now, message); + } + + public bool IsEnabled(LogLevel logLevel) => _logLevel <= logLevel; + + public IDisposable? BeginScope(TState state) + where TState : notnull + => new Disposable(); + + private sealed class Disposable : IDisposable + { + public void Dispose() + { + } + } + } +} diff --git a/WatchList.Core/Logger/FileLogger.cs b/WatchList.Core/Logger/FileLogger.cs new file mode 100644 index 00000000..b2596bd2 --- /dev/null +++ b/WatchList.Core/Logger/FileLogger.cs @@ -0,0 +1,48 @@ +using System.Globalization; +using Microsoft.Extensions.Logging; + +namespace WatchList.Core.Logger +{ + public sealed class FileLogger : ILogger + { + private readonly LogLevel _logLevel; + private readonly string _pathFileLog; + + public FileLogger(LogLevel logLevel, string pathFileLog) + { + _logLevel = logLevel; + _pathFileLog = pathFileLog; + } + + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) + { + if (!IsEnabled(logLevel)) + { + return; + } + + var message = formatter(state, exception); + var logText = string.Format("[{0} {1:G}] {2}", logLevel, DateTime.Now, message); + File.AppendAllText(BuildPath(), logText + Environment.NewLine); + } + + public bool IsEnabled(LogLevel logLevel) => _logLevel <= logLevel; + + public IDisposable? BeginScope(TState state) + where TState : notnull + => new Disposable(); + + private string BuildPath() + { + var dateStr = DateTime.Now.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture); + return Path.Combine(_pathFileLog, dateStr + ".txt"); + } + + private sealed class Disposable : IDisposable + { + public void Dispose() + { + } + } + } +} diff --git a/WatchList.Core/Repository/WatchItemRepository.cs b/WatchList.Core/Repository/WatchItemRepository.cs index edb59e8e..5987dc01 100644 --- a/WatchList.Core/Repository/WatchItemRepository.cs +++ b/WatchList.Core/Repository/WatchItemRepository.cs @@ -1,4 +1,5 @@ using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; using WatchList.Core.Model.ItemCinema; using WatchList.Core.PageItem; using WatchList.Core.Repository.Db; @@ -9,8 +10,13 @@ namespace WatchList.Core.Repository public class WatchItemRepository : IWatchItemRepository { private readonly WatchCinemaDbContext _db; + private readonly ILogger _logger; - public WatchItemRepository(WatchCinemaDbContext db) => _db = db; + public WatchItemRepository(WatchCinemaDbContext db, ILogger logger) + { + _db = db; + _logger = logger; + } public PagedList GetPage(WatchItemSearchRequest searchRequest) { @@ -24,6 +30,7 @@ public void Add(WatchItem item) item.Id = _db.ReplaceIdIsNotFree(item); _db.Add(item); _db.SaveChanges(); + _logger.LogInformation("Add item with ID {0}", item.Id); } public void Remove(Guid id) @@ -33,6 +40,8 @@ public void Remove(Guid id) { throw new InvalidOperationException("Interaction element not found."); } + + _logger.LogInformation("Remove item with ID {0}", id); } public void Update(WatchItem editItem) @@ -52,6 +61,8 @@ public void Update(WatchItem editItem) item.Status = editItem.Status; _db.SaveChanges(); + + _logger.LogInformation("Edit item with ID {0}", item.Id); } public List SelectDuplicateItems(WatchItem item) => diff --git a/WatchList.Core/Service/DataLoading/DownloadDataService.cs b/WatchList.Core/Service/DataLoading/DownloadDataService.cs index efff5da2..ddac11a3 100644 --- a/WatchList.Core/Service/DataLoading/DownloadDataService.cs +++ b/WatchList.Core/Service/DataLoading/DownloadDataService.cs @@ -1,3 +1,4 @@ +using Microsoft.Extensions.Logging; using WatchList.Core.Model.Filter; using WatchList.Core.Model.QuestionResult; using WatchList.Core.Model.Sorting; @@ -11,13 +12,14 @@ namespace WatchList.Core.Service.DataLoading public class DownloadDataService { private readonly WatchItemRepository _repository; - private readonly IMessageBox _messageBox; + private readonly ILogger _logger; - public DownloadDataService(WatchItemRepository repository, IMessageBox messageBox, int numberOfItemPerPage = 500) + public DownloadDataService(WatchItemRepository repository, IMessageBox messageBox, ILogger logger, int numberOfItemPerPage = 500) { _repository = repository; _messageBox = messageBox; + _logger = logger; NumberOfItemPerPage = numberOfItemPerPage; } @@ -33,6 +35,7 @@ public void Download(WatchItemRepository repository, ILoadRule loadRule) var watchItemCollection = new WatchItemCollection(pagedList); watchItemCollection = loadRule.Apply(watchItemCollection); + _logger.LogInformation("Load items according to selected rules"); AddItems(watchItemCollection); UpdateItems(watchItemCollection); diff --git a/WatchList.Core/WatchList.Core.csproj b/WatchList.Core/WatchList.Core.csproj index 4274e1ba..ccd7629b 100644 --- a/WatchList.Core/WatchList.Core.csproj +++ b/WatchList.Core/WatchList.Core.csproj @@ -11,6 +11,7 @@ + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/WatchList.Test/CoreTest/TestLogger.cs b/WatchList.Test/CoreTest/TestLogger.cs new file mode 100644 index 00000000..87d99e16 --- /dev/null +++ b/WatchList.Test/CoreTest/TestLogger.cs @@ -0,0 +1,34 @@ +using Microsoft.Extensions.Logging; + +namespace WatchList.Test.CoreTest +{ + public sealed class TestLogger : ILogger + { + private readonly LogLevel _logLevel; + + public TestLogger(LogLevel logLevel = LogLevel.Trace) => _logLevel = logLevel; + + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) + { + if (!IsEnabled(logLevel)) + { + return; + } + + var message = formatter(state, exception); + } + + public bool IsEnabled(LogLevel logLevel) => _logLevel <= logLevel; + + public IDisposable? BeginScope(TState state) + where TState : notnull + => new Disposable(); + + private sealed class Disposable : IDisposable + { + public void Dispose() + { + } + } + } +} diff --git a/WatchList.Test/CoreTest/WatchItemRepositoryTest.cs b/WatchList.Test/CoreTest/WatchItemRepositoryTest.cs index 5d1e063c..3f2e7b3f 100644 --- a/WatchList.Test/CoreTest/WatchItemRepositoryTest.cs +++ b/WatchList.Test/CoreTest/WatchItemRepositoryTest.cs @@ -71,7 +71,7 @@ public void Verifying_The_Use_Of_The_Database_Page_Get_Method( { // Arrange var appDbContext = new TestAppDbContextFactory().Create(); - var itemRepository = new WatchItemRepository(appDbContext); + var itemRepository = new WatchItemRepository(appDbContext, new TestLogger()); appDbContext.AddRange(watchItems); appDbContext.SaveChanges(); @@ -92,7 +92,7 @@ public void Verifying_The_Use_Of_The_Update_Item_Method( { // Arrange var appDbContext = new TestAppDbContextFactory().Create(); - var itemRepository = new WatchItemRepository(appDbContext); + var itemRepository = new WatchItemRepository(appDbContext, new TestLogger()); var searchRequest = new WatchItemSearchRequest(); appDbContext.AddRange(watchItems); appDbContext.SaveChanges(); diff --git a/WatchList.Test/CoreTest/WatchItemServiceTest/AddDuplicateItemTest.cs b/WatchList.Test/CoreTest/WatchItemServiceTest/AddDuplicateItemTest.cs index 41328678..4658eca1 100644 --- a/WatchList.Test/CoreTest/WatchItemServiceTest/AddDuplicateItemTest.cs +++ b/WatchList.Test/CoreTest/WatchItemServiceTest/AddDuplicateItemTest.cs @@ -58,7 +58,7 @@ public void Add_With_Replace_Duplicate_Element(List items, WatchItem { // Arrange var dbContext = new TestAppDbContextFactory().Create(); - var itemRepository = new WatchItemRepository(dbContext); + var itemRepository = new WatchItemRepository(dbContext, new TestLogger()); var messageBox = new Mock(); messageBox.Setup(foo => foo.ShowQuestionSaveItem(WatchItemService.DuplicateReplaceMessage)).Returns(true); var service = new WatchItemService(itemRepository, messageBox.Object); diff --git a/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadDataByDeleteGradeRuleTest.cs b/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadDataByDeleteGradeRuleTest.cs index 8a703e62..9ad819d6 100644 --- a/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadDataByDeleteGradeRuleTest.cs +++ b/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadDataByDeleteGradeRuleTest.cs @@ -111,15 +111,16 @@ public class DownloadDataByDeleteGradeRuleTest public void Add_Data_File_And_Replace_Duplicate_Element(List items, List addDownloadItem, DialogReplaceItemQuestion dialogReplaceItem, List expectItems) { // Arrange + var logger = new TestLogger(); var dbContext = new TestAppDbContextFactory().Create(); - var itemRepository = new WatchItemRepository(dbContext); + var itemRepository = new WatchItemRepository(dbContext, logger); var dbContextDownloadItem = new TestAppDbContextFactory().Create(); - var watchItemRepository = new WatchItemRepository(dbContext); + var watchItemRepository = new WatchItemRepository(dbContext, logger); var messageBox = new Mock(); messageBox.Setup(foo => foo.ShowDataReplaceQuestion(It.IsAny())).Returns(dialogReplaceItem); - var service = new DownloadDataService(watchItemRepository, messageBox.Object); + var service = new DownloadDataService(watchItemRepository, messageBox.Object, logger); var loadRuleConfig = new TestLoadRuleConfig() { DeleteGrade = false, @@ -130,7 +131,7 @@ public void Add_Data_File_And_Replace_Duplicate_Element(List items, L }), }; var loadRule = new TestAggregateLoadRule(itemRepository, loadRuleConfig); - var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem); + var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem, logger); dbContext.AddRange(items); dbContextDownloadItem.AddRange(addDownloadItem); @@ -151,15 +152,16 @@ public void Add_Data_File_And_Replace_Duplicate_Element(List items, L public async Task Add_Data_File_And_Replace_Duplicate_ElementAsync(List items, List addDownloadItem, DialogReplaceItemQuestion dialogReplaceItem, List expectItems) { // Arrange + var logger = new TestLogger(); var dbContext = new TestAppDbContextFactory().Create(); - var itemRepository = new WatchItemRepository(dbContext); - var watchItemRepository = new WatchItemRepository(dbContext); + var itemRepository = new WatchItemRepository(dbContext, logger); + var watchItemRepository = new WatchItemRepository(dbContext, logger); var dbContextDownloadItem = new TestAppDbContextFactory().Create(); var messageBox = new Mock(); messageBox.Setup(foo => foo.ShowDataReplaceQuestion(It.IsAny())).Returns(dialogReplaceItem); - var service = new DownloadDataService(watchItemRepository, messageBox.Object); + var service = new DownloadDataService(watchItemRepository, messageBox.Object, logger); var loadRuleConfig = new TestLoadRuleConfig() { DeleteGrade = false, @@ -170,7 +172,7 @@ public async Task Add_Data_File_And_Replace_Duplicate_ElementAsync(List items, List addDownloadItem, Dictionary dictionaryAddItem, List expectItems) { // Arrange + var logger = new TestLogger(); var dbContext = new TestAppDbContextFactory().Create(); - var itemRepository = new WatchItemRepository(dbContext); + var itemRepository = new WatchItemRepository(dbContext, logger); var dbContextDownloadItem = new TestAppDbContextFactory().Create(); - var watchItemRepository = new WatchItemRepository(dbContext); + var watchItemRepository = new WatchItemRepository(dbContext, logger); var messageBox = new Mock(); foreach (var item in dictionaryAddItem) @@ -201,7 +204,7 @@ public void Add_Data_File_And_One_Replace_And_Not_Replace_Duplicate_Element(List messageBox.Setup(foo => foo.ShowDataReplaceQuestion(item.Key)).Returns(item.Value); } - var service = new DownloadDataService(watchItemRepository, messageBox.Object); + var service = new DownloadDataService(watchItemRepository, messageBox.Object, logger); var loadRuleConfig = new TestLoadRuleConfig() { DeleteGrade = false, @@ -212,7 +215,7 @@ public void Add_Data_File_And_One_Replace_And_Not_Replace_Duplicate_Element(List }), }; var loadRule = new TestAggregateLoadRule(itemRepository, loadRuleConfig); - var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem); + var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem, logger); dbContext.AddRange(items); dbContextDownloadItem.AddRange(addDownloadItem); @@ -232,10 +235,11 @@ public void Add_Data_File_And_One_Replace_And_Not_Replace_Duplicate_Element(List public async Task Add_Data_File_And_One_Replace_And_Not_Replace_Duplicate_ElementAsync(List items, List addDownloadItem, Dictionary dictionaryAddItem, List expectItems) { // Arrange + var logger = new TestLogger(); var dbContext = new TestAppDbContextFactory().Create(); - var itemRepository = new WatchItemRepository(dbContext); + var itemRepository = new WatchItemRepository(dbContext, logger); var dbContextDownloadItem = new TestAppDbContextFactory().Create(); - var watchItemRepository = new WatchItemRepository(dbContext); + var watchItemRepository = new WatchItemRepository(dbContext, logger); var messageBox = new Mock(); foreach (var item in dictionaryAddItem) @@ -243,7 +247,7 @@ public async Task Add_Data_File_And_One_Replace_And_Not_Replace_Duplicate_Elemen messageBox.Setup(foo => foo.ShowDataReplaceQuestion(item.Key)).Returns(item.Value); } - var service = new DownloadDataService(watchItemRepository, messageBox.Object); + var service = new DownloadDataService(watchItemRepository, messageBox.Object, logger); var loadRuleConfig = new TestLoadRuleConfig() { DeleteGrade = false, @@ -254,7 +258,7 @@ public async Task Add_Data_File_And_One_Replace_And_Not_Replace_Duplicate_Elemen }), }; var loadRule = new TestAggregateLoadRule(itemRepository, loadRuleConfig); - var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem); + var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem, logger); dbContext.AddRange(items); dbContextDownloadItem.AddRange(addDownloadItem); diff --git a/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadDataByRuleMoreGradeTest.cs b/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadDataByRuleMoreGradeTest.cs index f9aafc3a..a3729c40 100644 --- a/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadDataByRuleMoreGradeTest.cs +++ b/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadDataByRuleMoreGradeTest.cs @@ -128,18 +128,19 @@ public class DownloadDataByRuleMoreGradeTest public void Load_Data_File_By_Rule_Type_Cinema(List items, List addDownloadItem, Grade moreGrade, List expectItems) { // Arrange + var logger = new TestLogger(); var dbContext = new TestAppDbContextFactory().Create(); - var itemRepository = new WatchItemRepository(dbContext); + var itemRepository = new WatchItemRepository(dbContext, logger); var dbContextDownloadItem = new TestAppDbContextFactory().Create(); - var watchItemRepository = new WatchItemRepository(dbContext); + var watchItemRepository = new WatchItemRepository(dbContext, logger); var messageBox = new Mock(); messageBox.Setup(foo => foo.ShowDataReplaceQuestion(It.IsAny())).Returns(DialogReplaceItemQuestion.AllYes); - var service = new DownloadDataService(watchItemRepository, messageBox.Object); + var service = new DownloadDataService(watchItemRepository, messageBox.Object, logger); var loadRuleConfig = new TestLoadRuleConfig() { MoreGrade = moreGrade }; var loadRule = new TestAggregateLoadRule(itemRepository, loadRuleConfig); - var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem); + var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem, logger); dbContext.AddRange(items); dbContextDownloadItem.AddRange(addDownloadItem); @@ -161,18 +162,19 @@ public void Load_Data_File_By_Rule_Type_Cinema(List items, List items, List addDownloadItem, Grade moreGrade, List expectItems) { // Arrange + var logger = new TestLogger(); var dbContext = new TestAppDbContextFactory().Create(); - var itemRepository = new WatchItemRepository(dbContext); + var itemRepository = new WatchItemRepository(dbContext, logger); var dbContextDownloadItem = new TestAppDbContextFactory().Create(); - var watchItemRepository = new WatchItemRepository(dbContext); + var watchItemRepository = new WatchItemRepository(dbContext, logger); var messageBox = new Mock(); messageBox.Setup(foo => foo.ShowDataReplaceQuestion(It.IsAny())).Returns(DialogReplaceItemQuestion.AllYes); - var service = new DownloadDataService(watchItemRepository, messageBox.Object); + var service = new DownloadDataService(watchItemRepository, messageBox.Object, logger); var loadRuleConfig = new TestLoadRuleConfig() { MoreGrade = moreGrade }; var loadRule = new TestAggregateLoadRule(itemRepository, loadRuleConfig); - var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem); + var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem, logger); dbContext.AddRange(items); dbContextDownloadItem.AddRange(addDownloadItem); diff --git a/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadDataByRuleTypeCinemaTest.cs b/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadDataByRuleTypeCinemaTest.cs index 555af5a0..abd12f29 100644 --- a/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadDataByRuleTypeCinemaTest.cs +++ b/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadDataByRuleTypeCinemaTest.cs @@ -126,18 +126,19 @@ public class DownloadDataByRuleTypeCinemaTest public void Load_Data_File_By_Rule_Type_Cinema(List items, List addDownloadItem, TypeCinema typeCinema, List expectItems) { // Arrange + var logger = new TestLogger(); var dbContext = new TestAppDbContextFactory().Create(); - var itemRepository = new WatchItemRepository(dbContext); + var itemRepository = new WatchItemRepository(dbContext, logger); var dbContextDownloadItem = new TestAppDbContextFactory().Create(); - var watchItemRepository = new WatchItemRepository(dbContext); + var watchItemRepository = new WatchItemRepository(dbContext, logger); var messageBox = new Mock(); messageBox.Setup(foo => foo.ShowDataReplaceQuestion(It.IsAny())).Returns(DialogReplaceItemQuestion.AllYes); - var service = new DownloadDataService(watchItemRepository, messageBox.Object); + var service = new DownloadDataService(watchItemRepository, messageBox.Object, logger); var loadRuleConfig = new TestLoadRuleConfig() { TypeCinemaLoad = typeCinema }; var loadRule = new TestAggregateLoadRule(itemRepository, loadRuleConfig); - var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem); + var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem, logger); dbContext.AddRange(items); dbContextDownloadItem.AddRange(addDownloadItem); @@ -159,18 +160,19 @@ public void Load_Data_File_By_Rule_Type_Cinema(List items, List items, List addDownloadItem, TypeCinema typeCinema, List expectItems) { // Arrange + var logger = new TestLogger(); var dbContext = new TestAppDbContextFactory().Create(); - var itemRepository = new WatchItemRepository(dbContext); + var itemRepository = new WatchItemRepository(dbContext, logger); var dbContextDownloadItem = new TestAppDbContextFactory().Create(); - var watchItemRepository = new WatchItemRepository(dbContext); + var watchItemRepository = new WatchItemRepository(dbContext, logger); var messageBox = new Mock(); messageBox.Setup(foo => foo.ShowDataReplaceQuestion(It.IsAny())).Returns(DialogReplaceItemQuestion.AllYes); - var service = new DownloadDataService(watchItemRepository, messageBox.Object); + var service = new DownloadDataService(watchItemRepository, messageBox.Object, logger); var loadRuleConfig = new TestLoadRuleConfig() { TypeCinemaLoad = typeCinema }; var loadRule = new TestAggregateLoadRule(itemRepository, loadRuleConfig); - var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem); + var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem, logger); dbContext.AddRange(items); dbContextDownloadItem.AddRange(addDownloadItem); diff --git a/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadPagedListTest.cs b/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadPagedListTest.cs index 3fd80614..6d357b2a 100644 --- a/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadPagedListTest.cs +++ b/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadPagedListTest.cs @@ -49,18 +49,19 @@ public class DownloadPagedListTest public void Add_Data_File(List items, List addDownloadItem, List expectItems) { // Arrange + var logger = new TestLogger(); var dbContext = new TestAppDbContextFactory().Create(); - var itemRepository = new WatchItemRepository(dbContext); + var itemRepository = new WatchItemRepository(dbContext, logger); var dbContextDownloadItem = new TestAppDbContextFactory().Create(); - var watchItemRepository = new WatchItemRepository(dbContext); + var watchItemRepository = new WatchItemRepository(dbContext, logger); var messageBox = new Mock(); messageBox.Setup(foo => foo.ShowDataReplaceQuestion(It.IsAny())).Returns(DialogReplaceItemQuestion.AllYes); - var service = new DownloadDataService(watchItemRepository, messageBox.Object) { NumberOfItemPerPage = PageSize }; + var service = new DownloadDataService(watchItemRepository, messageBox.Object, logger) { NumberOfItemPerPage = PageSize }; var loadRuleConfig = new TestLoadRuleConfig(); var loadRule = new TestAggregateLoadRule(itemRepository, loadRuleConfig); - var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem); + var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem, logger); dbContext.AddRange(items); dbContextDownloadItem.AddRange(addDownloadItem); @@ -80,18 +81,19 @@ public void Add_Data_File(List items, List addDownloadItem public async Task Add_Data_FileAsync(List items, List addDownloadItem, List expectItems) { // Arrange + var logger = new TestLogger(); var dbContext = new TestAppDbContextFactory().Create(); - var itemRepository = new WatchItemRepository(dbContext); + var itemRepository = new WatchItemRepository(dbContext, logger); var dbContextDownloadItem = new TestAppDbContextFactory().Create(); - var watchItemRepository = new WatchItemRepository(dbContext); + var watchItemRepository = new WatchItemRepository(dbContext, logger); var messageBox = new Mock(); messageBox.Setup(foo => foo.ShowDataReplaceQuestion(It.IsAny())).Returns(DialogReplaceItemQuestion.AllYes); - var service = new DownloadDataService(watchItemRepository, messageBox.Object) { NumberOfItemPerPage = PageSize }; + var service = new DownloadDataService(watchItemRepository, messageBox.Object, logger) { NumberOfItemPerPage = PageSize }; var loadRuleConfig = new TestLoadRuleConfig(); var loadRule = new TestAggregateLoadRule(itemRepository, loadRuleConfig); - var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem); + var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem, logger); dbContext.AddRange(items); dbContextDownloadItem.AddRange(addDownloadItem); diff --git a/WatchList.Test/CoreTest/WatchItemServiceTest/NonDuplicateElementsTest.cs b/WatchList.Test/CoreTest/WatchItemServiceTest/NonDuplicateElementsTest.cs index 4b558a87..4a733044 100644 --- a/WatchList.Test/CoreTest/WatchItemServiceTest/NonDuplicateElementsTest.cs +++ b/WatchList.Test/CoreTest/WatchItemServiceTest/NonDuplicateElementsTest.cs @@ -67,7 +67,7 @@ public void Add_New_Item_In_Database(List items, WatchItem addItem, L { // Arrange var dbContext = new TestAppDbContextFactory().Create(); - var itemRepository = new WatchItemRepository(dbContext); + var itemRepository = new WatchItemRepository(dbContext, new TestLogger()); var messageBox = new Mock(); messageBox.Setup(foo => foo.ShowQuestionSaveItem(WatchItemService.DuplicateReplaceMessage)).Returns(true); var service = new WatchItemService(itemRepository, messageBox.Object); @@ -88,7 +88,7 @@ public void Update_Item_In_Database(List items, WatchItem oldItem, Wa { // Arrange var dbContext = new TestAppDbContextFactory().Create(); - var itemRepository = new WatchItemRepository(dbContext); + var itemRepository = new WatchItemRepository(dbContext, new TestLogger()); var messageBox = new Mock(); messageBox.Setup(foo => foo.ShowQuestionSaveItem(WatchItemService.DuplicateReplaceMessage)).Returns(true); var service = new WatchItemService(itemRepository, messageBox.Object); diff --git a/WatchList.Test/CoreTest/WatchItemServiceTest/UpdateDuplicateItemTest.cs b/WatchList.Test/CoreTest/WatchItemServiceTest/UpdateDuplicateItemTest.cs index 9420ab40..633a6d2f 100644 --- a/WatchList.Test/CoreTest/WatchItemServiceTest/UpdateDuplicateItemTest.cs +++ b/WatchList.Test/CoreTest/WatchItemServiceTest/UpdateDuplicateItemTest.cs @@ -58,7 +58,7 @@ public void Update_With_Replace_Duplicate_Element(List items, WatchIt { // Arrange var dbContext = new TestAppDbContextFactory().Create(); - var itemRepository = new WatchItemRepository(dbContext); + var itemRepository = new WatchItemRepository(dbContext, new TestLogger()); var messageBox = new Mock(); messageBox.Setup(foo => foo.ShowQuestionSaveItem(WatchItemService.DuplicateReplaceMessage)).Returns(true); var service = new WatchItemService(itemRepository, messageBox.Object); @@ -79,7 +79,7 @@ public void Update_With_Not_Replace_Duplicate_Element(List items, Wat { // Arrange var dbContext = new TestAppDbContextFactory().Create(); - var itemRepository = new WatchItemRepository(dbContext); + var itemRepository = new WatchItemRepository(dbContext, new TestLogger()); var messageBox = new Mock(); messageBox.Setup(foo => foo.ShowQuestionSaveItem(WatchItemService.DuplicateReplaceMessage)).Returns(false); var service = new WatchItemService(itemRepository, messageBox.Object); diff --git a/WatchList.Test/WatchList.Test.csproj b/WatchList.Test/WatchList.Test.csproj index eaa0f811..d679f479 100644 --- a/WatchList.Test/WatchList.Test.csproj +++ b/WatchList.Test/WatchList.Test.csproj @@ -16,6 +16,7 @@ + diff --git a/WatchList.WinForms/BoxCinemaForm.Designer.cs b/WatchList.WinForms/BoxCinemaForm.Designer.cs index 81f4e334..d5123b88 100644 --- a/WatchList.WinForms/BoxCinemaForm.Designer.cs +++ b/WatchList.WinForms/BoxCinemaForm.Designer.cs @@ -610,6 +610,7 @@ private void InitializeComponent() Name = "BoxCinemaForm"; StartPosition = FormStartPosition.CenterScreen; Text = "BoxCinema"; + FormClosing += BoxCinemaForm_FormClosing; ((System.ComponentModel.ISupportInitialize)cinemaBindingSource).EndInit(); ((System.ComponentModel.ISupportInitialize)dgvCinema).EndInit(); ((System.ComponentModel.ISupportInitialize)filterModelBindingSource).EndInit(); diff --git a/WatchList.WinForms/BoxCinemaForm.cs b/WatchList.WinForms/BoxCinemaForm.cs index d81dae61..0819be91 100644 --- a/WatchList.WinForms/BoxCinemaForm.cs +++ b/WatchList.WinForms/BoxCinemaForm.cs @@ -1,5 +1,6 @@ using MaterialSkin.Controls; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using WatchList.Core.Model.Filter.Components; using WatchList.Core.Model.ItemCinema; using WatchList.Core.Model.ItemCinema.Components; @@ -36,6 +37,7 @@ public partial class BoxCinemaForm : MaterialForm private readonly WatchItemService _itemService; private readonly IMessageBox _messageBox; private readonly WatchItemRepository _itemRepository; + private readonly ILogger _logger; private WatchItemSearchRequest _searchRequest = new WatchItemSearchRequest(); @@ -48,8 +50,8 @@ public BoxCinemaForm(IServiceProvider serviceProvider) _itemRepository = serviceProvider.GetRequiredService(); _messageBox = serviceProvider.GetRequiredService(); _itemService = serviceProvider.GetRequiredService(); + _logger = serviceProvider.GetRequiredService(); _pagedList = _itemService.GetPage(_searchRequest); - Load += BoxCinemaForm_Load; } @@ -95,6 +97,7 @@ private void BtnAddCinema_Click(object sender, EventArgs e) return; } + _logger.LogInformation("Click save add item"); var itemCinema = addForm.GetCinema(); _itemService.Add(itemCinema.ToWatchItem()); @@ -114,6 +117,7 @@ private void BtnEditRow_Click(object sender, EventArgs e) return; } + _logger.LogInformation("Click save edit item"); var updateItem = updateForm.GetEditItemCinema(); _itemService.Update(oldItem.ToWatchItem(), updateItem.ToWatchItem()); UpdateGridData(); @@ -141,6 +145,7 @@ private void BtnDeleteMovie_Click(object sender, EventArgs e) return; } + _logger.LogInformation("Click delite item"); foreach (var id in selectedRowIds) { RemoveItemRowGrid(id); @@ -164,6 +169,7 @@ private void BtnDownloadDataFile_Click(object sender, EventArgs e) return; } + _logger.LogInformation("Add item from the selected file <{0}>", openReplaceDataFromFile.FileName); var dbContext = new FileDbContextFactory(openReplaceDataFromFile.FileName).Create(); var loadRuleConfig = dataLoadingForm.GetLoadRuleConfig(); var loadRuleHasGrade = new DeleteGradeRule(loadRuleConfig); @@ -172,13 +178,18 @@ private void BtnDownloadDataFile_Click(object sender, EventArgs e) var loadDuplicateItem = new DuplicateLoadRule(_itemRepository, loadRuleConfig); var aggregateRules = new AggregateLoadRule { loadRuleHasGrade, loadRuleType, loadRuleMoreGrade, loadDuplicateItem }; - var repositoryDataDownload = new WatchItemRepository(dbContext); + var repositoryDataDownload = new WatchItemRepository(dbContext, _logger); var downloadDataService = _serviceProvider.GetRequiredService(); downloadDataService.Download(repositoryDataDownload, aggregateRules); UpdateGridData(); } + private void BoxCinemaForm_FormClosing(object sender, FormClosingEventArgs e) + { + _logger.LogTrace("Close App."); + } + private void BtnBackPage_Click(object sender, EventArgs e) { if (_pagedList.HasPreviousPage) diff --git a/WatchList.WinForms/Program.cs b/WatchList.WinForms/Program.cs index b56d4022..a0ec4281 100644 --- a/WatchList.WinForms/Program.cs +++ b/WatchList.WinForms/Program.cs @@ -1,5 +1,7 @@ using MaterialSkin; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using WatchList.Core.Logger; using WatchList.Core.Repository; using WatchList.Core.Service; using WatchList.Core.Service.Component; @@ -23,6 +25,9 @@ public static void Main() // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); + var path = "logs"; + CreatDirectoryIfNotExists(path); + var serviceCollection = new ServiceCollection() .AddSingleton(new FileDbContextFactory("app.db")) .AddScoped(e => e.GetRequiredService().Create()) @@ -32,7 +37,8 @@ public static void Main() .AddScoped() .AddScoped() .AddTransient() - .AddTransient(); + .AddTransient() + .AddSingleton(e => new AggregateLogging() { new ConsoleLogger(LogLevel.Trace), new FileLogger(LogLevel.Trace, path) }); using var contain = serviceCollection.BuildServiceProvider(new ServiceProviderOptions { @@ -50,7 +56,25 @@ public static void Main() materialSkinManager.Theme = MaterialSkinManager.Themes.LIGHT; materialSkinManager.ColorScheme = new ColorScheme(Primary.Purple900, Primary.DeepPurple700, Primary.Purple50, Accent.LightBlue200, TextShade.WHITE); - Application.Run(form); + var loger = scope.ServiceProvider.GetRequiredService(); + + try + { + loger.LogTrace("Launch the application"); + Application.Run(form); + } + catch (Exception ex) + { + loger.LogError(ex, "Unhandled exception"); + } + } + + private static void CreatDirectoryIfNotExists(string path) + { + if (!Directory.Exists(path)) + { + Directory.CreateDirectory(path); + } } } } diff --git a/WatchList.WinForms/WatchList.WinForms.csproj b/WatchList.WinForms/WatchList.WinForms.csproj index d0e8ad5a..35994f9f 100644 --- a/WatchList.WinForms/WatchList.WinForms.csproj +++ b/WatchList.WinForms/WatchList.WinForms.csproj @@ -15,6 +15,7 @@ + all runtime; build; native; contentfiles; analyzers; buildtransitive