-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy pathCloudWatchLogsEvents.cs
60 lines (53 loc) · 2 KB
/
CloudWatchLogsEvents.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
namespace Amazon.Lambda.CloudWatchLogsEvents
{
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization;
using System.Text;
/// <summary>
/// AWS CloudWatch Logs event
/// http://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/Subscriptions.html
/// http://docs.aws.amazon.com/lambda/latest/dg/eventsources.html#eventsources-cloudwatch-logs
/// </summary>
public class CloudWatchLogsEvent
{
/// <summary>
/// The Log from the CloudWatch that is invoking the Lambda function.
/// </summary>
public Log Awslogs { get; set; }
/// <summary>
/// The class identifies the Log from the CloudWatch that is invoking the Lambda function.
/// </summary>
[DataContract]
public class Log
{
/// <summary>
/// The data that are base64 encoded and gziped messages in LogStreams.
/// </summary>
[DataMember(Name = "data", IsRequired = false)]
#if NETCOREAPP3_1_OR_GREATER
[System.Text.Json.Serialization.JsonPropertyName("data")]
#endif
public string EncodedData { get; set; }
/// <summary>
/// Decodes the data stored in the EncodedData property.
/// </summary>
public string DecodeData()
{
if (string.IsNullOrEmpty(this.EncodedData))
return this.EncodedData;
var bytes = Convert.FromBase64String(this.EncodedData);
var uncompressedStream = new MemoryStream();
using (var stream = new GZipStream(new MemoryStream(bytes), CompressionMode.Decompress))
{
stream.CopyTo(uncompressedStream);
uncompressedStream.Position = 0;
}
var decodedString = Encoding.UTF8.GetString(uncompressedStream.ToArray());
return decodedString;
}
}
}
}