Skip to content

Commit

Permalink
Solution: WatchList.Core - Add class AggregateLogging.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stan-Kudri committed Oct 13, 2023
1 parent 41648db commit bbb2689
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 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

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()
{
}
}
}
}
2 changes: 1 addition & 1 deletion WatchList.WinForms/BoxCinemaForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions WatchList.WinForms/BoxCinemaForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ private void BtnDownloadDataFile_Click(object sender, EventArgs e)
UpdateGridData();
}

private void BoxCinemaForm_FormClosed(object sender, FormClosedEventArgs e)
private void BoxCinemaForm_FormClosing(object sender, FormClosingEventArgs e)
{
_logger.LogInformation("Close App.");
_logger.LogTrace("Close App.");
}

private void BtnBackPage_Click(object sender, EventArgs e)
Expand Down
10 changes: 7 additions & 3 deletions WatchList.WinForms/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.Logging;
using WatchList.Core.Logger;
using WatchList.Core.Repository;
using WatchList.Core.Repository.Db;
using WatchList.Core.Service;
using WatchList.Core.Service.Component;
using WatchList.Core.Service.DataLoading;
Expand Down Expand Up @@ -32,14 +33,17 @@ public static void Main()
.AddSingleton(new FileDbContextFactory("app.db"))
.AddScoped(e => e.GetRequiredService<FileDbContextFactory>().Create())
.AddScoped<DbMigrator>()
.AddScoped<WatchItemRepository>()
.AddScoped<WatchItemRepository>(e => new WatchItemRepository(e.GetRequiredService<WatchCinemaDbContext>(), e.GetRequiredService<AggregateLogging>()))
.AddScoped<IMessageBox, MessageBoxShow>()
.AddScoped<WatchItemService>()
.AddScoped<DownloadDataService>()
.AddTransient<MergeDatabaseForm>()
.AddTransient<BoxCinemaForm>()
.AddTransient<ILogger>(e => new ConsoleLogger(LogLevel.Information))
.AddTransient<ILogger>(e => new FileLogger(LogLevel.Trace, path));
.AddSingleton(e => new ConsoleLogger(LogLevel.Information))
.AddSingleton(e => new FileLogger(LogLevel.Trace, path))
.AddSingleton<ILogger>(e => e.GetRequiredService<ConsoleLogger>())
.AddSingleton<ILogger>(e => e.GetRequiredService<FileLogger>())
.AddSingleton(e => new AggregateLogging() { e.GetRequiredService<ConsoleLogger>(), e.GetRequiredService<FileLogger>() });

using var contain = serviceCollection.BuildServiceProvider(new ServiceProviderOptions
{
Expand Down

0 comments on commit bbb2689

Please sign in to comment.