Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/google logging #472

Merged
merged 12 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.GoogleCloudLogging" Version="3.6.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.321">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
3 changes: 2 additions & 1 deletion OutOfSchool/OutOfSchool.IdentityServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions OutOfSchool/OutOfSchool.IdentityServer/appsettings.Azure.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"Serilog": {
"Using": [ "Serilog.Sinks.Console" ],
"WriteTo": [
{
"Name": "Console",
"Args": {
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
}
},
{ "Name": "Debug" }
}
]
},
"IdentityAccessConfig": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
{
"Serilog": {
"Using": [ "Serilog.Sinks.Console" ],
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "Debug"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
"DefaultConnection": "server=localhost;user=root;password=rootPassword;database=out_of_school;guidformat=binary16"
},
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.Debug" ],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Information",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "Console"
Expand Down
16 changes: 12 additions & 4 deletions OutOfSchool/OutOfSchool.IdentityServer/appsettings.Google.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
{
"Serilog": {
"Using": [ "Serilog.Sinks.GoogleCloudLogging" ],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Information",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "Console",
"Name": "GoogleCloudLogging",
"Args": {
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
"useJsonOutput": "true",
"useLogCorrelation": "true"
}
},
{ "Name": "Debug" }
}
]
},
"IdentityAccessConfig": {
Expand Down
7 changes: 0 additions & 7 deletions OutOfSchool/OutOfSchool.IdentityServer/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@
"FromLogContext"
]
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Email": {
"AddressFrom": "[email protected]",
"NameFrom": "Oos-Backend",
Expand Down
5 changes: 5 additions & 0 deletions OutOfSchool/OutOfSchool.WebApi/Common/OperationError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@ public class OperationError
/// The description for this error.
/// </value>
public string Description { get; set; }

public override string ToString()
{
return $"Code: {Code}; Description: {Description}";
}
}
}
2 changes: 2 additions & 0 deletions OutOfSchool/OutOfSchool.WebApi/Config/ElasticConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class ElasticConfig

public bool EnsureIndex { get; set; }

public bool EnableDebugMode { get; set; }

[Required]
public string DefaultIndex { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (config.EnableDebugMode)
{
settings
.EnableDebugMode(details =>
{
if (details.RequestBodyInBytes != null)
Expand Down Expand Up @@ -70,6 +70,7 @@ public static void AddElasticsearch(this IServiceCollection services, ElasticCon
Console.Error.WriteLine($"Reason: {details.OriginalException}");
}
});
}

AddDefaultMappings(settings, config.DefaultIndex);

Expand Down
7 changes: 4 additions & 3 deletions OutOfSchool/OutOfSchool.WebApi/OutOfSchool.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="5.0.2" />
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.GoogleCloudLogging" Version="3.6.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
24 changes: 3 additions & 21 deletions OutOfSchool/OutOfSchool.WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
12 changes: 6 additions & 6 deletions OutOfSchool/OutOfSchool.WebApi/Services/Images/ImageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public async Task<MultipleKeyValueOperationResult> 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: {string.Join(",", validationResult.Errors)}");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we will return only the first error if not succeeded in this case

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

join is more general then array[0] :)

uploadImageResults.Results.Add(i, validationResult);
continue;
}
Expand All @@ -125,7 +125,7 @@ public async Task<MultipleKeyValueOperationResult> 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 };
}

Expand All @@ -139,7 +139,7 @@ public async Task<MultipleKeyValueOperationResult> 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 };
}

Expand Down Expand Up @@ -190,7 +190,7 @@ public async Task<OperationResult> 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 });
}

Expand All @@ -204,7 +204,7 @@ public async Task<OperationResult> 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 });
}

Expand All @@ -224,7 +224,7 @@ private async Task<Result<string>> 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<string>.Failed(new OperationError { Code = ImageResourceCodes.ImageStorageError, Description = Resources.ImageResource.ImageStorageError });
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,12 +54,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});
}
}
Expand Down
16 changes: 10 additions & 6 deletions OutOfSchool/OutOfSchool.WebApi/appsettings.Azure.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
9 changes: 9 additions & 0 deletions OutOfSchool/OutOfSchool.WebApi/appsettings.Compose.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
{
"Serilog": {
"Using": [ "Serilog.Sinks.Console" ],
"WriteTo": [
{
"Name": "Console"
}
]
},
"Identity": {
"Authority": "https://oos.local/identity"
},
Expand All @@ -12,6 +20,7 @@
},
"Elasticsearch": {
"EnsureIndex": true,
"EnableDebugMode": false,
"DefaultIndex": "workshop",
"Urls": [
"http://elasticsearch:9200/"
Expand Down
31 changes: 24 additions & 7 deletions OutOfSchool/OutOfSchool.WebApi/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,34 @@
"DatabaseName": "Images"
}
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.Debug" ],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Information",
"System": "Warning"
}
},
"FilePath": "Loggs\\log.log"
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "File",
"Args": {
"path": "Loggs/log.log",
"rollingInterval": "Day",
"retainedFileCountLimit": 2,
"fileSizeLimitBytes": null
}
},
{ "Name": "Debug" }
]
},

"Elasticsearch": {
"EnsureIndex": false,
"EnableDebugMode": true,
"DefaultIndex": "workshop",
"Urls": [
"http://localhost:9200/"
Expand Down
Loading