Loggin WIP
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Alma.Abstraction\Alma.Abstraction.csproj" />
|
||||
<ProjectReference Include="..\Alma.Logging\Alma.Logging.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
|
||||
@@ -1,22 +1,25 @@
|
||||
using Alma.Command;
|
||||
using Alma.Command.Help;
|
||||
using Alma.Logging;
|
||||
|
||||
namespace Alma;
|
||||
|
||||
public class Application
|
||||
{
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,22 +1,30 @@
|
||||
using Alma.Logging;
|
||||
|
||||
namespace Alma.Command.Help;
|
||||
|
||||
public class HelpCommand : ICommand
|
||||
{
|
||||
private readonly Func<IEnumerable<ICommand>> _commandsProvider;
|
||||
private readonly ILogger<HelpCommand> _logger;
|
||||
|
||||
public string CommandString => "help";
|
||||
|
||||
public HelpCommand(Func<IEnumerable<ICommand>> commandsProvider)
|
||||
public HelpCommand(
|
||||
Func<IEnumerable<ICommand>> commandsProvider,
|
||||
ILogger<HelpCommand> logger
|
||||
)
|
||||
{
|
||||
_commandsProvider = commandsProvider;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
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))
|
||||
{
|
||||
Console.WriteLine(command.CommandString);
|
||||
_logger.LogInformation(command.CommandString);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
|
||||
@@ -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<ModuleInfoCommand> _logger;
|
||||
|
||||
public ModuleInfoCommand(
|
||||
IFolderService folderService,
|
||||
IRepositoryConfiguration repositoryConfiguration
|
||||
IRepositoryConfiguration repositoryConfiguration,
|
||||
ILogger<ModuleInfoCommand> logger
|
||||
)
|
||||
{
|
||||
_folderService = folderService;
|
||||
_repositoryConfiguration = repositoryConfiguration;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Run(List<string> 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;
|
||||
|
||||
@@ -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<LinkCommand> _logger;
|
||||
|
||||
public string CommandString => "link";
|
||||
|
||||
public LinkCommand(
|
||||
IRepositoryConfiguration repositoryConfiguration,
|
||||
IModuleConfigurationResolver moduleConfigurationResolver,
|
||||
IFolderService folderService,
|
||||
IMetadataHandler metadataHandler)
|
||||
IMetadataHandler metadataHandler,
|
||||
ILogger<LinkCommand> logger)
|
||||
{
|
||||
_repositoryConfiguration = repositoryConfiguration;
|
||||
_moduleConfigurationResolver = moduleConfigurationResolver;
|
||||
_folderService = folderService;
|
||||
_metadataHandler = metadataHandler;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task Run(List<string> 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.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<ListCommand> _logger;
|
||||
|
||||
public string CommandString => "ls";
|
||||
|
||||
public ListCommand(
|
||||
IRepositoryConfiguration repositoryConfiguration,
|
||||
IModuleConfigurationResolver moduleConfigurationResolver)
|
||||
IModuleConfigurationResolver moduleConfigurationResolver,
|
||||
ILogger<ListCommand> logger)
|
||||
{
|
||||
_repositoryConfiguration = repositoryConfiguration;
|
||||
_moduleConfigurationResolver = moduleConfigurationResolver;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task Run(List<string> 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, '/'));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<RepositoryConfiguration> _logger;
|
||||
|
||||
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;
|
||||
_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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
9
src/Alma.Logging/Alma.Logging.csproj
Normal file
9
src/Alma.Logging/Alma.Logging.csproj
Normal file
@@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
10
src/Alma.Logging/ILogger.cs
Normal file
10
src/Alma.Logging/ILogger.cs
Normal 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);
|
||||
}
|
||||
7
src/Alma.Logging/ILoggerFactory.cs
Normal file
7
src/Alma.Logging/ILoggerFactory.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Alma.Logging;
|
||||
|
||||
public interface ILoggerFactory
|
||||
{
|
||||
ILogger<T> CreateLogger<T>();
|
||||
LogLevel DefaultLogLevel { get; }
|
||||
}
|
||||
8
src/Alma.Logging/LogLevel.cs
Normal file
8
src/Alma.Logging/LogLevel.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace Alma.Logging;
|
||||
|
||||
public enum LogLevel
|
||||
{
|
||||
Information,
|
||||
Debug,
|
||||
Trace
|
||||
}
|
||||
25
src/Alma.Logging/Logger.cs
Normal file
25
src/Alma.Logging/Logger.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
16
src/Alma.Logging/LoggerFactory.cs
Normal file
16
src/Alma.Logging/LoggerFactory.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Alma.App\Alma.App.csproj" />
|
||||
<ProjectReference Include="..\Alma.Logging\Alma.Logging.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -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<IRepositoryConfiguration>();
|
||||
@@ -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<T> CustomLoggerFactory<T>()
|
||||
{
|
||||
return Program.AlmaLoggerFactory.CreateLogger<T>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user