Serilog.Sinks.AwsCloudWatch 4.3.37

Serilog Sink for AWS CloudWatch

This Serilog Sink allows to log to AWS CloudWatch.

Version and build status

NuGet version

Usage

There are two important aspects for configuring this library. The first is providing the configuration options necessary via the ICloudWatchSinkOptions implementation. And the second is configuring the AWS Credentials. Both of these are required to log to CloudWatch.

Installation

dotnet add package Serilog.Sinks.AwsCloudWatch

CloudWatchSinkOptions

This library provides an extension method which takes in only a ICloudWatchSinkOptions instance and the IAmazonCloudWatchLogs instance.

Configuration via Code First

The preferred approach for configuration is to construct the necessary objects via code and pass them directly to the library extension method.

  // name of the log group
  var logGroupName = "myLogGroup/" + env.EnvironmentName;

  // customer formatter
  var formatter = new MyCustomTextFormatter();

  // options for the sink defaults in https://github.com/Cimpress-MCP/serilog-sinks-awscloudwatch/blob/master/src/Serilog.Sinks.AwsCloudWatch/CloudWatchSinkOptions.cs
  var options = new CloudWatchSinkOptions
  {
    // the name of the CloudWatch Log group for logging
    LogGroupName = logGroupName,

    // the main formatter of the log event
    TextFormatter = formatter,
    
    // other defaults defaults
    MinimumLogEventLevel = LogEventLevel.Information,
    BatchSizeLimit = 100,
    QueueSizeLimit = 10000,
    Period = TimeSpan.FromSeconds(10),
    CreateLogGroup = true,
    LogStreamNameProvider = new DefaultLogStreamProvider(),
    RetryAttempts = 5
  };

  // setup AWS CloudWatch client
  var client = new AmazonCloudWatchLogsClient(myAwsRegion);

  // Attach the sink to the logger configuration
  Log.Logger = new LoggerConfiguration()
    .WriteTo.AmazonCloudWatch(options, client)
    .CreateLogger();
Configuration via Fluent Code First

Call the extension method passing the configuration values that you wish to make use of.

  // setup AWS CloudWatch client
  var client = myAppConfigRoot.GetAWSOptions().CreateServiceClient<IAmazonCloudWatchLogs>();

  // Attach the sink to the logger configuration
  Log.Logger = new LoggerConfiguration()
    .WriteTo.AmazonCloudWatch(
		"myLogGroup/" + env.EnvironmentName, 
		batchSizeLimit = 100,
		queueSizeLimit = 10000,
		batchUploadPeriodInSeconds = 15,
		createLogGroup = true,
		maxRetryAttempts = 3
		cloudWatchClient = client)
    .CreateLogger();
Configuration via config file

