diff --git a/src/Alma.App/Alma.App.csproj b/src/Alma.App/Alma.App.csproj index 8ffd592..365f292 100644 --- a/src/Alma.App/Alma.App.csproj +++ b/src/Alma.App/Alma.App.csproj @@ -2,6 +2,7 @@ + diff --git a/src/Alma.App/Application.cs b/src/Alma.App/Application.cs index 05e147b..6f91ff3 100644 --- a/src/Alma.App/Application.cs +++ b/src/Alma.App/Application.cs @@ -1,22 +1,25 @@ using Alma.Command; using Alma.Command.Help; +using Alma.Logging; namespace Alma; public class Application { private readonly IList _commands; + private readonly ILogger _logger; - public Application(IEnumerable commands) + public Application(IEnumerable commands, ILogger logger, ILogger helpCommandLogger) { - _commands = commands.Append(new HelpCommand(() => _commands!)).ToList(); + _commands = commands.Append(new HelpCommand(() => _commands!, helpCommandLogger)).ToList(); + _logger = logger; } public async Task Run(string[] args) { if (args.Length == 0) { - Console.WriteLine("No command was given"); + _logger.LogInformation("No command was given"); return; } @@ -26,7 +29,7 @@ public class Application if (command is null) { - Console.WriteLine($"Invalid command: {commandString}"); + _logger.LogInformation($"Invalid command: {commandString}"); return; } diff --git a/src/Alma.App/Command/Help/HelpCommand.cs b/src/Alma.App/Command/Help/HelpCommand.cs index 774cd94..e03e990 100644 --- a/src/Alma.App/Command/Help/HelpCommand.cs +++ b/src/Alma.App/Command/Help/HelpCommand.cs @@ -1,22 +1,30 @@ +using Alma.Logging; + namespace Alma.Command.Help; public class HelpCommand : ICommand { private readonly Func> _commandsProvider; + private readonly ILogger _logger; + public string CommandString => "help"; - public HelpCommand(Func> commandsProvider) + public HelpCommand( + Func> commandsProvider, + ILogger logger + ) { _commandsProvider = commandsProvider; + _logger = logger; } public Task Run(List parameters) { - Console.WriteLine("Commands:" + Environment.NewLine); + _logger.LogInformation("Commands:" + Environment.NewLine); foreach (var command in _commandsProvider().OrderBy(c => c.CommandString)) { - Console.WriteLine(command.CommandString); + _logger.LogInformation(command.CommandString); } return Task.CompletedTask; diff --git a/src/Alma.App/Command/Info/ModuleInfoCommand.cs b/src/Alma.App/Command/Info/ModuleInfoCommand.cs index a032404..d07e0cc 100644 --- a/src/Alma.App/Command/Info/ModuleInfoCommand.cs +++ b/src/Alma.App/Command/Info/ModuleInfoCommand.cs @@ -1,4 +1,5 @@ using Alma.Configuration.Repository; +using Alma.Logging; using Alma.Services; namespace Alma.Command.Info; @@ -9,37 +10,40 @@ public class ModuleInfoCommand : ICommand private readonly IFolderService _folderService; private readonly IRepositoryConfiguration _repositoryConfiguration; + private readonly ILogger _logger; public ModuleInfoCommand( IFolderService folderService, - IRepositoryConfiguration repositoryConfiguration + IRepositoryConfiguration repositoryConfiguration, + ILogger logger ) { _folderService = folderService; _repositoryConfiguration = repositoryConfiguration; + _logger = logger; } public Task Run(List parameters) { //Add info REPO //Add info REPO MODULE - Console.WriteLine("AppData folder: " + _folderService.AppData); + _logger.LogInformation("AppData folder: " + _folderService.AppData); if (_folderService.ConfigRoot is string configRoot) { - Console.WriteLine("Configuration folder: " + configRoot); + _logger.LogInformation("Configuration folder: " + configRoot); } else { - Console.WriteLine("Configuration folder not exists."); - Console.WriteLine("Preffered configuration folder is: " + Path.Combine(_folderService.GetPreferredConfigurationFolder(), _folderService.ApplicationSubfolderName)); + _logger.LogInformation("Configuration folder not exists."); + _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) { - Console.WriteLine("Repositories:"); + _logger.LogInformation("Repositories:"); foreach (var repository in repositores) { Console.Write(repository.Name); @@ -47,12 +51,12 @@ public class ModuleInfoCommand : ICommand { Console.Write($" (containing folder not exists {repository.RepositoryPath})"); } - Console.WriteLine(); + _logger.LogInformation(""); } } else { - Console.WriteLine("No repositories found"); + _logger.LogInformation("No repositories found"); } return Task.CompletedTask; diff --git a/src/Alma.App/Command/Link/LinkCommand.cs b/src/Alma.App/Command/Link/LinkCommand.cs index c855b4b..6422471 100644 --- a/src/Alma.App/Command/Link/LinkCommand.cs +++ b/src/Alma.App/Command/Link/LinkCommand.cs @@ -2,6 +2,7 @@ using System.Runtime.InteropServices; using Alma.Configuration.Module; using Alma.Configuration.Repository; using Alma.Data; +using Alma.Logging; using Alma.Services; namespace Alma.Command.Link; @@ -10,27 +11,28 @@ public class LinkCommand : ICommand { private readonly IRepositoryConfiguration _repositoryConfiguration; private readonly IModuleConfigurationResolver _moduleConfigurationResolver; - private readonly IFolderService _folderService; private readonly IMetadataHandler _metadataHandler; + private readonly ILogger _logger; + public string CommandString => "link"; public LinkCommand( IRepositoryConfiguration repositoryConfiguration, IModuleConfigurationResolver moduleConfigurationResolver, - IFolderService folderService, - IMetadataHandler metadataHandler) + IMetadataHandler metadataHandler, + ILogger logger) { _repositoryConfiguration = repositoryConfiguration; _moduleConfigurationResolver = moduleConfigurationResolver; - _folderService = folderService; _metadataHandler = metadataHandler; + _logger = logger; } public async Task Run(List parameters) { if (parameters.Count == 0) { - Console.WriteLine("No module specified"); + _logger.LogInformation("No module specified"); return; } @@ -49,7 +51,7 @@ public class LinkCommand : ICommand if (!Directory.Exists(sourceDirectory)) { - Console.WriteLine("Source directory not exists: " + sourceDirectory); + _logger.LogInformation("Source directory not exists: " + sourceDirectory); return; } @@ -58,7 +60,7 @@ public class LinkCommand : ICommand if (!Directory.Exists(moduleDirectory)) { - Console.WriteLine("Module directory not exists: " + moduleDirectory); + _logger.LogInformation("Module directory not exists: " + moduleDirectory); return; } @@ -102,14 +104,14 @@ public class LinkCommand : ICommand { if (File.Exists(itemToLink.TargetPath) || Directory.Exists(itemToLink.TargetPath)) { - Console.WriteLine("Item already exists: " + itemToLink.TargetPath); + _logger.LogInformation("Item already exists: " + itemToLink.TargetPath); continue; } var sourceFileExists = File.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) { @@ -121,7 +123,7 @@ public class LinkCommand : ICommand } else { - Console.WriteLine("Source not exists: " + itemToLink.SourcePath); + _logger.LogInformation("Source not exists: " + itemToLink.SourcePath); continue; } @@ -130,10 +132,10 @@ public class LinkCommand : ICommand } 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)) { - Console.WriteLine("On Windows symlinks can be greated only with Administrator privileges."); + _logger.LogInformation("On Windows symlinks can be greated only with Administrator privileges."); } } diff --git a/src/Alma.App/Command/List/ListCommand.cs b/src/Alma.App/Command/List/ListCommand.cs index 013d391..0e980ea 100644 --- a/src/Alma.App/Command/List/ListCommand.cs +++ b/src/Alma.App/Command/List/ListCommand.cs @@ -1,5 +1,6 @@ using Alma.Configuration.Repository; using Alma.Data; +using Alma.Logging; using Alma.Services; namespace Alma.Command.List; @@ -8,14 +9,18 @@ public class ListCommand : ICommand { private readonly IRepositoryConfiguration _repositoryConfiguration; private readonly IModuleConfigurationResolver _moduleConfigurationResolver; + private readonly ILogger _logger; + public string CommandString => "ls"; public ListCommand( IRepositoryConfiguration repositoryConfiguration, - IModuleConfigurationResolver moduleConfigurationResolver) + IModuleConfigurationResolver moduleConfigurationResolver, + ILogger logger) { _repositoryConfiguration = repositoryConfiguration; _moduleConfigurationResolver = moduleConfigurationResolver; + _logger = logger; } public async Task Run(List parameters) @@ -32,10 +37,10 @@ public class ListCommand : ICommand private Task ListRepositories() { - Console.WriteLine("Repositories:" + Environment.NewLine); + _logger.LogInformation("Repositories"); foreach (var repository in _repositoryConfiguration.Configuration.Repositories) { - Console.WriteLine(repository.Name); + _logger.LogInformation(repository.Name); } return Task.CompletedTask; @@ -46,13 +51,13 @@ public class ListCommand : ICommand var repo = _repositoryConfiguration.Configuration.Repositories.FirstOrDefault(r => r.Name == repositoryName); if (repo is null) { - Console.WriteLine($"No repository found with name '{repositoryName}'"); + _logger.LogInformation($"No repository found with name '{repositoryName}'"); return; } 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; } @@ -64,10 +69,10 @@ public class ListCommand : ICommand var repositoryDirectory = new DirectoryInfo(repositoryPath); var moduleDirectories = await TraverseRepositoryFolder(repositoryDirectory); - Console.WriteLine($"Modules in repository '{repositoryName}':" + Environment.NewLine); + _logger.LogInformation($"Modules in repository '{repositoryName}'"); 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, '/')); } } diff --git a/src/Alma.App/Configuration/Repository/RepositoryConfiguration.cs b/src/Alma.App/Configuration/Repository/RepositoryConfiguration.cs index 4ff2559..bad573a 100644 --- a/src/Alma.App/Configuration/Repository/RepositoryConfiguration.cs +++ b/src/Alma.App/Configuration/Repository/RepositoryConfiguration.cs @@ -1,3 +1,4 @@ +using Alma.Logging; using Alma.Services; namespace Alma.Configuration.Repository; @@ -6,13 +7,19 @@ public class RepositoryConfiguration : IRepositoryConfiguration { private readonly IFolderService _folderService; private readonly ConfigurationFileReader _configurationFileReader; + private readonly ILogger _logger; public RepositoryConfigurationRoot Configuration { get; private set; } = new RepositoryConfigurationRoot(new List()); - public RepositoryConfiguration(IFolderService folderService, ConfigurationFileReader configurationFileReader) + public RepositoryConfiguration( + IFolderService folderService, + ConfigurationFileReader configurationFileReader, + ILogger logger + ) { _folderService = folderService; _configurationFileReader = configurationFileReader; + _logger = logger; } public async Task LoadAsync() @@ -31,7 +38,7 @@ public class RepositoryConfiguration : IRepositoryConfiguration { if (repositoryConfigurationEntry.Name is null) { - Console.WriteLine($"Entry name is null in {repoConfigFileName}"); + _logger.LogInformation($"Entry name is null in {repoConfigFileName}"); } } } diff --git a/src/Alma.Logging/Alma.Logging.csproj b/src/Alma.Logging/Alma.Logging.csproj new file mode 100644 index 0000000..132c02c --- /dev/null +++ b/src/Alma.Logging/Alma.Logging.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/src/Alma.Logging/ILogger.cs b/src/Alma.Logging/ILogger.cs new file mode 100644 index 0000000..2bb57cd --- /dev/null +++ b/src/Alma.Logging/ILogger.cs @@ -0,0 +1,10 @@ +namespace Alma.Logging; + +public interface ILogger +{ + LogLevel DefaultLogLevel { get; } + void LogInformation(string logMessage); + void LogDebug(string logMessage); + void LogTrace(string logMessage); + void Log(string logMessage, LogLevel logLevel); +} \ No newline at end of file diff --git a/src/Alma.Logging/ILoggerFactory.cs b/src/Alma.Logging/ILoggerFactory.cs new file mode 100644 index 0000000..47fd757 --- /dev/null +++ b/src/Alma.Logging/ILoggerFactory.cs @@ -0,0 +1,7 @@ +namespace Alma.Logging; + +public interface ILoggerFactory +{ + ILogger CreateLogger(); + LogLevel DefaultLogLevel { get; } +} \ No newline at end of file diff --git a/src/Alma.Logging/LogLevel.cs b/src/Alma.Logging/LogLevel.cs new file mode 100644 index 0000000..fd2ec99 --- /dev/null +++ b/src/Alma.Logging/LogLevel.cs @@ -0,0 +1,8 @@ +namespace Alma.Logging; + +public enum LogLevel +{ + Information, + Debug, + Trace +} \ No newline at end of file diff --git a/src/Alma.Logging/Logger.cs b/src/Alma.Logging/Logger.cs new file mode 100644 index 0000000..d6c324c --- /dev/null +++ b/src/Alma.Logging/Logger.cs @@ -0,0 +1,25 @@ +namespace Alma.Logging; + +public class Logger : ILogger +{ + 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); + } + } +} \ No newline at end of file diff --git a/src/Alma.Logging/LoggerFactory.cs b/src/Alma.Logging/LoggerFactory.cs new file mode 100644 index 0000000..cc7d425 --- /dev/null +++ b/src/Alma.Logging/LoggerFactory.cs @@ -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 CreateLogger() + { + return new Logger(DefaultLogLevel); + } +} \ No newline at end of file diff --git a/src/Alma.sln b/src/Alma.sln index e3696d2..edd8ce3 100644 --- a/src/Alma.sln +++ b/src/Alma.sln @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Alma.App", "Alma.App\Alma.A EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Alma", "Alma\Alma.csproj", "{23157A6F-C737-4ED4-B36B-BFE3EA31EAF1}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Alma.Logging", "Alma.Logging\Alma.Logging.csproj", "{295330FF-A52A-4BA5-8CC0-BD7D4F048171}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.Release|Any CPU.ActiveCfg = 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 EndGlobal diff --git a/src/Alma/Alma.csproj b/src/Alma/Alma.csproj index d6aad34..8253687 100644 --- a/src/Alma/Alma.csproj +++ b/src/Alma/Alma.csproj @@ -2,6 +2,7 @@ + diff --git a/src/Alma/Program.cs b/src/Alma/Program.cs index f4e6c4e..af22e2d 100644 --- a/src/Alma/Program.cs +++ b/src/Alma/Program.cs @@ -1,10 +1,10 @@ using Alma.Command; -using Alma.Command.Help; using Alma.Command.Info; using Alma.Command.Link; using Alma.Command.List; using Alma.Command.Unlink; using Alma.Configuration.Repository; +using Alma.Logging; using Alma.Services; using Jab; @@ -50,6 +50,8 @@ public static class Program public static async Task Main(string[] args) { + InitLogging(); + var services = new AlmaServiceProvider(); var repositoryConfiguration = services.GetService(); @@ -58,6 +60,15 @@ public static class Program await application.Run(args); } + + private static ILoggerFactory InitLogging() + { + var loggerFactory = new LoggerFactory(); + + return AlmaLoggerFactory = loggerFactory; + } + + public static ILoggerFactory AlmaLoggerFactory { get; private set; } } [ServiceProvider] @@ -75,7 +86,11 @@ public static class Program [Singleton(typeof(IModuleConfigurationResolver), typeof(ModuleConfigurationResolver))] [Singleton(typeof(IMetadataHandler), typeof(MetadataHandler))] [Singleton(typeof(Application))] +[Transient(typeof(ILogger<>), Factory = nameof(CustomLoggerFactory))] internal partial class AlmaServiceProvider { - + public ILogger CustomLoggerFactory() + { + return Program.AlmaLoggerFactory.CreateLogger(); + } } \ No newline at end of file