Serilog.Sinks.AwsCloudWatch
4.3.37
dotnet add package Serilog.Sinks.AwsCloudWatch --version 4.3.37
NuGet\Install-Package Serilog.Sinks.AwsCloudWatch -Version 4.3.37
<PackageReference Include="Serilog.Sinks.AwsCloudWatch" Version="4.3.37" />
paket add Serilog.Sinks.AwsCloudWatch --version 4.3.37
#r "nuget: Serilog.Sinks.AwsCloudWatch, 4.3.37"
// Install Serilog.Sinks.AwsCloudWatch as a Cake Addin #addin nuget:?package=Serilog.Sinks.AwsCloudWatch&version=4.3.37 // Install Serilog.Sinks.AwsCloudWatch as a Cake Tool #tool nuget:?package=Serilog.Sinks.AwsCloudWatch&version=4.3.37
Serilog Sink for AWS CloudWatch
This Serilog Sink allows to log to AWS CloudWatch.
Version and build status
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
orWeb.config
file. Create a concrete implementation of theICloudWatchSinkOptions
interface, and specify the necessary configuration values. Then in the configuration file, specify the following:
<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 theICloudWatchSinkOptions
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
orCannot 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:
- Fork this repository
- Create a branch
- Contribute
- Pull request
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- AWSSDK.CloudWatchLogs (>= 3.7.102.27)
- Serilog (>= 3.0.0)
- Serilog.Sinks.PeriodicBatching (>= 3.1.0)
-
net6.0
- AWSSDK.CloudWatchLogs (>= 3.7.102.27)
- Serilog (>= 3.0.0)
- Serilog.Sinks.PeriodicBatching (>= 3.1.0)
NuGet packages (17)
Showing the top 5 NuGet packages that depend on Serilog.Sinks.AwsCloudWatch:
Package | Downloads |
---|---|
Serilog.Sinks.AwsCloudWatch.Extensions
Configuration extension for the AwsCloudWatch Serilog sink |
|
Informatique.Base.Core
Base classed used in Project in Core |
|
CBORD.Omni.Domain
Package Description |
|
LShared.Frameworks
Package Description |
|
ITMakers.InfraestructureBase
Shared libraries for infrastructure in IT Makers' apps |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
4.3.37 | 375,705 | 7/23/2024 |
4.2.34 | 8,629 | 7/19/2024 |
4.2.29 | 198,920 | 5/20/2024 |
4.2.25 | 109,941 | 4/11/2024 |
4.2.24 | 18,967 | 4/2/2024 |
4.2.19 | 51,111 | 3/18/2024 |
4.2.17 | 5,114 | 3/15/2024 |
4.1.15 | 1,055 | 3/15/2024 |
4.1.14 | 320 | 3/15/2024 |
4.0.182 | 930,481 | 9/20/2023 |
4.0.180 | 545 | 9/19/2023 |
4.0.178 | 797,795 | 7/12/2023 |
4.0.171 | 6,296,108 | 4/22/2021 |
4.0.168 | 626,003 | 2/11/2021 |
4.0.167 | 45,613 | 2/11/2021 |
4.0.161 | 723,806 | 9/9/2020 |
4.0.159 | 1,065,121 | 2/28/2020 |
4.0.155 | 815,950 | 5/22/2019 |
4.0.149 | 230,911 | 2/22/2019 |
4.0.145 | 4,845 | 2/19/2019 |
4.0.143 | 30,938 | 2/11/2019 |
4.0.140 | 246,277 | 10/15/2018 |
4.0.138 | 2,095 | 10/12/2018 |
4.0.135 | 1,788 | 10/11/2018 |
4.0.131 | 155,059 | 9/4/2018 |
4.0.125 | 155,151 | 7/18/2018 |
4.0.119 | 80,456 | 7/3/2018 |
3.0.114 | 163,367 | 6/22/2018 |
3.0.110 | 3,334 | 6/22/2018 |
3.0.106 | 167,382 | 5/16/2018 |
3.0.101 | 25,378 | 5/3/2018 |
3.0.97 | 163,987 | 2/1/2018 |
3.0.93 | 87,580 | 1/5/2018 |
3.0.86 | 17,382 | 11/6/2017 |
3.0.77 | 4,856 | 10/30/2017 |
2.0.75 | 4,837 | 10/27/2017 |
2.0.73 | 48,231 | 8/14/2017 |
2.0.67 | 55,551 | 5/12/2017 |
2.0.62 | 6,848 | 4/26/2017 |
2.0.54 | 12,591 | 3/9/2017 |
2.0.51 | 2,755 | 2/8/2017 |
2.0.49 | 5,743 | 10/28/2016 |
2.0.46 | 1,981 | 10/12/2016 |
2.0.45 | 1,320 | 10/11/2016 |
2.0.41 | 1,562 | 10/9/2016 |
2.0.39 | 1,551 | 9/15/2016 |
2.0.0 | 4,125 | 6/29/2016 |
0.1.19 | 1,683 | 4/28/2016 |
0.1.17 | 1,355 | 4/28/2016 |
0.1.13 | 1,374 | 3/20/2016 |
0.1.11 | 8,178 | 3/20/2016 |
0.0.1-beta-8 | 1,114 | 3/20/2016 |