Implementation for accessing CloudWatchLogs You can use Amazon CloudWatch Logs to monitor, store, and access your log files from EC2 instances, Amazon CloudTrail, or other sources. You can then retrieve the associated log data from CloudWatch Logs using the Amazon CloudWatch console, the CloudWatch Logs commands in the AWS CLI, the CloudWatch Logs API, or the CloudWatch Logs SDK.

You can use CloudWatch Logs to:

  • Monitor Logs from Amazon EC2 Instances in Real-time: You can use CloudWatch Logs to monitor applications and systems using log data. For example, CloudWatch Logs can track the number of errors that occur in your application logs and send you a notification whenever the rate of errors exceeds a threshold you specify. CloudWatch Logs uses your log data for monitoring; so, no code changes are required. For example, you can monitor application logs for specific literal terms (such as "NullReferenceException") or count the number of occurrences of a literal term at a particular position in log data (such as "404" status codes in an Apache access log). When the term you are searching for is found, CloudWatch Logs reports the data to a Amazon CloudWatch metric that you specify.

  • Monitor Amazon CloudTrail Logged Events: You can create alarms in Amazon CloudWatch and receive notifications of particular API activity as captured by CloudTrail and use the notification to perform troubleshooting.

  • Archive Log Data: You can use CloudWatch Logs to store your log data in highly durable storage. You can change the log retention setting so that any log events older than this setting are automatically deleted. The CloudWatch Logs agent makes it easy to quickly send both rotated and non-rotated log data off of a host and into the log service. You can then access the raw log data when you need it.

Inheritance: AmazonServiceClient, IAmazonCloudWatchLogs
Beispiel #1
0
 protected CloudWatchLogger( AmazonCloudWatchLogsClient client, string groupName, string streamName, string sequenceToken = null )
 {
     Client = client;
       GroupName = groupName;
       StreamName = streamName;
       SequenceToken = sequenceToken;
 }
Beispiel #2
0
        /// <summary>
        /// 附加到现有日志流并开始记录
        /// </summary>
        /// <param name="client">AWS 客户端</param>
        /// <param name="groupName">日志组名称</param>
        /// <param name="streamName">日志流名称</param>
        /// <returns></returns>
        public static CloudWatchLogger AppendStream( AmazonCloudWatchLogsClient client, string groupName, string streamName )
        {
            var response = client.DescribeLogStreams( new DescribeLogStreamsRequest { OrderBy = OrderBy.LogStreamName, LogGroupName = groupName, LogStreamNamePrefix = streamName, Limit = 1 } );
              var streamInfo = response.LogStreams.FirstOrDefault();

              if ( streamInfo == null || streamInfo.LogStreamName != streamName )
            return CreateStream( client, groupName, streamName );

              return new CloudWatchLogger( client, groupName, streamName, streamInfo.UploadSequenceToken );
        }
Beispiel #3
0
 /// <summary>
 /// 创建一个新的日志流并开始记录
 /// </summary>
 /// <param name="client">AWS 客户端</param>
 /// <param name="groupName">日志组名称</param>
 /// <param name="streamName">日志流名称</param>
 /// <returns></returns>
 public static CloudWatchLogger CreateStream( AmazonCloudWatchLogsClient client, string groupName, string streamName )
 {
     var response = client.CreateLogStream( new CreateLogStreamRequest { LogGroupName = groupName, LogStreamName = streamName } );
       return new CloudWatchLogger( client, groupName, streamName );
 }
        void Writer(object sender, ElapsedEventArgs e)
        {
            try
            {
                var logEvents = new List<InputLogEvent>();

                var more = true;
                while (more)
                {
                    InputLogEvent item;
                    more = items.TryTake(out item);
                    if (more)
                        logEvents.Add(item);
                }

                if (logEvents.Count == 0)
                    return;

                if (!Settings.Default.SendUsageData)
                    return;

                using (var logs = new AmazonCloudWatchLogsClient(AwsKeys.AccessKey, AwsKeys.SecretKey, RegionEndpoint.APSoutheast2))
                {
                    var request = new PutLogEventsRequest(AwsKeys.GroupName, LogStreamName, logEvents);

                    var describeLogStreamsRequest = new DescribeLogStreamsRequest(AwsKeys.GroupName);
                    var describeLogStreamsResponse = logs.DescribeLogStreams(describeLogStreamsRequest);
                    var logStreams = describeLogStreamsResponse.LogStreams;
                    var logStream = logStreams.FirstOrDefault(ls => ls.LogStreamName == LogStreamName);
                    if (logStream != null)
                    {
                        var token = logStream.UploadSequenceToken;
                        request.SequenceToken = token;
                        checkResponse(logs.PutLogEvents(request));
                    }
                    else
                    {
                        var createRequest = new CreateLogStreamRequest(AwsKeys.GroupName, LogStreamName);
                        checkResponse(logs.CreateLogStream(createRequest));
                        checkResponse(logs.PutLogEvents(request));
                    }
                }
            }
            catch(Exception ee)
            {
                TraceInfo.WriteLine(ee.Message);
                TraceDebug.WriteLine(ee.StackTrace);
            }
            finally
            {
                if(timer != null)
                    timer.Start();
            }
        }
