From 37ce88c29e093c3b90dcbcdce93aefa7ebe944e1 Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Fri, 3 May 2019 16:22:27 +0300 Subject: [PATCH] Added logging for CLI --- .../Volo.AbpWebSite.Web/publish-ignore.json | 1 + .../Volo/Abp/Cli/CliService.cs | 11 ++++-- .../Abp/Cli/Commands/NewProjectCommand.cs | 22 +++++++----- .../Abp/ProjectBuilding/AbpIoTemplateStore.cs | 36 ++++++++++++++++--- .../src/Volo.Abp.Cli/Volo.Abp.Cli.csproj | 5 +++ .../Volo.Abp.Cli/Volo/Abp/Cli/AbpCliModule.cs | 6 ++-- .../src/Volo.Abp.Cli/Volo/Abp/Cli/Program.cs | 17 ++++++++- .../Volo/Abp/Modularity/ModuleManager.cs | 4 +-- 8 files changed, 83 insertions(+), 19 deletions(-) diff --git a/abp_io/src/Volo.AbpWebSite.Web/publish-ignore.json b/abp_io/src/Volo.AbpWebSite.Web/publish-ignore.json index 89e3176a69..c5e2fd7c51 100644 --- a/abp_io/src/Volo.AbpWebSite.Web/publish-ignore.json +++ b/abp_io/src/Volo.AbpWebSite.Web/publish-ignore.json @@ -3,6 +3,7 @@ "TemplateFiles/": {}, "Logs/": {}, "wwwroot/files/": {}, + "wwwroot/downloads/": {}, "appsettings.json": {}, "web.config": {} } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs index 1b65577f1d..502ab294e6 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs @@ -2,9 +2,12 @@ using System.Reflection; using Microsoft.Extensions.DependencyInjection; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.Cli.Args; using Volo.Abp.Cli.Commands; using Volo.Abp.DependencyInjection; +using Volo.Abp.ProjectBuilding; namespace Volo.Abp.Cli { @@ -12,6 +15,8 @@ namespace Volo.Abp.Cli { public static string Version => typeof(AbpCliCoreModule).Assembly.GetFileVersion(); + public ILogger Logger { get; set; } + protected ICommandLineArgumentParser CommandLineArgumentParser { get; } protected ICommandSelector CommandSelector { get; } public IHybridServiceScopeFactory ServiceScopeFactory { get; } @@ -24,12 +29,14 @@ namespace Volo.Abp.Cli CommandLineArgumentParser = commandLineArgumentParser; CommandSelector = commandSelector; ServiceScopeFactory = serviceScopeFactory; + + Logger = NullLogger.Instance; } public async Task RunAsync(string[] args) { - Console.WriteLine("ABP CLI (abp.io)"); - Console.WriteLine("Version: " + Version); + Logger.LogInformation("ABP CLI (abp.io)"); + Logger.LogInformation("Version: " + Version); var commandLineArgs = CommandLineArgumentParser.Parse(args); var commandType = CommandSelector.Select(commandLineArgs); diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/NewProjectCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/NewProjectCommand.cs index 8addefbdde..09fd578d85 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/NewProjectCommand.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/NewProjectCommand.cs @@ -2,6 +2,8 @@ using System.IO; using System.Threading.Tasks; using Ionic.Zip; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.Cli.Args; using Volo.Abp.DependencyInjection; using Volo.Abp.ProjectBuilding; @@ -12,28 +14,32 @@ namespace Volo.Abp.Cli.Commands { public class NewProjectCommand : IConsoleCommand, ITransientDependency { + public ILogger Logger { get; set; } + protected ProjectBuilder ProjectBuilder { get; } public NewProjectCommand(ProjectBuilder projectBuilder) { ProjectBuilder = projectBuilder; + + Logger = NullLogger.Instance; } public async Task ExecuteAsync(CommandLineArgs commandLineArgs) { if (commandLineArgs.Target == null) { - Console.WriteLine("Project name is missing."); - Console.WriteLine("Usage:"); - Console.WriteLine(" abp new [-t ]"); - Console.WriteLine("Examples:"); - Console.WriteLine(" abp new Acme.BookStore"); - Console.WriteLine(" abp new Acme.BookStore mvc"); + Logger.LogWarning("Project name is missing."); + Logger.LogWarning("Usage:"); + Logger.LogWarning(" abp new [-t ]"); + Logger.LogWarning("Examples:"); + Logger.LogWarning(" abp new Acme.BookStore"); + Logger.LogWarning(" abp new Acme.BookStore mvc"); return; } - Console.WriteLine("Creating a new solution"); - Console.WriteLine("Solution name: " + commandLineArgs.Target); + Logger.LogInformation("Creating a new project..."); + Logger.LogInformation("Project name: " + commandLineArgs.Target); var result = await ProjectBuilder.BuildAsync( new ProjectBuildArgs( diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectBuilding/AbpIoTemplateStore.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectBuilding/AbpIoTemplateStore.cs index c8c36b967d..e81e7e414b 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectBuilding/AbpIoTemplateStore.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectBuilding/AbpIoTemplateStore.cs @@ -1,16 +1,44 @@ -using System.IO; +using System; +using System.IO; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.DependencyInjection; +using Volo.Abp.IO; namespace Volo.Abp.ProjectBuilding { public class AbpIoTemplateStore : ITemplateStore, ITransientDependency { + public ILogger Logger { get; set; } + + public AbpIoTemplateStore() + { + Logger = NullLogger.Instance; + } + public async Task GetAsync(string templateName, string version) { - return new TemplateFile( - File.ReadAllBytes("C:\\Temp\\abp-templates\\" + version + "\\" + templateName + ".zip") - ); + var localCacheFolder = "C:\\Temp\\abp-template-cache\\" + version; + DirectoryHelper.CreateIfNotExists(localCacheFolder); + + var localCacheFile = Path.Combine(localCacheFolder, templateName + ".zip"); + if (File.Exists(localCacheFile)) + { + Logger.LogInformation("Using cached template: " + templateName + ", version: " + version); + return new TemplateFile(File.ReadAllBytes(localCacheFile)); + } + + Logger.LogInformation("Downloading template: " + templateName + ", version: " + version); + + using (var client = new System.Net.Http.HttpClient()) + { + client.Timeout = TimeSpan.FromMinutes(5); + var downloadUrl = "https://abp.io/downloads/templates/" + version + "/" + templateName + ".zip"; + var fileContents = await client.GetByteArrayAsync(downloadUrl); + File.WriteAllBytes(localCacheFile, fileContents); + return new TemplateFile(fileContents); + } } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.Cli/Volo.Abp.Cli.csproj b/framework/src/Volo.Abp.Cli/Volo.Abp.Cli.csproj index 9cdc913391..4f0de79850 100644 --- a/framework/src/Volo.Abp.Cli/Volo.Abp.Cli.csproj +++ b/framework/src/Volo.Abp.Cli/Volo.Abp.Cli.csproj @@ -12,9 +12,14 @@ + + + + + diff --git a/framework/src/Volo.Abp.Cli/Volo/Abp/Cli/AbpCliModule.cs b/framework/src/Volo.Abp.Cli/Volo/Abp/Cli/AbpCliModule.cs index 523dd6c813..339041ef8a 100644 --- a/framework/src/Volo.Abp.Cli/Volo/Abp/Cli/AbpCliModule.cs +++ b/framework/src/Volo.Abp.Cli/Volo/Abp/Cli/AbpCliModule.cs @@ -1,9 +1,11 @@ -using Volo.Abp.Modularity; +using Volo.Abp.Autofac; +using Volo.Abp.Modularity; namespace Volo.Abp.Cli { [DependsOn( - typeof(AbpCliCoreModule) + typeof(AbpCliCoreModule), + typeof(AbpAutofacModule) )] public class AbpCliModule : AbpModule { diff --git a/framework/src/Volo.Abp.Cli/Volo/Abp/Cli/Program.cs b/framework/src/Volo.Abp.Cli/Volo/Abp/Cli/Program.cs index c7f321a8e1..706dc91d47 100644 --- a/framework/src/Volo.Abp.Cli/Volo/Abp/Cli/Program.cs +++ b/framework/src/Volo.Abp.Cli/Volo/Abp/Cli/Program.cs @@ -1,4 +1,6 @@ using Microsoft.Extensions.DependencyInjection; +using Serilog; +using Serilog.Events; using Volo.Abp.Threading; namespace Volo.Abp.Cli @@ -7,7 +9,20 @@ namespace Volo.Abp.Cli { private static void Main(string[] args) { - using (var application = AbpApplicationFactory.Create()) + Log.Logger = new LoggerConfiguration() + .MinimumLevel.Information() + .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) + .Enrich.FromLogContext() + //.WriteTo.File("Logs/logs.txt") //TODO: Write logs to a global path + .WriteTo.Console() + .CreateLogger(); + + using (var application = AbpApplicationFactory.Create( + options => + { + options.UseAutofac(); + options.Services.AddLogging(c => c.AddSerilog()); + })) { application.Initialize(); diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/ModuleManager.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/ModuleManager.cs index d79ae0b843..934948dc03 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/ModuleManager.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/ModuleManager.cs @@ -42,12 +42,12 @@ namespace Volo.Abp.Modularity } } - _logger.LogInformation("Initialized all modules."); + _logger.LogInformation("Initialized all ABP modules."); } private void LogListOfModules() { - _logger.LogInformation("Loaded modules:"); + _logger.LogInformation("Loaded ABP modules:"); foreach (var module in _moduleContainer.Modules) {