Loggin WIP

This commit is contained in:
Ádám Kovács
2022-11-02 11:31:32 +01:00
parent 642c30d1ce
commit 5c2214fa50
16 changed files with 166 additions and 39 deletions

View File

@@ -2,6 +2,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Alma.Abstraction\Alma.Abstraction.csproj" /> <ProjectReference Include="..\Alma.Abstraction\Alma.Abstraction.csproj" />
<ProjectReference Include="..\Alma.Logging\Alma.Logging.csproj" />
</ItemGroup> </ItemGroup>
<PropertyGroup> <PropertyGroup>

View File

@@ -1,22 +1,25 @@
using Alma.Command; using Alma.Command;
using Alma.Command.Help; using Alma.Command.Help;
using Alma.Logging;
namespace Alma; namespace Alma;
public class Application public class Application
{ {
private readonly IList<ICommand> _commands; private readonly IList<ICommand> _commands;
private readonly ILogger<Application> _logger;
public Application(IEnumerable<ICommand> commands) public Application(IEnumerable<ICommand> commands, ILogger<Application> logger, ILogger<HelpCommand> helpCommandLogger)
{ {
_commands = commands.Append(new HelpCommand(() => _commands!)).ToList(); _commands = commands.Append(new HelpCommand(() => _commands!, helpCommandLogger)).ToList();
_logger = logger;
} }
public async Task Run(string[] args) public async Task Run(string[] args)
{ {
if (args.Length == 0) if (args.Length == 0)
{ {
Console.WriteLine("No command was given"); _logger.LogInformation("No command was given");
return; return;
} }
@@ -26,7 +29,7 @@ public class Application
if (command is null) if (command is null)
{ {
Console.WriteLine($"Invalid command: {commandString}"); _logger.LogInformation($"Invalid command: {commandString}");
return; return;
} }

View File

@@ -1,22 +1,30 @@
using Alma.Logging;
namespace Alma.Command.Help; namespace Alma.Command.Help;
public class HelpCommand : ICommand public class HelpCommand : ICommand
{ {
private readonly Func<IEnumerable<ICommand>> _commandsProvider; private readonly Func<IEnumerable<ICommand>> _commandsProvider;
private readonly ILogger<HelpCommand> _logger;
public string CommandString => "help"; public string CommandString => "help";
public HelpCommand(Func<IEnumerable<ICommand>> commandsProvider) public HelpCommand(
Func<IEnumerable<ICommand>> commandsProvider,
ILogger<HelpCommand> logger
)
{ {
_commandsProvider = commandsProvider; _commandsProvider = commandsProvider;
_logger = logger;
} }
public Task Run(List<string> parameters) public Task Run(List<string> parameters)
{ {
Console.WriteLine("Commands:" + Environment.NewLine); _logger.LogInformation("Commands:" + Environment.NewLine);
foreach (var command in _commandsProvider().OrderBy(c => c.CommandString)) foreach (var command in _commandsProvider().OrderBy(c => c.CommandString))
{ {
Console.WriteLine(command.CommandString); _logger.LogInformation(command.CommandString);
} }
return Task.CompletedTask; return Task.CompletedTask;

View File

@@ -1,4 +1,5 @@
using Alma.Configuration.Repository; using Alma.Configuration.Repository;
using Alma.Logging;
using Alma.Services; using Alma.Services;
namespace Alma.Command.Info; namespace Alma.Command.Info;
@@ -9,37 +10,40 @@ public class ModuleInfoCommand : ICommand
private readonly IFolderService _folderService; private readonly IFolderService _folderService;
private readonly IRepositoryConfiguration _repositoryConfiguration; private readonly IRepositoryConfiguration _repositoryConfiguration;
private readonly ILogger<ModuleInfoCommand> _logger;
public ModuleInfoCommand( public ModuleInfoCommand(
IFolderService folderService, IFolderService folderService,
IRepositoryConfiguration repositoryConfiguration IRepositoryConfiguration repositoryConfiguration,
ILogger<ModuleInfoCommand> logger
) )
{ {
_folderService = folderService; _folderService = folderService;
_repositoryConfiguration = repositoryConfiguration; _repositoryConfiguration = repositoryConfiguration;
_logger = logger;
} }
public Task Run(List<string> parameters) public Task Run(List<string> parameters)
{ {
//Add info REPO //Add info REPO
//Add info REPO MODULE //Add info REPO MODULE
Console.WriteLine("AppData folder: " + _folderService.AppData); _logger.LogInformation("AppData folder: " + _folderService.AppData);
if (_folderService.ConfigRoot is string configRoot) if (_folderService.ConfigRoot is string configRoot)
{ {
Console.WriteLine("Configuration folder: " + configRoot); _logger.LogInformation("Configuration folder: " + configRoot);
} }
else else
{ {
Console.WriteLine("Configuration folder not exists."); _logger.LogInformation("Configuration folder not exists.");
Console.WriteLine("Preffered configuration folder is: " + Path.Combine(_folderService.GetPreferredConfigurationFolder(), _folderService.ApplicationSubfolderName)); _logger.LogInformation("Preffered configuration folder is: " + Path.Combine(_folderService.GetPreferredConfigurationFolder(), _folderService.ApplicationSubfolderName));
} }
Console.WriteLine(); _logger.LogInformation("");
if (_repositoryConfiguration.Configuration.Repositories is var repositores && repositores?.Count > 0) if (_repositoryConfiguration.Configuration.Repositories is var repositores && repositores?.Count > 0)
{ {
Console.WriteLine("Repositories:"); _logger.LogInformation("Repositories:");
foreach (var repository in repositores) foreach (var repository in repositores)
{ {
Console.Write(repository.Name); Console.Write(repository.Name);
@@ -47,12 +51,12 @@ public class ModuleInfoCommand : ICommand
{ {
Console.Write($" (containing folder not exists {repository.RepositoryPath})"); Console.Write($" (containing folder not exists {repository.RepositoryPath})");
} }
Console.WriteLine(); _logger.LogInformation("");
} }
} }
else else
{ {
Console.WriteLine("No repositories found"); _logger.LogInformation("No repositories found");
} }
return Task.CompletedTask; return Task.CompletedTask;

View File

@@ -2,6 +2,7 @@ using System.Runtime.InteropServices;
using Alma.Configuration.Module; using Alma.Configuration.Module;
using Alma.Configuration.Repository; using Alma.Configuration.Repository;
using Alma.Data; using Alma.Data;
using Alma.Logging;
using Alma.Services; using Alma.Services;
namespace Alma.Command.Link; namespace Alma.Command.Link;
@@ -10,27 +11,28 @@ public class LinkCommand : ICommand
{ {
private readonly IRepositoryConfiguration _repositoryConfiguration; private readonly IRepositoryConfiguration _repositoryConfiguration;
private readonly IModuleConfigurationResolver _moduleConfigurationResolver; private readonly IModuleConfigurationResolver _moduleConfigurationResolver;
private readonly IFolderService _folderService;
private readonly IMetadataHandler _metadataHandler; private readonly IMetadataHandler _metadataHandler;
private readonly ILogger<LinkCommand> _logger;
public string CommandString => "link"; public string CommandString => "link";
public LinkCommand( public LinkCommand(
IRepositoryConfiguration repositoryConfiguration, IRepositoryConfiguration repositoryConfiguration,
IModuleConfigurationResolver moduleConfigurationResolver, IModuleConfigurationResolver moduleConfigurationResolver,
IFolderService folderService, IMetadataHandler metadataHandler,
IMetadataHandler metadataHandler) ILogger<LinkCommand> logger)
{ {
_repositoryConfiguration = repositoryConfiguration; _repositoryConfiguration = repositoryConfiguration;
_moduleConfigurationResolver = moduleConfigurationResolver; _moduleConfigurationResolver = moduleConfigurationResolver;
_folderService = folderService;
_metadataHandler = metadataHandler; _metadataHandler = metadataHandler;
_logger = logger;
} }
public async Task Run(List<string> parameters) public async Task Run(List<string> parameters)
{ {
if (parameters.Count == 0) if (parameters.Count == 0)
{ {
Console.WriteLine("No module specified"); _logger.LogInformation("No module specified");
return; return;
} }
@@ -49,7 +51,7 @@ public class LinkCommand : ICommand
if (!Directory.Exists(sourceDirectory)) if (!Directory.Exists(sourceDirectory))
{ {
Console.WriteLine("Source directory not exists: " + sourceDirectory); _logger.LogInformation("Source directory not exists: " + sourceDirectory);
return; return;
} }
@@ -58,7 +60,7 @@ public class LinkCommand : ICommand
if (!Directory.Exists(moduleDirectory)) if (!Directory.Exists(moduleDirectory))
{ {
Console.WriteLine("Module directory not exists: " + moduleDirectory); _logger.LogInformation("Module directory not exists: " + moduleDirectory);
return; return;
} }
@@ -102,14 +104,14 @@ public class LinkCommand : ICommand
{ {
if (File.Exists(itemToLink.TargetPath) || Directory.Exists(itemToLink.TargetPath)) if (File.Exists(itemToLink.TargetPath) || Directory.Exists(itemToLink.TargetPath))
{ {
Console.WriteLine("Item already exists: " + itemToLink.TargetPath); _logger.LogInformation("Item already exists: " + itemToLink.TargetPath);
continue; continue;
} }
var sourceFileExists = File.Exists(itemToLink.SourcePath); var sourceFileExists = File.Exists(itemToLink.SourcePath);
var sourceDirectoryExists = Directory.Exists(itemToLink.SourcePath); var sourceDirectoryExists = Directory.Exists(itemToLink.SourcePath);
Console.WriteLine($"Linking: '{itemToLink.SourcePath}' '{itemToLink.TargetPath}'"); _logger.LogInformation($"Linking: '{itemToLink.SourcePath}' '{itemToLink.TargetPath}'");
if (sourceFileExists) if (sourceFileExists)
{ {
@@ -121,7 +123,7 @@ public class LinkCommand : ICommand
} }
else else
{ {
Console.WriteLine("Source not exists: " + itemToLink.SourcePath); _logger.LogInformation("Source not exists: " + itemToLink.SourcePath);
continue; continue;
} }
@@ -130,10 +132,10 @@ public class LinkCommand : ICommand
} }
catch (IOException e) catch (IOException e)
{ {
Console.WriteLine("An error occured while creating links: " + e.Message); _logger.LogInformation("An error occured while creating links: " + e.Message);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{ {
Console.WriteLine("On Windows symlinks can be greated only with Administrator privileges."); _logger.LogInformation("On Windows symlinks can be greated only with Administrator privileges.");
} }
} }

View File

@@ -1,5 +1,6 @@
using Alma.Configuration.Repository; using Alma.Configuration.Repository;
using Alma.Data; using Alma.Data;
using Alma.Logging;
using Alma.Services; using Alma.Services;
namespace Alma.Command.List; namespace Alma.Command.List;
@@ -8,14 +9,18 @@ public class ListCommand : ICommand
{ {
private readonly IRepositoryConfiguration _repositoryConfiguration; private readonly IRepositoryConfiguration _repositoryConfiguration;
private readonly IModuleConfigurationResolver _moduleConfigurationResolver; private readonly IModuleConfigurationResolver _moduleConfigurationResolver;
private readonly ILogger<ListCommand> _logger;
public string CommandString => "ls"; public string CommandString => "ls";
public ListCommand( public ListCommand(
IRepositoryConfiguration repositoryConfiguration, IRepositoryConfiguration repositoryConfiguration,
IModuleConfigurationResolver moduleConfigurationResolver) IModuleConfigurationResolver moduleConfigurationResolver,
ILogger<ListCommand> logger)
{ {
_repositoryConfiguration = repositoryConfiguration; _repositoryConfiguration = repositoryConfiguration;
_moduleConfigurationResolver = moduleConfigurationResolver; _moduleConfigurationResolver = moduleConfigurationResolver;
_logger = logger;
} }
public async Task Run(List<string> parameters) public async Task Run(List<string> parameters)
@@ -32,10 +37,10 @@ public class ListCommand : ICommand
private Task ListRepositories() private Task ListRepositories()
{ {
Console.WriteLine("Repositories:" + Environment.NewLine); _logger.LogInformation("Repositories");
foreach (var repository in _repositoryConfiguration.Configuration.Repositories) foreach (var repository in _repositoryConfiguration.Configuration.Repositories)
{ {
Console.WriteLine(repository.Name); _logger.LogInformation(repository.Name);
} }
return Task.CompletedTask; return Task.CompletedTask;
@@ -46,13 +51,13 @@ public class ListCommand : ICommand
var repo = _repositoryConfiguration.Configuration.Repositories.FirstOrDefault(r => r.Name == repositoryName); var repo = _repositoryConfiguration.Configuration.Repositories.FirstOrDefault(r => r.Name == repositoryName);
if (repo is null) if (repo is null)
{ {
Console.WriteLine($"No repository found with name '{repositoryName}'"); _logger.LogInformation($"No repository found with name '{repositoryName}'");
return; return;
} }
if (repo.RepositoryPath is null) if (repo.RepositoryPath is null)
{ {
Console.WriteLine($"No repository path is specified in repository settings '{repositoryName}'"); _logger.LogInformation($"No repository path is specified in repository settings '{repositoryName}'");
return; return;
} }
@@ -64,10 +69,10 @@ public class ListCommand : ICommand
var repositoryDirectory = new DirectoryInfo(repositoryPath); var repositoryDirectory = new DirectoryInfo(repositoryPath);
var moduleDirectories = await TraverseRepositoryFolder(repositoryDirectory); var moduleDirectories = await TraverseRepositoryFolder(repositoryDirectory);
Console.WriteLine($"Modules in repository '{repositoryName}':" + Environment.NewLine); _logger.LogInformation($"Modules in repository '{repositoryName}'");
foreach (var modulePath in moduleDirectories) foreach (var modulePath in moduleDirectories)
{ {
Console.WriteLine(modulePath.FullName[repositoryDirectory.FullName.Length..].TrimStart(Path.DirectorySeparatorChar).Replace(Path.DirectorySeparatorChar, '/')); _logger.LogInformation(modulePath.FullName[repositoryDirectory.FullName.Length..].TrimStart(Path.DirectorySeparatorChar).Replace(Path.DirectorySeparatorChar, '/'));
} }
} }

View File

@@ -1,3 +1,4 @@
using Alma.Logging;
using Alma.Services; using Alma.Services;
namespace Alma.Configuration.Repository; namespace Alma.Configuration.Repository;
@@ -6,13 +7,19 @@ public class RepositoryConfiguration : IRepositoryConfiguration
{ {
private readonly IFolderService _folderService; private readonly IFolderService _folderService;
private readonly ConfigurationFileReader _configurationFileReader; private readonly ConfigurationFileReader _configurationFileReader;
private readonly ILogger<RepositoryConfiguration> _logger;
public RepositoryConfigurationRoot Configuration { get; private set; } = new RepositoryConfigurationRoot(new List<RepositoryConfigurationEntry>()); public RepositoryConfigurationRoot Configuration { get; private set; } = new RepositoryConfigurationRoot(new List<RepositoryConfigurationEntry>());
public RepositoryConfiguration(IFolderService folderService, ConfigurationFileReader configurationFileReader) public RepositoryConfiguration(
IFolderService folderService,
ConfigurationFileReader configurationFileReader,
ILogger<RepositoryConfiguration> logger
)
{ {
_folderService = folderService; _folderService = folderService;
_configurationFileReader = configurationFileReader; _configurationFileReader = configurationFileReader;
_logger = logger;
} }
public async Task LoadAsync() public async Task LoadAsync()
@@ -31,7 +38,7 @@ public class RepositoryConfiguration : IRepositoryConfiguration
{ {
if (repositoryConfigurationEntry.Name is null) if (repositoryConfigurationEntry.Name is null)
{ {
Console.WriteLine($"Entry name is null in {repoConfigFileName}"); _logger.LogInformation($"Entry name is null in {repoConfigFileName}");
} }
} }
} }

View File

@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,10 @@
namespace Alma.Logging;
public interface ILogger<T>
{
LogLevel DefaultLogLevel { get; }
void LogInformation(string logMessage);
void LogDebug(string logMessage);
void LogTrace(string logMessage);
void Log(string logMessage, LogLevel logLevel);
}

View File

@@ -0,0 +1,7 @@
namespace Alma.Logging;
public interface ILoggerFactory
{
ILogger<T> CreateLogger<T>();
LogLevel DefaultLogLevel { get; }
}

View File

@@ -0,0 +1,8 @@
namespace Alma.Logging;
public enum LogLevel
{
Information,
Debug,
Trace
}

View File

@@ -0,0 +1,25 @@
namespace Alma.Logging;
public class Logger<T> : ILogger<T>
{
public LogLevel DefaultLogLevel { get; }
public Logger(LogLevel defaultLogLevel)
{
DefaultLogLevel = defaultLogLevel;
}
public void LogInformation(string s) => Log(s, LogLevel.Information);
public void LogDebug(string s) => Log(s, LogLevel.Debug);
public void LogTrace(string s) => Log(s, LogLevel.Trace);
public void Log(string s, LogLevel logLevel)
{
if (logLevel <= DefaultLogLevel)
{
Console.WriteLine(s);
}
}
}

View File

@@ -0,0 +1,16 @@
namespace Alma.Logging;
public class LoggerFactory : ILoggerFactory
{
public LogLevel DefaultLogLevel { get; }
public LoggerFactory(LogLevel defaultLogLevel = LogLevel.Information)
{
DefaultLogLevel = defaultLogLevel;
}
public ILogger<T> CreateLogger<T>()
{
return new Logger<T>(DefaultLogLevel);
}
}

View File

@@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Alma.App", "Alma.App\Alma.A
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Alma", "Alma\Alma.csproj", "{23157A6F-C737-4ED4-B36B-BFE3EA31EAF1}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Alma", "Alma\Alma.csproj", "{23157A6F-C737-4ED4-B36B-BFE3EA31EAF1}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Alma.Logging", "Alma.Logging\Alma.Logging.csproj", "{295330FF-A52A-4BA5-8CC0-BD7D4F048171}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@@ -30,5 +32,9 @@ Global
{23157A6F-C737-4ED4-B36B-BFE3EA31EAF1}.Debug|Any CPU.Build.0 = Debug|Any CPU {23157A6F-C737-4ED4-B36B-BFE3EA31EAF1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{23157A6F-C737-4ED4-B36B-BFE3EA31EAF1}.Release|Any CPU.ActiveCfg = Release|Any CPU {23157A6F-C737-4ED4-B36B-BFE3EA31EAF1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{23157A6F-C737-4ED4-B36B-BFE3EA31EAF1}.Release|Any CPU.Build.0 = Release|Any CPU {23157A6F-C737-4ED4-B36B-BFE3EA31EAF1}.Release|Any CPU.Build.0 = Release|Any CPU
{295330FF-A52A-4BA5-8CC0-BD7D4F048171}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{295330FF-A52A-4BA5-8CC0-BD7D4F048171}.Debug|Any CPU.Build.0 = Debug|Any CPU
{295330FF-A52A-4BA5-8CC0-BD7D4F048171}.Release|Any CPU.ActiveCfg = Release|Any CPU
{295330FF-A52A-4BA5-8CC0-BD7D4F048171}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

View File

@@ -2,6 +2,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Alma.App\Alma.App.csproj" /> <ProjectReference Include="..\Alma.App\Alma.App.csproj" />
<ProjectReference Include="..\Alma.Logging\Alma.Logging.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -1,10 +1,10 @@
using Alma.Command; using Alma.Command;
using Alma.Command.Help;
using Alma.Command.Info; using Alma.Command.Info;
using Alma.Command.Link; using Alma.Command.Link;
using Alma.Command.List; using Alma.Command.List;
using Alma.Command.Unlink; using Alma.Command.Unlink;
using Alma.Configuration.Repository; using Alma.Configuration.Repository;
using Alma.Logging;
using Alma.Services; using Alma.Services;
using Jab; using Jab;
@@ -50,6 +50,8 @@ public static class Program
public static async Task Main(string[] args) public static async Task Main(string[] args)
{ {
InitLogging();
var services = new AlmaServiceProvider(); var services = new AlmaServiceProvider();
var repositoryConfiguration = services.GetService<IRepositoryConfiguration>(); var repositoryConfiguration = services.GetService<IRepositoryConfiguration>();
@@ -58,6 +60,15 @@ public static class Program
await application.Run(args); await application.Run(args);
} }
private static ILoggerFactory InitLogging()
{
var loggerFactory = new LoggerFactory();
return AlmaLoggerFactory = loggerFactory;
}
public static ILoggerFactory AlmaLoggerFactory { get; private set; }
} }
[ServiceProvider] [ServiceProvider]
@@ -75,7 +86,11 @@ public static class Program
[Singleton(typeof(IModuleConfigurationResolver), typeof(ModuleConfigurationResolver))] [Singleton(typeof(IModuleConfigurationResolver), typeof(ModuleConfigurationResolver))]
[Singleton(typeof(IMetadataHandler), typeof(MetadataHandler))] [Singleton(typeof(IMetadataHandler), typeof(MetadataHandler))]
[Singleton(typeof(Application))] [Singleton(typeof(Application))]
[Transient(typeof(ILogger<>), Factory = nameof(CustomLoggerFactory))]
internal partial class AlmaServiceProvider internal partial class AlmaServiceProvider
{ {
public ILogger<T> CustomLoggerFactory<T>()
{
return Program.AlmaLoggerFactory.CreateLogger<T>();
}
} }