Skip to content

Commit

Permalink
Feature/#69. (#70)
Browse files Browse the repository at this point in the history
* Solution - Add and used Logger.

* WathList.Test - Chang the initialization of class instances to new ones.

* Solution - Fixed Linter.

* Solution: WatchList.Core - Add class AggregateLogging.

* Solution: WatchList.WinForms - Changed contain DI.
  • Loading branch information
Stan-Kudri authored Oct 16, 2023
1 parent 97a8f97 commit e3aa8a6
Show file tree
Hide file tree
Showing 20 changed files with 279 additions and 54 deletions.
45 changes: 45 additions & 0 deletions WatchList.Core/Logger/AggregateLogging.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.Extensions.Logging;

namespace WatchList.Core.Logger
{
public class AggregateLogging : List<ILogger>, ILogger
{
private readonly LogLevel _logLevel;

Check warning on line 7 in WatchList.Core/Logger/AggregateLogging.cs

View workflow job for this annotation

GitHub Actions / build

Field 'AggregateLogging._logLevel' is never assigned to, and will always have its default value

Check warning on line 7 in WatchList.Core/Logger/AggregateLogging.cs

View workflow job for this annotation

GitHub Actions / build

Field 'AggregateLogging._logLevel' is never assigned to, and will always have its default value

public AggregateLogging()
: base()
{
}

public AggregateLogging(int capacity)
: base(capacity)
{
}

public AggregateLogging(IEnumerable<ILogger> collection)
: base(collection)
{
}

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
foreach (var loggin in this)
{
loggin.Log(logLevel, eventId, state, exception, formatter);
}
}

public bool IsEnabled(LogLevel logLevel) => _logLevel <= logLevel;

public IDisposable? BeginScope<TState>(TState state)
where TState : notnull
=> new Disposable();

private sealed class Disposable : IDisposable
{
public void Dispose()
{
}
}
}
}
35 changes: 35 additions & 0 deletions WatchList.Core/Logger/ConsoleLogger.cs
Original file line number Diff line number Diff line change
@@ -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<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> 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>(TState state)
where TState : notnull
=> new Disposable();

private sealed class Disposable : IDisposable
{
public void Dispose()
{
}
}
}
}
48 changes: 48 additions & 0 deletions WatchList.Core/Logger/FileLogger.cs
Original file line number Diff line number Diff line change
@@ -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<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> 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>(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()
{
}
}
}
}
13 changes: 12 additions & 1 deletion WatchList.Core/Repository/WatchItemRepository.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<WatchItem> GetPage(WatchItemSearchRequest searchRequest)
{
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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<Guid> SelectDuplicateItems(WatchItem item) =>
Expand Down
7 changes: 5 additions & 2 deletions WatchList.Core/Service/DataLoading/DownloadDataService.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Extensions.Logging;
using WatchList.Core.Model.Filter;
using WatchList.Core.Model.QuestionResult;
using WatchList.Core.Model.Sorting;
Expand All @@ -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;
}

Expand All @@ -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);

Expand Down
1 change: 1 addition & 0 deletions WatchList.Core/WatchList.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PackageReference Include="Ardalis.SmartEnum.SystemTextJson" Version="2.1.0" />
<PackageReference Include="CassandraCSharpDriver" Version="3.18.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.7" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
34 changes: 34 additions & 0 deletions WatchList.Test/CoreTest/TestLogger.cs
Original file line number Diff line number Diff line change
@@ -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<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
if (!IsEnabled(logLevel))
{
return;
}

var message = formatter(state, exception);
}

public bool IsEnabled(LogLevel logLevel) => _logLevel <= logLevel;

public IDisposable? BeginScope<TState>(TState state)
where TState : notnull
=> new Disposable();

private sealed class Disposable : IDisposable
{
public void Dispose()
{
}
}
}
}
4 changes: 2 additions & 2 deletions WatchList.Test/CoreTest/WatchItemRepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void Add_With_Replace_Duplicate_Element(List<WatchItem> items, WatchItem
{
// Arrange
var dbContext = new TestAppDbContextFactory().Create();
var itemRepository = new WatchItemRepository(dbContext);
var itemRepository = new WatchItemRepository(dbContext, new TestLogger());
var messageBox = new Mock<IMessageBox>();
messageBox.Setup(foo => foo.ShowQuestionSaveItem(WatchItemService.DuplicateReplaceMessage)).Returns(true);
var service = new WatchItemService(itemRepository, messageBox.Object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,16 @@ public class DownloadDataByDeleteGradeRuleTest
public void Add_Data_File_And_Replace_Duplicate_Element(List<WatchItem> items, List<WatchItem> addDownloadItem, DialogReplaceItemQuestion dialogReplaceItem, List<WatchItem> 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<IMessageBox>();
messageBox.Setup(foo => foo.ShowDataReplaceQuestion(It.IsAny<string>())).Returns(dialogReplaceItem);

var service = new DownloadDataService(watchItemRepository, messageBox.Object);
var service = new DownloadDataService(watchItemRepository, messageBox.Object, logger);
var loadRuleConfig = new TestLoadRuleConfig()
{
DeleteGrade = false,
Expand All @@ -130,7 +131,7 @@ public void Add_Data_File_And_Replace_Duplicate_Element(List<WatchItem> 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);
Expand All @@ -151,15 +152,16 @@ public void Add_Data_File_And_Replace_Duplicate_Element(List<WatchItem> items, L
public async Task Add_Data_File_And_Replace_Duplicate_ElementAsync(List<WatchItem> items, List<WatchItem> addDownloadItem, DialogReplaceItemQuestion dialogReplaceItem, List<WatchItem> 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<IMessageBox>();
messageBox.Setup(foo => foo.ShowDataReplaceQuestion(It.IsAny<string>())).Returns(dialogReplaceItem);

var service = new DownloadDataService(watchItemRepository, messageBox.Object);
var service = new DownloadDataService(watchItemRepository, messageBox.Object, logger);
var loadRuleConfig = new TestLoadRuleConfig()
{
DeleteGrade = false,
Expand All @@ -170,7 +172,7 @@ public async Task Add_Data_File_And_Replace_Duplicate_ElementAsync(List<WatchIte
}),
};
var loadRule = new TestAggregateLoadRule(itemRepository, loadRuleConfig);
var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem);
var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem, logger);

dbContext.AddRange(items);
dbContextDownloadItem.AddRange(addDownloadItem);
Expand All @@ -190,18 +192,19 @@ public async Task Add_Data_File_And_Replace_Duplicate_ElementAsync(List<WatchIte
public void Add_Data_File_And_One_Replace_And_Not_Replace_Duplicate_Element(List<WatchItem> items, List<WatchItem> addDownloadItem, Dictionary<string, DialogReplaceItemQuestion> dictionaryAddItem, List<WatchItem> 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<IMessageBox>();
foreach (var item in dictionaryAddItem)
{
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,
Expand All @@ -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);
Expand All @@ -232,18 +235,19 @@ 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<WatchItem> items, List<WatchItem> addDownloadItem, Dictionary<string, DialogReplaceItemQuestion> dictionaryAddItem, List<WatchItem> 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<IMessageBox>();
foreach (var item in dictionaryAddItem)
{
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,
Expand All @@ -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);
Expand Down
Loading

0 comments on commit e3aa8a6

Please sign in to comment.