-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
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 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() | ||
{ | ||
} | ||
} | ||
} | ||
} |
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 class FileLogger : ILogger | ||
{ | ||
private LogLevel _logLevel; | ||
private readonly string _pathFileLog; | ||
Check warning on line 9 in WatchList.Core/Logger/FileLogger.cs GitHub Actions / build
Check warning on line 9 in WatchList.Core/Logger/FileLogger.cs GitHub Actions / build
Check warning on line 9 in WatchList.Core/Logger/FileLogger.cs GitHub Actions / Lint
Check warning on line 9 in WatchList.Core/Logger/FileLogger.cs GitHub Actions / build
Check warning on line 9 in WatchList.Core/Logger/FileLogger.cs GitHub Actions / build
|
||
|
||
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 sealed class Disposable : IDisposable | ||
{ | ||
public void Dispose() | ||
{ | ||
} | ||
} | ||
|
||
private string BuildPath() | ||
Check warning on line 42 in WatchList.Core/Logger/FileLogger.cs GitHub Actions / build
Check warning on line 42 in WatchList.Core/Logger/FileLogger.cs GitHub Actions / build
Check warning on line 42 in WatchList.Core/Logger/FileLogger.cs GitHub Actions / Lint
Check warning on line 42 in WatchList.Core/Logger/FileLogger.cs GitHub Actions / build
Check warning on line 42 in WatchList.Core/Logger/FileLogger.cs GitHub Actions / build
|
||
{ | ||
var dateStr = DateTime.Now.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture); | ||
return Path.Combine(_pathFileLog, dateStr + ".txt"); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<FileDbContextFactory>().Create()) | ||
|
@@ -32,7 +37,9 @@ public static void Main() | |
.AddScoped<WatchItemService>() | ||
.AddScoped<DownloadDataService>() | ||
.AddTransient<MergeDatabaseForm>() | ||
.AddTransient<BoxCinemaForm>(); | ||
.AddTransient<BoxCinemaForm>() | ||
//.AddTransient<ILogger>(e => new ConsoleLogger(LogLevel.Information)) | ||
Check warning on line 41 in WatchList.WinForms/Program.cs GitHub Actions / build
Check warning on line 41 in WatchList.WinForms/Program.cs GitHub Actions / build
Check warning on line 41 in WatchList.WinForms/Program.cs GitHub Actions / build
Check warning on line 41 in WatchList.WinForms/Program.cs GitHub Actions / Lint
Check warning on line 41 in WatchList.WinForms/Program.cs GitHub Actions / Lint
Check warning on line 41 in WatchList.WinForms/Program.cs GitHub Actions / build
Check warning on line 41 in WatchList.WinForms/Program.cs GitHub Actions / build
Check warning on line 41 in WatchList.WinForms/Program.cs GitHub Actions / build
|
||
.AddTransient<ILogger>(e => new FileLogger(LogLevel.Trace, path)); | ||
|
||
using var contain = serviceCollection.BuildServiceProvider(new ServiceProviderOptions | ||
{ | ||
|
@@ -50,7 +57,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<ILogger>(); | ||
|
||
try | ||
{ | ||
loger.LogTrace("Launch the application"); | ||
Application.Run(form); | ||
} | ||
catch (Exception ex) | ||
{ | ||
loger.LogError(ex, "Unhandled exception"); | ||
} | ||
} | ||
|
||
static void CreatDirectoryIfNotExists(string path) | ||
Check warning on line 73 in WatchList.WinForms/Program.cs GitHub Actions / build
Check warning on line 73 in WatchList.WinForms/Program.cs GitHub Actions / Lint
Check warning on line 73 in WatchList.WinForms/Program.cs GitHub Actions / build
|
||
{ | ||
if (!Directory.Exists(path)) | ||
{ | ||
Directory.CreateDirectory(path); | ||
} | ||
} | ||
} | ||
} |