While not recommended, it is still possible to config the library via a configuration file. There are two libraries which provide these capabilities.

  • Serilog.Settings.AppSettings Configuration is done by App.config or Web.config file. Create a concrete implementation of the ICloudWatchSinkOptions interface, and specify the necessary configuration values. Then in the configuration file, specify the following:
  <!-- {Assembly} name is `typeof(YourOptionsClass).AssemblyQualifiedName` and {Namespace} is the class namespace. -->
  <add key="serilog:write-to:AmazonCloudWatch.options" value="{namespace}.CloudWatchSinkOptions, {assembly}, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
  • Serilog.Settings.Configuration Configuration is done by an appsettings.json file (or optional specific override. Create a concrete implementation of the ICloudWatchSinkOptions interface, and specify the necessary configuration values. Then in the configuration file, specify the following:
  // {Assembly} name is `typeof(YourOptionsClass).AssemblyQualifiedName` and {Namespace} is the class namespace.
  {
    "Args": {
      "options": "{namespace}.CloudWatchSinkOptions, {assembly}"
    }
  }

Alternatively you may configure the library without creating a concrete instance of the ICloudWatchSinkOptions interface however this will cause the AWS Service client to follow the credential rules in the official AWS SDK documentation. You may configure any of the passed values in the Extension method.

  {
    "Serilog": {
        "Using": [ "Serilog.Sinks.AwsCloudWatch" ],
        "MinimumLevel": "Verbose",
        "WriteTo": [            
            {
                "Name": "AmazonCloudWatch",
                "Args": {
                    "logGroup": "your-app",
                    "logStreamPrefix": "environment/component",
                    "restrictedToMinimumLevel": "Verbose"
                }
            }
        ]
    }
  }

or using XML:

  <add key="serilog:using:AwsCloudWatch" value="Serilog.Sinks.AwsCloudWatch" />
  <add key="serilog:write-to:AmazonCloudWatch.logGroup" value="your-app" />
  <add key="serilog:write-to:AmazonCloudWatch.logStreamPrefix" value="environment/component" />
  <add key="serilog:write-to:AmazonCloudWatch.restrictedToMinimumLevel" value="Verbose" />

Configuring Credentials

AmazonCloudWatchLogsClient from the AWS SDK requires AWS credentials. To correctly associate credentials with the library, please refer to The Official AWS Recommendation on C# for credentials management. To reiterate here:

  • Preferred => Use IAM Profile set on your instance, machine, lambda function.
  • Create a credentials profile with your AWS credentials.
  • Use Environment Variables
  • Manually construct the credentials via:
  var options = new CredentialProfileOptions { AccessKey = "access_key", SecretKey = "secret_key" };
  var profile = new Amazon.Runtime.CredentialManagement.CredentialProfile("basic_profile", options);
  profile.Region = GetBySystemName("eu-west-1"); // OR RegionEndpoint.EUWest1
  var netSDKFile = new NetSDKCredentialsFile();
  netSDKFile.RegisterProfile(profile);

Troubleshooting

  • Cannot find region in config or Cannot find credentials in config AWS configuration is not complete. Refer to the Configuring Credentials section to complete the configuration.

  • Errors related to the setup of the Sink (for example, invalid AWS credentials), or problems during sending the data are logged to Serilog's SelfLog. Short version, enable it with something like the following command:

  Serilog.Debugging.SelfLog.Enable(Console.Error);

Contribution

We value your input as part of direct feedback to us, by filing issues, or preferably by directly contributing improvements:

  1. Fork this repository
  2. Create a branch
  3. Contribute
  4. Pull request

No packages depend on Serilog.Sinks.AwsCloudWatch.

.NET 6.0

.NET Standard 2.0

Version Downloads Last updated
4.3.37 5 07/25/2024
4.2.34 3 07/22/2024
4.2.29 19 05/23/2024
4.2.25 25 05/15/2024
4.2.24 2 05/15/2024
4.2.19 21 03/21/2024
4.2.17 3 03/21/2024
4.1.15 5 03/21/2024
4.1.14 5 03/21/2024
4.0.182 11 01/07/2024
4.0.180 5 02/07/2024
4.0.178 28 09/22/2023
4.0.171 5 08/29/2023
4.0.168 11 06/17/2021
4.0.167 4 02/15/2024
4.0.161 4 02/15/2024
4.0.159 43 07/01/2020
4.0.155 5 08/25/2023
4.0.149 13 08/24/2023
4.0.145 4 08/29/2023
4.0.143 5 08/31/2023
4.0.140 7 08/31/2023
4.0.138 4 08/30/2023
4.0.135 3 08/30/2023
4.0.131 5 08/31/2023
4.0.125 3 02/15/2024
4.0.119 9 08/31/2023
3.0.114 54 06/04/2021
3.0.110 4 08/29/2023
3.0.106 5 08/31/2023
3.0.101 10 08/25/2023
3.0.97 11 11/01/2023
3.0.93 4 02/15/2024
3.0.86 5 02/15/2024
3.0.77 5 02/15/2024
2.0.75 3 02/15/2024
2.0.73 4 02/15/2024
2.0.67 4 02/15/2024
2.0.62 4 02/15/2024
2.0.54 13 02/12/2024
2.0.51 4 02/15/2024
2.0.49 6 02/15/2024
2.0.46 4 02/15/2024
2.0.45 4 02/15/2024
2.0.41 15 11/02/2023
2.0.39 3 02/15/2024
2.0.0 10 06/12/2023
0.1.19 4 02/15/2024
0.1.17 6 02/15/2024
0.1.13 4 02/15/2024
0.1.11 3 02/15/2024
0.0.1-beta-8 31 08/26/2023