using System; using Amazon.Runtime; namespace AWS.Logger { /// /// This class contains all the configuration options for logging messages to AWS. As messages from the application are /// sent to the logger they are queued up in a batch. The batch will be sent when either BatchPushInterval or BatchSizeInBytes /// are exceeded. /// /// /// AWS Credentials are determined using the following steps. /// 1) If the Credentials property is set /// 2) If the Profile property is set and the can be found /// 3) Use the AWS SDK for .NET fall back mechanism to find enviroment credentials. /// /// public class AWSLoggerConfig : IAWSLoggerConfig { private int batchSizeInBytes = 102400; #region Public Properties /// /// Gets and sets the LogGroup property. This is the name of the CloudWatch Logs group where /// streams will be created and log messages written to. /// public string LogGroup { get; set; } /// /// Determines whether or not to create a new Log Group, if the one specified by doesn't already exist /// If false (the default), the Log Group is created if it doesn't already exist. This requires logs:DescribeLogGroups /// permission to determine if the group exists, and logs:CreateLogGroup permission to create the group if it doesn't already exist. /// If true, creation of Log Groups is disabled. Logging functions only if the specified log group already exists. /// When creation of log groups is disabled, logs:DescribeLogGroups permission is NOT required. /// public bool DisableLogGroupCreation { get; set; } /// /// Gets and sets the Profile property. The profile is used to look up AWS credentials in the profile store. /// /// For understanding how credentials are determine view the top level documentation for AWSLoggerConfig class. /// /// public string Profile { get; set; } /// /// Gets and sets the ProfilesLocation property. If this is not set the default profile store is used by the AWS SDK for .NET /// to look up credentials. This is most commonly used when you are running an application of on-priemse under a service account. /// /// For understanding how credentials are determine view the top level documentation for AWSLoggerConfig class. /// /// public string ProfilesLocation { get; set; } /// /// Gets and sets the Credentials property. These are the AWS credentials used by the AWS SDK for .NET to make service calls. /// /// For understanding how credentials are determine view the top level documentation for AWSLoggerConfig class. /// /// public AWSCredentials Credentials { get; set; } /// /// Gets and sets the Region property. This is the AWS Region that will be used for CloudWatch Logs. If this is not /// the AWS SDK for .NET will use its fall back logic to try and determine the region through environment variables and EC2 instance metadata. /// If the Region is not set and no region is found by the SDK's fall back logic then an exception will be thrown. /// public string Region { get; set; } /// /// Gets and sets of the ServiceURL property. This is an optional property; change /// it only if you want to try a different service endpoint. Ex. for LocalStack /// public string ServiceUrl { get; set; } /// /// Gets and sets the BatchPushInterval property. For performance the log messages are sent to AWS in batch sizes. BatchPushInterval /// dictates the frequency of when batches are sent. If either BatchPushInterval or BatchSizeInBytes are exceeded the batch will be sent. /// /// The default is 3 seconds. /// /// public TimeSpan BatchPushInterval { get; set; } = TimeSpan.FromMilliseconds(3000); /// /// Gets and sets the BatchSizeInBytes property. For performance the log messages are sent to AWS in batch sizes. BatchSizeInBytes /// dictates the total size of the batch in bytes when batches are sent. If either BatchPushInterval or BatchSizeInBytes are exceeded the batch will be sent. /// /// The default is 100 Kilobytes. /// /// public int BatchSizeInBytes { get { return batchSizeInBytes; } set { if (value > Math.Pow(1024, 2)) { throw new ArgumentException("The events batch size cannot exeed 1MB. https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/cloudwatch_limits_cwl.html"); } batchSizeInBytes = value; } } /// /// Gets and sets the MaxQueuedMessages property. This specifies the maximum number of log messages that could be stored in-memory. MaxQueuedMessages /// dictates the total number of log messages that can be stored in-memory. If this is exceeded, incoming log messages will be dropped. /// /// The default is 10000. /// /// public int MaxQueuedMessages { get; set; } = 10000; /// /// Internal MonitorSleepTime property. This specifies the timespan after which the Monitor wakes up. MonitorSleepTime /// dictates the timespan after which the Monitor checks the size and time constarint on the batch log event and the existing in-memory buffer for new messages. /// /// The value is 500 Milliseconds. /// /// internal TimeSpan MonitorSleepTime = TimeSpan.FromMilliseconds(500); #endregion /// /// Default Constructor /// public AWSLoggerConfig() { } /// /// Construct instance and sets the LogGroup /// /// The CloudWatch Logs log group. public AWSLoggerConfig(string logGroup) { LogGroup = logGroup; } /// /// Gets and sets the LogStreamNameSuffix property. The LogStreamName consists of an optional user-defined LogStreamNamePrefix (that can be set here) /// followed by a DateTimeStamp as the prefix, and a user defined suffix value /// The LogstreamName then follows the pattern '[LogStreamNamePrefix]-[DateTime.Now.ToString("yyyy/MM/ddTHH.mm.ss")]-[LogStreamNameSuffix]' /// /// The default is new a Guid. /// /// public string LogStreamNameSuffix { get; set; } = Guid.NewGuid().ToString(); /// /// Gets and sets the LogStreamNamePrefix property. The LogStreamName consists of an optional user-defined LogStreamNamePrefix (that can be set here) /// followed by a DateTimeStamp as the prefix, and a user defined suffix value /// The LogstreamName then follows the pattern '[LogStreamNamePrefix]-[DateTime.Now.ToString("yyyy/MM/ddTHH.mm.ss")]-[LogStreamNameSuffix]' /// /// The default is an empty string. /// /// public string LogStreamNamePrefix { get; set; } = string.Empty; /// /// Gets and sets the LibraryLogErrors property. This is the boolean value of whether or not you would like this library to log logging errors. /// /// The default is "true". /// /// public bool LibraryLogErrors { get; set; } = true; /// /// Gets and sets the LibraryLogFileName property. This is the name of the file into which errors from the AWS.Logger.Core library will be written into. /// /// The default is "aws-logger-errors.txt". /// /// public string LibraryLogFileName { get; set; } = "aws-logger-errors.txt"; /// /// Gets the FlushTimeout property. The value is in milliseconds. When performing a flush of the in-memory queue this is the maximum period of time allowed to send the remaining /// messages before it will be aborted. If this is exceeded, incoming log messages will be dropped. /// /// The default is 30000 milliseconds. /// /// public TimeSpan FlushTimeout { get; set; } = TimeSpan.FromMilliseconds(30000); } }