Beispiel #5
0
        static void Main( string[] args )
        {
            var client = new AmazonCloudWatchLogsClient();

              var logger = new ConsoleLogger() + new TextFileLogger( @"C:\Temp\Logs\1.log" ) + new TextFileLogger( new DirectoryInfo( @"C:\Temp\Logs\Test" ) )
            + CloudWatchLogger.AppendStream( client, "Test", "Test" );

              var watch = new Stopwatch();

              watch.Restart();

              for ( int i = 0; i < 10000; i++ )
              {

            logger.LogInfo( "Hello World!" );
            logger.LogInfo( "Hello World!" );
            logger.LogWarning( "Multiline\r\nLogs\r\n" );
            logger.LogError( "This has an error!" );
            logger.LogError( "This has an error!" );
            logger.LogError( "This has an error!" );
            try
            {
              throw new Exception( "Test exception!" );
            }
            catch ( Exception e )
            {

              logger.LogException( e );
            }

              }

              watch.Stop();
              Console.WriteLine( watch.Elapsed );

              TextLogFileManager.AutoFlush = false;
              watch.Restart();
              for ( int i = 0; i < 10000; i++ )
              {

            logger.LogInfo( "Hello World!" );
            logger.LogInfo( "Hello World!" );
            logger.LogWarning( "Multiline\r\nLogs\r\n" );
            logger.LogError( "This has an error!" );
            logger.LogError( "This has an error!" );
            logger.LogError( "This has an error!" );
            try
            {
              throw new Exception( "Test exception!" );
            }
            catch ( Exception e )
            {

              logger.LogException( e );
            }

              }
              TextLogFileManager.Flush();
              watch.Stop();
              Console.WriteLine( watch.Elapsed );

              Console.ReadLine();
        }
        private void Connect()
        {
            //If any of the params were not provided to the constructor, try to read them from the config file.
            if (string.IsNullOrEmpty(AccessKey) && Attributes.ContainsKey(CONFIG_ACCESSKEY)) { AccessKey = Attributes[CONFIG_ACCESSKEY]; };
            if (string.IsNullOrEmpty(SecretKey) && Attributes.ContainsKey(CONFIG_SECRETKEY)) { SecretKey = Attributes[CONFIG_SECRETKEY]; };
            if (string.IsNullOrEmpty(Region) && Attributes.ContainsKey(CONFIG_REGION)) { Region = Attributes[CONFIG_REGION]; };
            if (string.IsNullOrEmpty(GroupName) && Attributes.ContainsKey(CONFIG_GROUPNAME)) { GroupName = Attributes[CONFIG_GROUPNAME]; };
            if (string.IsNullOrEmpty(StreamName) && Attributes.ContainsKey(CONFIG_STREAMNAME)) { StreamName = Attributes[CONFIG_STREAMNAME]; };

            try{
                if(string.IsNullOrEmpty(Region) && string.IsNullOrEmpty(AccessKey))
                    //No config provided, use the default configuration
                    _Client = new AmazonCloudWatchLogsClient();
                else if(string.IsNullOrEmpty(Region))
                    //Use explicit access/secret key but default region
                    _Client = new AmazonCloudWatchLogsClient(AccessKey, SecretKey);
                else if(string.IsNullOrEmpty(AccessKey))
                    //Use explicit region and default access/secret key
                    _Client = new AmazonCloudWatchLogsClient(Amazon.RegionEndpoint.GetBySystemName(Region));
                else
                    //All params were supplied explicitly
                    _Client = new AmazonCloudWatchLogsClient(AccessKey, SecretKey, Amazon.RegionEndpoint.GetBySystemName(Region));

                //Let's try to connect to the AWS Cloud with Group and Steam provided
                var response = _Client.DescribeLogStreams(new DescribeLogStreamsRequest()
                {
                    LogGroupName = GroupName,
                    LogStreamNamePrefix = StreamName
                });

                //Get the next token needed to publish
                _Token = response.LogStreams[0].UploadSequenceToken;
            }
            catch
            {
                _ConnectionFailed = true;
                if(FailOnError) throw;
            }

            if (!_ConnectionFailed)
            {
                _Connected = true;
            }
        }