From 21022b85a15ebb9dcc919d0816815c4e1b02426f Mon Sep 17 00:00:00 2001 From: Dmytro M Date: Wed, 22 Dec 2021 00:59:23 +0200 Subject: [PATCH 01/12] refactor logging --- .../OutOfSchool.IdentityServer.csproj | 1 + .../OutOfSchool.IdentityServer/Program.cs | 3 ++- .../appsettings.Azure.json | 4 +-- .../appsettings.Compose.json | 4 +-- .../appsettings.Development.json | 5 ++-- .../appsettings.Google.json | 10 +++++--- .../Extensions/ElasticsearchExtension.cs | 11 ++++---- .../OutOfSchool.WebApi.csproj | 7 +++--- OutOfSchool/OutOfSchool.WebApi/Program.cs | 24 +++--------------- .../OutOfSchool.WebApi/appsettings.Azure.json | 16 +++++++----- .../appsettings.Compose.json | 8 ++++++ .../appsettings.Development.json | 25 +++++++++++++------ .../appsettings.Google.json | 18 ++++++++----- .../OutOfSchool.WebApi/appsettings.json | 13 ++++++++++ 14 files changed, 88 insertions(+), 61 deletions(-) diff --git a/OutOfSchool/OutOfSchool.IdentityServer/OutOfSchool.IdentityServer.csproj b/OutOfSchool/OutOfSchool.IdentityServer/OutOfSchool.IdentityServer.csproj index a1d460e816..d2069a44c7 100644 --- a/OutOfSchool/OutOfSchool.IdentityServer/OutOfSchool.IdentityServer.csproj +++ b/OutOfSchool/OutOfSchool.IdentityServer/OutOfSchool.IdentityServer.csproj @@ -35,6 +35,7 @@ + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/OutOfSchool/OutOfSchool.IdentityServer/Program.cs b/OutOfSchool/OutOfSchool.IdentityServer/Program.cs index e159b6b6f3..b98e196559 100644 --- a/OutOfSchool/OutOfSchool.IdentityServer/Program.cs +++ b/OutOfSchool/OutOfSchool.IdentityServer/Program.cs @@ -28,12 +28,13 @@ public class Program public static void Main(string[] args) { var config = new ConfigurationBuilder() + .AddJsonFile("appsettings.json") .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development"}.json", true) .AddEnvironmentVariables() .Build(); var loggerConfigBuilder = new LoggerConfiguration() - .ReadFrom.Configuration(config, sectionName: "Serilog"); + .ReadFrom.Configuration(config); Log.Logger = loggerConfigBuilder.CreateLogger(); diff --git a/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Azure.json b/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Azure.json index 7a173c2d5a..560dd22dd1 100644 --- a/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Azure.json +++ b/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Azure.json @@ -1,13 +1,13 @@ { "Serilog": { + "Using": [ "Serilog.Sinks.Console" ], "WriteTo": [ { "Name": "Console", "Args": { "formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact" } - }, - { "Name": "Debug" } + } ] }, "IdentityAccessConfig": { diff --git a/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Compose.json b/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Compose.json index 10d352f5ab..386d45b5d2 100644 --- a/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Compose.json +++ b/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Compose.json @@ -1,11 +1,9 @@ { "Serilog": { + "Using": [ "Serilog.Sinks.Console" ], "WriteTo": [ { "Name": "Console" - }, - { - "Name": "Debug" } ] }, diff --git a/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Development.json b/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Development.json index 23513744e1..78694727a7 100644 --- a/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Development.json +++ b/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Development.json @@ -3,6 +3,8 @@ "DefaultConnection": "server=localhost;user=root;password=rootPassword;database=out_of_school;guidformat=binary16" }, "Serilog": { + "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], + "MinimumLevel": "Debug", "WriteTo": [ { "Name": "Console" @@ -15,8 +17,7 @@ "retainedFileCountLimit": 2, "fileSizeLimitBytes": null } - }, - { "Name": "Debug" } + } ] } } diff --git a/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Google.json b/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Google.json index 4915c57446..b53efaf7f1 100644 --- a/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Google.json +++ b/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Google.json @@ -1,13 +1,15 @@ { "Serilog": { + "Using": [ "Serilog.Sinks.GoogleCloudLogging" ], + "MinimumLevel": "Debug", "WriteTo": [ { - "Name": "Console", + "Name": "GoogleCloudLogging", "Args": { - "formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact" + "useJsonOutput": "true", + "useLogCorrelation": "true" } - }, - { "Name": "Debug" } + } ] }, "IdentityAccessConfig": { diff --git a/OutOfSchool/OutOfSchool.WebApi/Extensions/ElasticsearchExtension.cs b/OutOfSchool/OutOfSchool.WebApi/Extensions/ElasticsearchExtension.cs index f7d05851dc..8a96f8f4ef 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Extensions/ElasticsearchExtension.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Extensions/ElasticsearchExtension.cs @@ -35,12 +35,12 @@ public static void AddElasticsearch(this IServiceCollection services, ElasticCon var pool = new StaticConnectionPool(uris); var settings = new ConnectionSettings(pool) - .DefaultIndex(config.DefaultIndex) - .BasicAuthentication(config.User, config.Password) + .DefaultIndex(config.DefaultIndex) + .BasicAuthentication(config.User, config.Password); - // TODO: Configure this to run only in Dev and Test environments - // TODO: Need this now to debug role permissions for Elastic - // TODO: Use decent logger + if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development") + { + settings .EnableDebugMode(details => { if (details.RequestBodyInBytes != null) @@ -70,6 +70,7 @@ public static void AddElasticsearch(this IServiceCollection services, ElasticCon Console.Error.WriteLine($"Reason: {details.OriginalException}"); } }); + } AddDefaultMappings(settings, config.DefaultIndex); diff --git a/OutOfSchool/OutOfSchool.WebApi/OutOfSchool.WebApi.csproj b/OutOfSchool/OutOfSchool.WebApi/OutOfSchool.WebApi.csproj index 04f00ef882..8ea34de653 100644 --- a/OutOfSchool/OutOfSchool.WebApi/OutOfSchool.WebApi.csproj +++ b/OutOfSchool/OutOfSchool.WebApi/OutOfSchool.WebApi.csproj @@ -39,9 +39,10 @@ - - - + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/OutOfSchool/OutOfSchool.WebApi/Program.cs b/OutOfSchool/OutOfSchool.WebApi/Program.cs index 03d1e9c341..5daaf86a73 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Program.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Program.cs @@ -11,32 +11,14 @@ public class Program { public static void Main(string[] args) { - var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); - var config = new ConfigurationBuilder() - .AddJsonFile($"appsettings.{environment}.json") + .AddJsonFile("appsettings.json") + .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development"}.json", true) .AddEnvironmentVariables() .Build(); var loggerConfigBuilder = new LoggerConfiguration() - .ReadFrom.Configuration(config, sectionName: "Logging") - .Enrich.FromLogContext() - .WriteTo.Debug(); - - if (environment != "Azure" && environment != "Google") - { - loggerConfigBuilder - .WriteTo.Console() - .WriteTo.File( - path: config.GetSection("Logging:FilePath").Value, - rollingInterval: RollingInterval.Day, - retainedFileCountLimit: 2, - fileSizeLimitBytes: null); - } - else - { - loggerConfigBuilder.WriteTo.Console(new RenderedCompactJsonFormatter()); - } + .ReadFrom.Configuration(config); Log.Logger = loggerConfigBuilder.CreateLogger(); diff --git a/OutOfSchool/OutOfSchool.WebApi/appsettings.Azure.json b/OutOfSchool/OutOfSchool.WebApi/appsettings.Azure.json index b8886d7505..5aca655d0c 100644 --- a/OutOfSchool/OutOfSchool.WebApi/appsettings.Azure.json +++ b/OutOfSchool/OutOfSchool.WebApi/appsettings.Azure.json @@ -1,10 +1,14 @@ { - "Logging": { - "LogLevel": { - "Default": "Error", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Warning" - } + "Serilog": { + "Using": [ "Serilog.Sinks.Console" ], + "WriteTo": [ + { + "Name": "Console", + "Args": { + "formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact" + } + } + ] }, "Swagger": { "IdentityAccess": { diff --git a/OutOfSchool/OutOfSchool.WebApi/appsettings.Compose.json b/OutOfSchool/OutOfSchool.WebApi/appsettings.Compose.json index 39f796e409..3fc5270251 100644 --- a/OutOfSchool/OutOfSchool.WebApi/appsettings.Compose.json +++ b/OutOfSchool/OutOfSchool.WebApi/appsettings.Compose.json @@ -1,4 +1,12 @@ { + "Serilog": { + "Using": [ "Serilog.Sinks.Console" ], + "WriteTo": [ + { + "Name": "Console" + } + ] + }, "Identity": { "Authority": "https://oos.local/identity" }, diff --git a/OutOfSchool/OutOfSchool.WebApi/appsettings.Development.json b/OutOfSchool/OutOfSchool.WebApi/appsettings.Development.json index 64e0336b40..d19f2b5139 100644 --- a/OutOfSchool/OutOfSchool.WebApi/appsettings.Development.json +++ b/OutOfSchool/OutOfSchool.WebApi/appsettings.Development.json @@ -6,15 +6,24 @@ "DatabaseName": "Images" } }, - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" - }, - "FilePath": "Loggs\\log.log" + "Serilog": { + "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], + "MinimumLevel": "Debug", + "WriteTo": [ + { + "Name": "Console" + }, + { + "Name": "File", + "Args": { + "path": "Loggs/log.log", + "rollingInterval": "Day", + "retainedFileCountLimit": 2, + "fileSizeLimitBytes": null + } + } + ] }, - "Elasticsearch": { "EnsureIndex": false, "DefaultIndex": "workshop", diff --git a/OutOfSchool/OutOfSchool.WebApi/appsettings.Google.json b/OutOfSchool/OutOfSchool.WebApi/appsettings.Google.json index d6db06bc40..6bd1988179 100644 --- a/OutOfSchool/OutOfSchool.WebApi/appsettings.Google.json +++ b/OutOfSchool/OutOfSchool.WebApi/appsettings.Google.json @@ -1,10 +1,16 @@ { - "Logging": { - "LogLevel": { - "Default": "Error", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Warning" - } + "Serilog": { + "Using": [ "Serilog.Sinks.GoogleCloudLogging" ], + "MinimumLevel": "Debug", + "WriteTo": [ + { + "Name": "GoogleCloudLogging", + "Args": { + "useJsonOutput": "true", + "useLogCorrelation": "true" + } + } + ] }, "Swagger": { "IdentityAccess": { diff --git a/OutOfSchool/OutOfSchool.WebApi/appsettings.json b/OutOfSchool/OutOfSchool.WebApi/appsettings.json index 7e8874b71d..153cc2def5 100644 --- a/OutOfSchool/OutOfSchool.WebApi/appsettings.json +++ b/OutOfSchool/OutOfSchool.WebApi/appsettings.json @@ -2,6 +2,19 @@ "AppDefaults": { "City": "Київ" }, + "Serilog": { + "Using": [], + "MinimumLevel": { + "Default": "Information", + "Override": { + "Microsoft": "Information", + "System": "Warning" + } + }, + "Enrich": [ + "FromLogContext" + ] + }, "Logging": { "LogLevel": { "Default": "Information", From 201931ed18010568bb1403527e83b70a25d2ce75 Mon Sep 17 00:00:00 2001 From: Dmytro M Date: Wed, 22 Dec 2021 01:11:22 +0200 Subject: [PATCH 02/12] update min level --- .../appsettings.Development.json | 8 +++++++- .../OutOfSchool.IdentityServer/appsettings.Google.json | 8 +++++++- .../OutOfSchool.WebApi/appsettings.Development.json | 8 +++++++- OutOfSchool/OutOfSchool.WebApi/appsettings.Google.json | 8 +++++++- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Development.json b/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Development.json index 78694727a7..3cc54c7eba 100644 --- a/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Development.json +++ b/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Development.json @@ -4,7 +4,13 @@ }, "Serilog": { "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], - "MinimumLevel": "Debug", + "MinimumLevel": { + "Default": "Debug", + "Override": { + "Microsoft": "Information", + "System": "Warning" + } + }, "WriteTo": [ { "Name": "Console" diff --git a/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Google.json b/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Google.json index b53efaf7f1..795eaaceb4 100644 --- a/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Google.json +++ b/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Google.json @@ -1,7 +1,13 @@ { "Serilog": { "Using": [ "Serilog.Sinks.GoogleCloudLogging" ], - "MinimumLevel": "Debug", + "MinimumLevel": { + "Default": "Debug", + "Override": { + "Microsoft": "Information", + "System": "Warning" + } + }, "WriteTo": [ { "Name": "GoogleCloudLogging", diff --git a/OutOfSchool/OutOfSchool.WebApi/appsettings.Development.json b/OutOfSchool/OutOfSchool.WebApi/appsettings.Development.json index d19f2b5139..de583f0d06 100644 --- a/OutOfSchool/OutOfSchool.WebApi/appsettings.Development.json +++ b/OutOfSchool/OutOfSchool.WebApi/appsettings.Development.json @@ -8,7 +8,13 @@ }, "Serilog": { "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], - "MinimumLevel": "Debug", + "MinimumLevel": { + "Default": "Debug", + "Override": { + "Microsoft": "Information", + "System": "Warning" + } + }, "WriteTo": [ { "Name": "Console" diff --git a/OutOfSchool/OutOfSchool.WebApi/appsettings.Google.json b/OutOfSchool/OutOfSchool.WebApi/appsettings.Google.json index 6bd1988179..3302d76c38 100644 --- a/OutOfSchool/OutOfSchool.WebApi/appsettings.Google.json +++ b/OutOfSchool/OutOfSchool.WebApi/appsettings.Google.json @@ -1,7 +1,13 @@ { "Serilog": { "Using": [ "Serilog.Sinks.GoogleCloudLogging" ], - "MinimumLevel": "Debug", + "MinimumLevel": { + "Default": "Debug", + "Override": { + "Microsoft": "Information", + "System": "Warning" + } + }, "WriteTo": [ { "Name": "GoogleCloudLogging", From ea12da2d0c2ab11f81579a278ac3e6796053e280 Mon Sep 17 00:00:00 2001 From: Dmytro M Date: Wed, 22 Dec 2021 01:32:50 +0200 Subject: [PATCH 03/12] remove default logging config --- OutOfSchool/OutOfSchool.IdentityServer/appsettings.json | 7 ------- OutOfSchool/OutOfSchool.WebApi/appsettings.json | 7 ------- 2 files changed, 14 deletions(-) diff --git a/OutOfSchool/OutOfSchool.IdentityServer/appsettings.json b/OutOfSchool/OutOfSchool.IdentityServer/appsettings.json index dc23bf9d4f..84a2273b40 100644 --- a/OutOfSchool/OutOfSchool.IdentityServer/appsettings.json +++ b/OutOfSchool/OutOfSchool.IdentityServer/appsettings.json @@ -13,13 +13,6 @@ "FromLogContext" ] }, - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" - } - }, "Email": { "AddressFrom": "OoS.Backend.Test.Server@gmail.com", "NameFrom": "Oos-Backend", diff --git a/OutOfSchool/OutOfSchool.WebApi/appsettings.json b/OutOfSchool/OutOfSchool.WebApi/appsettings.json index 153cc2def5..c2fe96ace7 100644 --- a/OutOfSchool/OutOfSchool.WebApi/appsettings.json +++ b/OutOfSchool/OutOfSchool.WebApi/appsettings.json @@ -15,13 +15,6 @@ "FromLogContext" ] }, - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" - } - }, "Identity": { "Authority": "http://localhost:5443" }, From 30c868b7df9faa36c51747782e8e1acb84cde5ad Mon Sep 17 00:00:00 2001 From: Dmytro M Date: Wed, 22 Dec 2021 09:53:27 +0200 Subject: [PATCH 04/12] add debug mode for elastic --- OutOfSchool/OutOfSchool.WebApi/Config/ElasticConfig.cs | 2 ++ .../OutOfSchool.WebApi/Extensions/ElasticsearchExtension.cs | 2 +- OutOfSchool/OutOfSchool.WebApi/appsettings.Compose.json | 1 + OutOfSchool/OutOfSchool.WebApi/appsettings.Development.json | 1 + OutOfSchool/OutOfSchool.WebApi/appsettings.Google.json | 1 + 5 files changed, 6 insertions(+), 1 deletion(-) diff --git a/OutOfSchool/OutOfSchool.WebApi/Config/ElasticConfig.cs b/OutOfSchool/OutOfSchool.WebApi/Config/ElasticConfig.cs index aeb8ed061a..b9584bd1de 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Config/ElasticConfig.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Config/ElasticConfig.cs @@ -9,6 +9,8 @@ public class ElasticConfig public bool EnsureIndex { get; set; } + public bool EnableDebugMode { get; set; } + [Required] public string DefaultIndex { get; set; } diff --git a/OutOfSchool/OutOfSchool.WebApi/Extensions/ElasticsearchExtension.cs b/OutOfSchool/OutOfSchool.WebApi/Extensions/ElasticsearchExtension.cs index 8a96f8f4ef..3f73fc1f9f 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Extensions/ElasticsearchExtension.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Extensions/ElasticsearchExtension.cs @@ -38,7 +38,7 @@ public static void AddElasticsearch(this IServiceCollection services, ElasticCon .DefaultIndex(config.DefaultIndex) .BasicAuthentication(config.User, config.Password); - if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development") + if (config.EnableDebugMode) { settings .EnableDebugMode(details => diff --git a/OutOfSchool/OutOfSchool.WebApi/appsettings.Compose.json b/OutOfSchool/OutOfSchool.WebApi/appsettings.Compose.json index 3fc5270251..5c3fe10f8f 100644 --- a/OutOfSchool/OutOfSchool.WebApi/appsettings.Compose.json +++ b/OutOfSchool/OutOfSchool.WebApi/appsettings.Compose.json @@ -20,6 +20,7 @@ }, "Elasticsearch": { "EnsureIndex": true, + "EnableDebugMode": false, "DefaultIndex": "workshop", "Urls": [ "http://elasticsearch:9200/" diff --git a/OutOfSchool/OutOfSchool.WebApi/appsettings.Development.json b/OutOfSchool/OutOfSchool.WebApi/appsettings.Development.json index de583f0d06..8341ada6d8 100644 --- a/OutOfSchool/OutOfSchool.WebApi/appsettings.Development.json +++ b/OutOfSchool/OutOfSchool.WebApi/appsettings.Development.json @@ -32,6 +32,7 @@ }, "Elasticsearch": { "EnsureIndex": false, + "EnableDebugMode": true, "DefaultIndex": "workshop", "Urls": [ "http://localhost:9200/" diff --git a/OutOfSchool/OutOfSchool.WebApi/appsettings.Google.json b/OutOfSchool/OutOfSchool.WebApi/appsettings.Google.json index 3302d76c38..394a4be92f 100644 --- a/OutOfSchool/OutOfSchool.WebApi/appsettings.Google.json +++ b/OutOfSchool/OutOfSchool.WebApi/appsettings.Google.json @@ -28,6 +28,7 @@ }, "Elasticsearch": { "EnsureIndex": true, + "EnableDebugMode": false, "DefaultIndex": "workshop", "Urls": [ "https://elastic.dmytrominochkin.cloud/" From cc1b297665bece1ed9ff494986190d9230f5af7c Mon Sep 17 00:00:00 2001 From: Dmytro M Date: Wed, 22 Dec 2021 09:53:45 +0200 Subject: [PATCH 05/12] add exception to error logs --- .../OutOfSchool.WebApi/Services/Images/ImageService.cs | 10 +++++----- .../Services/Images/ImageValidatorService.cs | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageService.cs index a2b34de2ab..7e384fc33f 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageService.cs @@ -125,7 +125,7 @@ public async Task UploadManyWorkshopImagesWithU } catch (Exception ex) { - logger.LogError($"Exception while uploading images for workshopId = {workshopId}: {ex.Message}"); + logger.LogError(ex, $"Exception while uploading images for workshopId = {workshopId}: {ex.Message}"); return new MultipleKeyValueOperationResult { GeneralResultMessage = Resources.ImageResource.UploadImagesError }; } @@ -139,7 +139,7 @@ public async Task UploadManyWorkshopImagesWithU catch (Exception ex) { // TODO: mark image ids in order to delete - logger.LogError($"Cannot update workshop with id = {workshopId} because of {ex.Message}"); + logger.LogError(ex, $"Cannot update workshop with id = {workshopId} because of {ex.Message}"); return new MultipleKeyValueOperationResult { GeneralResultMessage = Resources.ImageResource.UploadImagesError }; } @@ -190,7 +190,7 @@ public async Task UploadWorkshopImageWithUpdatingEntityAsync(Gu } catch (Exception ex) { - logger.LogError($"Exception while uploading images for workshopId = {workshopId}: {ex.Message}"); + logger.LogError(ex, $"Exception while uploading images for workshopId = {workshopId}: {ex.Message}"); return OperationResult.Failed(new OperationError { Code = ImageResourceCodes.UploadImagesError, Description = ImageResourceCodes.UploadImagesError }); } @@ -204,7 +204,7 @@ public async Task UploadWorkshopImageWithUpdatingEntityAsync(Gu catch (Exception ex) { // TODO: mark image id in order to delete - logger.LogError($"Cannot update workshop with id = {workshopId} because of {ex.Message}"); + logger.LogError(ex, $"Cannot update workshop with id = {workshopId} because of {ex.Message}"); return OperationResult.Failed(new OperationError { Code = ImageResourceCodes.UploadImagesError, Description = Resources.ImageResource.UploadImagesError }); } @@ -224,7 +224,7 @@ private async Task> UploadImageProcessAsync(Stream contentStream, } catch (ImageStorageException ex) { - logger.LogError($"Unable to upload image into an external storage because of {ex.Message}."); + logger.LogError(ex, $"Unable to upload image into an external storage because of {ex.Message}."); return Result.Failed(new OperationError { Code = ImageResourceCodes.ImageStorageError, Description = Resources.ImageResource.ImageStorageError }); } } diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageValidatorService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageValidatorService.cs index 7406463d39..abf9576e98 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageValidatorService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageValidatorService.cs @@ -55,12 +55,12 @@ public OperationResult Validate(Stream stream) } catch (ArgumentException ex) { - logger.LogError($"Unable to validate stream {ex.Message}"); + logger.LogError(ex, $"Unable to validate stream {ex.Message}"); return OperationResult.Failed(new OperationError { Code = ImageResourceCodes.InvalidImageFormatError, Description = Resources.ImageResource.InvalidImageFormatError }); } catch (Exception ex) { - logger.LogError($"Unable to validate stream {ex.Message}"); + logger.LogError(ex, $"Unable to validate stream {ex.Message}"); return OperationResult.Failed(new OperationError {Code = ImageResourceCodes.UnexpectedValidationError, Description = Resources.ImageResource.UnexpectedValidationError}); } } From 4a27614fb16cecd3666ed2ac3abf9379f4874cc7 Mon Sep 17 00:00:00 2001 From: Dmytro M Date: Wed, 22 Dec 2021 10:38:19 +0200 Subject: [PATCH 06/12] add error results --- OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageService.cs index 7e384fc33f..a57d93e7f6 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageService.cs @@ -106,7 +106,7 @@ public async Task UploadManyWorkshopImagesWithU var validationResult = validator.Validate(stream); if (!validationResult.Succeeded) { - logger.LogError($"Image with {nameof(fileCollection)} id = {i} isn't valid."); + logger.LogError($"Image with {nameof(fileCollection)} id = {i} isn't valid: {validationResult.Errors}"); uploadImageResults.Results.Add(i, validationResult); continue; } From a05abcc258430bd9e9e7d49018be6fd616ff78ee Mon Sep 17 00:00:00 2001 From: Dmytro M Date: Wed, 22 Dec 2021 10:57:53 +0200 Subject: [PATCH 07/12] fix log to string --- OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageService.cs index a57d93e7f6..6f3eec1c0e 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageService.cs @@ -106,7 +106,7 @@ public async Task UploadManyWorkshopImagesWithU var validationResult = validator.Validate(stream); if (!validationResult.Succeeded) { - logger.LogError($"Image with {nameof(fileCollection)} id = {i} isn't valid: {validationResult.Errors}"); + logger.LogError($"Image with {nameof(fileCollection)} id = {i} isn't valid: {string.Join(",", validationResult.Errors)}"); uploadImageResults.Results.Add(i, validationResult); continue; } From 7206fb6488b95d9780692ddb7f2c86bbd96a041a Mon Sep 17 00:00:00 2001 From: Dmytro M Date: Wed, 22 Dec 2021 11:49:17 +0200 Subject: [PATCH 08/12] add to string for operation error --- OutOfSchool/OutOfSchool.WebApi/Common/OperationError.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/OutOfSchool/OutOfSchool.WebApi/Common/OperationError.cs b/OutOfSchool/OutOfSchool.WebApi/Common/OperationError.cs index 7f6bf922fd..f3264694df 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Common/OperationError.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Common/OperationError.cs @@ -20,5 +20,10 @@ public class OperationError /// The description for this error. /// public string Description { get; set; } + + public override string ToString() + { + return $"Code: {Code}; Description: {Description}"; + } } } From c7519520867758990ae04b2675527d07d5bafd8a Mon Sep 17 00:00:00 2001 From: Dmytro M Date: Wed, 22 Dec 2021 13:46:58 +0200 Subject: [PATCH 09/12] test debug --- .../OutOfSchool.WebApi/Services/Images/ImageValidatorService.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageValidatorService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageValidatorService.cs index abf9576e98..4dcc48e646 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageValidatorService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageValidatorService.cs @@ -68,6 +68,8 @@ public OperationResult Validate(Stream stream) /// public bool ValidateImageSize(long size) { + logger.LogDebug($"Image size: {size}"); + logger.LogDebug($"Max size: {options.MaxSizeBytes}"); return size <= options.MaxSizeBytes; } From 4433e0704bbd43dc90b768ee5cbcdb963d7175e6 Mon Sep 17 00:00:00 2001 From: Dmytro M Date: Wed, 22 Dec 2021 14:02:02 +0200 Subject: [PATCH 10/12] fixed settings --- .../Services/Images/ImageValidatorService.cs | 3 --- OutOfSchool/OutOfSchool.WebApi/appsettings.json | 12 ++++++------ 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageValidatorService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageValidatorService.cs index 4dcc48e646..eb89b4703d 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageValidatorService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageValidatorService.cs @@ -2,7 +2,6 @@ using System.Drawing; using System.IO; using System.Linq; -using Microsoft.Extensions.Localization; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using OutOfSchool.WebApi.Common; @@ -68,8 +67,6 @@ public OperationResult Validate(Stream stream) /// public bool ValidateImageSize(long size) { - logger.LogDebug($"Image size: {size}"); - logger.LogDebug($"Max size: {options.MaxSizeBytes}"); return size <= options.MaxSizeBytes; } diff --git a/OutOfSchool/OutOfSchool.WebApi/appsettings.json b/OutOfSchool/OutOfSchool.WebApi/appsettings.json index c2fe96ace7..e3c36143e6 100644 --- a/OutOfSchool/OutOfSchool.WebApi/appsettings.json +++ b/OutOfSchool/OutOfSchool.WebApi/appsettings.json @@ -46,14 +46,14 @@ }, "Images": { "Workshop": { - "MinWidth": 350, - "MaxWidth": 10000, - "MinHeight": 250, - "MaxHeight": 8000, + "MinWidthPixels": 350, + "MaxWidthPixels": 10000, + "MinHeightPixels": 250, + "MaxHeightPixels": 8000, + "MaxSizeBytes": 16777216, "MaxWidthHeightRatio": 2, - "MaxSize": 16777216, "SupportedFormats": [ - "jpg", + "jpeg", "png" ] } From 69108c23b18369ee815624842bd751bb2d6a2092 Mon Sep 17 00:00:00 2001 From: Dmytro M Date: Wed, 22 Dec 2021 14:35:27 +0200 Subject: [PATCH 11/12] update builder to full --- app-cloudbuild.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app-cloudbuild.yml b/app-cloudbuild.yml index 80e1db79c2..6e7a5d5fab 100644 --- a/app-cloudbuild.yml +++ b/app-cloudbuild.yml @@ -5,7 +5,7 @@ steps: args: - build - gcr.io/$PROJECT_ID/oos-api:$SHORT_SHA - - --builder=paketobuildpacks/builder:base + - --builder=paketobuildpacks/builder:full - --buildpack=fagiani/apt - --buildpack=gcr.io/paketo-buildpacks/dotnet-core - --env=BP_DOTNET_PROJECT_PATH=./OutOfSchool/OutOfSchool.WebApi/ From 1b7bb2b35122c25d4f2a534ed889ec3509d97d1c Mon Sep 17 00:00:00 2001 From: Dmytro M Date: Wed, 22 Dec 2021 15:15:49 +0200 Subject: [PATCH 12/12] add debug sink --- .../OutOfSchool.IdentityServer/appsettings.Development.json | 5 +++-- OutOfSchool/OutOfSchool.WebApi/appsettings.Development.json | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Development.json b/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Development.json index 3cc54c7eba..a3f6a5dec2 100644 --- a/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Development.json +++ b/OutOfSchool/OutOfSchool.IdentityServer/appsettings.Development.json @@ -3,7 +3,7 @@ "DefaultConnection": "server=localhost;user=root;password=rootPassword;database=out_of_school;guidformat=binary16" }, "Serilog": { - "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], + "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.Debug" ], "MinimumLevel": { "Default": "Debug", "Override": { @@ -23,7 +23,8 @@ "retainedFileCountLimit": 2, "fileSizeLimitBytes": null } - } + }, + { "Name": "Debug" } ] } } diff --git a/OutOfSchool/OutOfSchool.WebApi/appsettings.Development.json b/OutOfSchool/OutOfSchool.WebApi/appsettings.Development.json index 8341ada6d8..31c6873ecb 100644 --- a/OutOfSchool/OutOfSchool.WebApi/appsettings.Development.json +++ b/OutOfSchool/OutOfSchool.WebApi/appsettings.Development.json @@ -7,7 +7,7 @@ } }, "Serilog": { - "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], + "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.Debug" ], "MinimumLevel": { "Default": "Debug", "Override": { @@ -27,7 +27,8 @@ "retainedFileCountLimit": 2, "fileSizeLimitBytes": null } - } + }, + { "Name": "Debug" } ] }, "Elasticsearch": {