Skip to content

Commit

Permalink
(#245) Amazon CloudWatch Logs
Browse files Browse the repository at this point in the history
  • Loading branch information
phongnguyend committed Jul 20, 2024
1 parent d19bb60 commit 539677f
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="AWS.Logger.SeriLog" Version="3.4.3" />
<PackageReference Include="AWSSDK.S3" Version="3.7.304.8" />
<PackageReference Include="Azure.Communication.Sms" Version="1.0.1" />
<PackageReference Include="Azure.Data.AppConfiguration" Version="1.3.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Azure.Monitor.OpenTelemetry.Exporter;
using Amazon.Runtime;
using AWS.Logger;
using AWS.Logger.SeriLog;
using Azure.Monitor.OpenTelemetry.Exporter;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
Expand All @@ -9,6 +12,7 @@
using OpenTelemetry.Trace;
using Serilog;
using Serilog.Exceptions;
using Serilog.Formatting.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
Expand Down Expand Up @@ -64,6 +68,19 @@ private static void UseClassifiedAdsLogger(this IWebHostEnvironment env, Logging
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] [TraceId: {TraceId}] [MachineName: {MachineName}] [ProcessId: {ProcessId}] {Message:lj}{NewLine}{Exception}",
restrictedToMinimumLevel: options.File.MinimumLogEventLevel);

if (options?.AwsCloudWatch?.IsEnabled ?? false)
{
loggerConfiguration
.WriteTo.AWSSeriLog(new AWSLoggerConfig(options.AwsCloudWatch.LogGroup)
{
LogStreamNamePrefix = options.AwsCloudWatch.LogStreamNamePrefix,
Region = options.AwsCloudWatch.Region,
Credentials = new BasicAWSCredentials(options.AwsCloudWatch.AccessKey, options.AwsCloudWatch.SecretKey),
},
iFormatProvider: null,
textFormatter: new JsonFormatter());
}

Log.Logger = loggerConfiguration.CreateLogger();
}

Expand Down Expand Up @@ -281,6 +298,19 @@ private static void UseClassifiedAdsLogger(this IHostEnvironment env, LoggingOpt
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] [TraceId: {TraceId}] [MachineName: {MachineName}] [ProcessId: {ProcessId}] {Message:lj}{NewLine}{Exception}",
restrictedToMinimumLevel: options.File.MinimumLogEventLevel);

if (options?.AwsCloudWatch?.IsEnabled ?? false)
{
loggerConfiguration
.WriteTo.AWSSeriLog(new AWSLoggerConfig(options.AwsCloudWatch.LogGroup)
{
LogStreamName = options.AwsCloudWatch.LogStreamNamePrefix,
Region = options.AwsCloudWatch.Region,
Credentials = new BasicAWSCredentials(options.AwsCloudWatch.AccessKey, options.AwsCloudWatch.SecretKey),
},
iFormatProvider: null,
textFormatter: new JsonFormatter());
}

Log.Logger = loggerConfiguration.CreateLogger();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class LoggingOptions

public ApplicationInsightsOptions ApplicationInsights { get; set; }

public AwsCloudWatchOptions AwsCloudWatch { get; set; }

public OpenTelemetryOptions OpenTelemetry { get; set; }

public class FileOptions
Expand All @@ -36,6 +38,21 @@ public class ApplicationInsightsOptions
public string InstrumentationKey { get; set; }
}

public class AwsCloudWatchOptions
{
public bool IsEnabled { get; set; }

public string LogGroup { get; set; }

public string LogStreamNamePrefix { get; set; }

public string Region { get; set; }

public string AccessKey { get; set; }

public string SecretKey { get; set; }
}

public class OpenTelemetryOptions
{
public bool IsEnabled { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="AWS.Logger.SeriLog" Version="3.4.3" />
<PackageReference Include="AWSSDK.S3" Version="3.7.304.8" />
<PackageReference Include="Azure.Communication.Sms" Version="1.0.1" />
<PackageReference Include="Azure.Data.AppConfiguration" Version="1.3.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Azure.Monitor.OpenTelemetry.Exporter;
using Amazon.Runtime;
using AWS.Logger;
using AWS.Logger.SeriLog;
using Azure.Monitor.OpenTelemetry.Exporter;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
Expand All @@ -9,6 +12,7 @@
using OpenTelemetry.Trace;
using Serilog;
using Serilog.Exceptions;
using Serilog.Formatting.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
Expand Down Expand Up @@ -64,6 +68,19 @@ private static void UseClassifiedAdsLogger(this IWebHostEnvironment env, Logging
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] [TraceId: {TraceId}] [MachineName: {MachineName}] [ProcessId: {ProcessId}] {Message:lj}{NewLine}{Exception}",
restrictedToMinimumLevel: options.File.MinimumLogEventLevel);

if (options?.AwsCloudWatch?.IsEnabled ?? false)
{
loggerConfiguration
.WriteTo.AWSSeriLog(new AWSLoggerConfig(options.AwsCloudWatch.LogGroup)
{
LogStreamNamePrefix = options.AwsCloudWatch.LogStreamNamePrefix,
Region = options.AwsCloudWatch.Region,
Credentials = new BasicAWSCredentials(options.AwsCloudWatch.AccessKey, options.AwsCloudWatch.SecretKey),
},
iFormatProvider: null,
textFormatter: new JsonFormatter());
}

Log.Logger = loggerConfiguration.CreateLogger();
}

Expand Down Expand Up @@ -281,6 +298,19 @@ private static void UseClassifiedAdsLogger(this IHostEnvironment env, LoggingOpt
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] [TraceId: {TraceId}] [MachineName: {MachineName}] [ProcessId: {ProcessId}] {Message:lj}{NewLine}{Exception}",
restrictedToMinimumLevel: options.File.MinimumLogEventLevel);

if (options?.AwsCloudWatch?.IsEnabled ?? false)
{
loggerConfiguration
.WriteTo.AWSSeriLog(new AWSLoggerConfig(options.AwsCloudWatch.LogGroup)
{
LogStreamName = options.AwsCloudWatch.LogStreamNamePrefix,
Region = options.AwsCloudWatch.Region,
Credentials = new BasicAWSCredentials(options.AwsCloudWatch.AccessKey, options.AwsCloudWatch.SecretKey),
},
iFormatProvider: null,
textFormatter: new JsonFormatter());
}

Log.Logger = loggerConfiguration.CreateLogger();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class LoggingOptions

public ApplicationInsightsOptions ApplicationInsights { get; set; }

public AwsCloudWatchOptions AwsCloudWatch { get; set; }

public OpenTelemetryOptions OpenTelemetry { get; set; }

public class FileOptions
Expand All @@ -36,6 +38,21 @@ public class ApplicationInsightsOptions
public string InstrumentationKey { get; set; }
}

public class AwsCloudWatchOptions
{
public bool IsEnabled { get; set; }

public string LogGroup { get; set; }

public string LogStreamNamePrefix { get; set; }

public string Region { get; set; }

public string AccessKey { get; set; }

public string SecretKey { get; set; }
}

public class OpenTelemetryOptions
{
public bool IsEnabled { get; set; }
Expand Down

0 comments on commit 539677f

Please sign in to comment.