protected CloudWatchLogger( AmazonCloudWatchLogsClient client, string groupName, string streamName, string sequenceToken = null ) { Client = client; GroupName = groupName; StreamName = streamName; SequenceToken = sequenceToken; }
/// <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 ); }
/// <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(); } }
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; } }