Skip to content

Commit

Permalink
WIP: writing logs to CloudWatch
Browse files Browse the repository at this point in the history
  • Loading branch information
mareg committed Sep 3, 2024
1 parent 40fd096 commit efdab43
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 2 deletions.
1 change: 1 addition & 0 deletions Libraries/CO.CDP.AwsServices/CO.CDP.AwsServices.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.7.301" />
<PackageReference Include="AWSSDK.S3" Version="3.7.402.2" />
<PackageReference Include="AWSSDK.SQS" Version="3.7.400.11" />
<PackageReference Include="AWSSDK.CloudWatch" Version="3.7.401.11" />
<ProjectReference Include="..\CO.CDP.MQ\CO.CDP.MQ.csproj" />
</ItemGroup>

Expand Down
8 changes: 8 additions & 0 deletions Libraries/CO.CDP.AwsServices/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Amazon.Auth.AccessControlPolicy.ActionIdentifiers;
using Amazon.Extensions.NETCore.Setup;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.SQS;
using Amazon.CloudWatch;
using CO.CDP.AwsServices.S3;
using CO.CDP.AwsServices.Sqs;
using CO.CDP.MQ;
Expand Down Expand Up @@ -64,6 +66,12 @@ public static IServiceCollection AddAwsS3Service(this IServiceCollection service
.AddSingleton<IFileHostManager, AwsFileManager>();
}

public static IServiceCollection AddAwsCloudWatchService(this IServiceCollection services)
{
return services
.AddAWSService<IAmazonCloudWatch>();
}

public static IServiceCollection AddAwsSqsService(this IServiceCollection services)
{
return services
Expand Down
1 change: 0 additions & 1 deletion Services/CO.CDP.DataSharing.WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
//builder.Services.AddAuthorization();
builder.Services.AddOrganisationAuthorization();


builder.Services
.AddAwsConfiguration(builder.Configuration)
.AddAwsS3Service();
Expand Down
1 change: 1 addition & 0 deletions Services/CO.CDP.Tenant.WebApi/CO.CDP.Tenant.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<PackageReference Include="Serilog.Sinks.AwsCloudWatch" Version="4.3.37" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<ProjectReference Include="..\..\Libraries\CO.CDP.Authentication\CO.CDP.Authentication.csproj" />
<ProjectReference Include="..\..\Libraries\CO.CDP.AwsServices\CO.CDP.AwsServices.csproj" />
<ProjectReference Include="..\CO.CDP.OrganisationInformation.Persistence\CO.CDP.OrganisationInformation.Persistence.csproj" />
<ProjectReference Include="..\CO.CDP.OrganisationInformation\CO.CDP.OrganisationInformation.csproj" />
<ProjectReference Include="..\..\Libraries\CO.CDP.Functional\CO.CDP.Functional.csproj" />
Expand Down
23 changes: 23 additions & 0 deletions Services/CO.CDP.Tenant.WebApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Reflection;
using Amazon;
using Amazon.CloudWatchLogs;
using CO.CDP.Authentication;
using CO.CDP.AwsServices;
using CO.CDP.Configuration.Assembly;
using CO.CDP.Configuration.ForwardedHeaders;
using CO.CDP.Configuration.Helpers;
Expand All @@ -13,6 +16,8 @@
using Tenant = CO.CDP.Tenant.WebApi.Model.Tenant;
using TenantLookup = CO.CDP.OrganisationInformation.TenantLookup;
using Serilog;
using Serilog.Formatting.Compact;
using Serilog.Sinks.AwsCloudWatch;

var builder = WebApplication.CreateBuilder(args);
builder.ConfigureForwardedHeaders();
Expand Down Expand Up @@ -44,6 +49,10 @@

if (Assembly.GetEntryAssembly().IsRunAs("CO.CDP.Tenant.WebApi"))
{
builder.Services
.AddAwsConfiguration(builder.Configuration)
.AddAwsCloudWatchService();

builder.Services.AddHealthChecks()
.AddNpgSql(ConnectionStringHelper.GetConnectionString(builder.Configuration,
"OrganisationInformationDatabase"));
Expand All @@ -52,6 +61,20 @@
.ReadFrom.Services(services)
.Enrich.FromLogContext()
);

using var log = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.AmazonCloudWatch(
logGroup: builder.Configuration["Aws:LogGroup"],
logStreamPrefix: builder.Configuration["Aws:LogStream"],
cloudWatchClient: new AmazonCloudWatchLogsClient(),
textFormatter: new CompactJsonFormatter())
.WriteTo.Console()
.CreateLogger();

log.Verbose("Writing introduction message...");
log.Information("Hi there! How are you?");
log.Verbose("Wrote introduction message!");
}

var app = builder.Build();
Expand Down
4 changes: 4 additions & 0 deletions Services/CO.CDP.Tenant.WebApi/appsettings.AwsDevelopment.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@
},
"Features": {
"SwaggerUI": true
},
"Aws": {
"LogGroup": "/ecs/tenant",
"LogStream": "development/tenant"
}
}
4 changes: 4 additions & 0 deletions Services/CO.CDP.Tenant.WebApi/appsettings.AwsIntegration.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@
},
"Features": {
"SwaggerUI": true
},
"Aws": {
"LogGroup": "/ecs/tenant",
"LogStream": "integration/tenant"
}
}
4 changes: 4 additions & 0 deletions Services/CO.CDP.Tenant.WebApi/appsettings.AwsStaging.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@
},
"Features": {
"SwaggerUI": true
},
"Aws": {
"LogGroup": "/ecs/tenant",
"LogStream": "staging/tenant"
}
}
19 changes: 18 additions & 1 deletion Services/CO.CDP.Tenant.WebApi/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@
},
"WriteTo": [
{
"Name": "Console"
"Name": "AmazonCloudWatch",
"Args": {
"serviceUrl": "http://localhost:4566",
"region": "eu-west-1",
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact",
"logGroup": "test",
"logStreamPrefix": "test",
"restrictedToMinimumLevel": "Verbose"
}
}
]
},
Expand All @@ -24,5 +32,14 @@
},
"Features": {
"SwaggerUI": true
},
"Aws": {
"Credentials": {
"AccessKeyId": "test",
"SecretAccessKey": "test"
},
"ServiceURL": "http://localhost:4566",
"LogGroup": "test",
"LogStream": "test"
}
}
4 changes: 4 additions & 0 deletions Services/CO.CDP.Tenant.WebApi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,9 @@
},
"Features": {
"SwaggerUI": false
},
"Aws": {
"LogGroup": "/ecs/tenant",
"LogStream": "production/tenant"
}
}

0 comments on commit efdab43

Please sign in to comment.