-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathAWSAppender.cs
278 lines (248 loc) · 11.2 KB
/
AWSAppender.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using System;
using log4net.Appender;
using log4net.Core;
using Amazon.Runtime;
using AWS.Logger.Core;
namespace AWS.Logger.Log4net
{
/// <summary>
/// A Log4net appender that sends logging messages to AWS.
/// </summary>
public class AWSAppender : AppenderSkeleton, IAWSLoggerConfig
{
private readonly AWSLoggerConfig _config = new AWSLoggerConfig();
AWSLoggerCore _core = null;
/// <summary>
/// Default Constructor
/// </summary>
public AWSAppender()
{
}
/// <summary>
/// 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.
/// </summary>
public string LogGroup
{
get { return _config.LogGroup; }
set { _config.LogGroup = value; }
}
/// <summary>
/// Determines whether or not to create a new Log Group, if the one specified by <see cref="LogGroup"/> doesn't already exist
/// <seealso cref="AWSLoggerConfig.DisableLogGroupCreation"/>
/// </summary>
public bool DisableLogGroupCreation
{
get { return _config.DisableLogGroupCreation; }
set { _config.DisableLogGroupCreation = value; }
}
/// <summary>
/// Specifies the days to retain events in log groups which are created by this logger, if the one specified by <see cref="LogGroup"/> doesn't already exist
/// and <see cref="DisableLogGroupCreation"/> is not <c>true</c>. Requires logs:PutRetentionPolicy permission to apply the retention policy to
/// newly created log groups. The default value of <c>null</c> will not apply a retention policy to new log groups.
/// <para>
/// Note that log groups which already exist will not have this retention policy applied for startup performance reasons.
/// </para>
/// <para>
/// Possible values could be found in the CloudWatchLogs API reference at https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutRetentionPolicy.html#API_PutRetentionPolicy_RequestSyntax
/// </para>
/// </summary>
/// <remarks>
/// Note that invalid retention policy values will result in the policy not being applied, however this error is non-fatal and the application and will continue without the policy.
/// </remarks>
public int? NewLogGroupRetentionInDays
{
get { return _config.NewLogGroupRetentionInDays; }
set { _config.NewLogGroupRetentionInDays = value; }
}
/// <summary>
/// Gets and sets the Profile property. The profile is used to look up AWS credentials in the profile store.
/// <para>
/// For understanding how credentials are determine view the top level documentation for AWSLoggerConfig class.
/// </para>
/// </summary>
public string Profile
{
get { return _config.Profile; }
set { _config.Profile = value; }
}
/// <summary>
/// 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.
/// <para>
/// For understanding how credentials are determine view the top level documentation for AWSLoggerConfig class.
/// </para>
/// </summary>
public string ProfilesLocation
{
get { return _config.ProfilesLocation; }
set { _config.ProfilesLocation = value; }
}
/// <summary>
/// Gets and sets the Credentials property. These are the AWS credentials used by the AWS SDK for .NET to make service calls.
/// <para>
/// For understanding how credentials are determine view the top level documentation for AWSLoggerConfig class.
/// </para>
/// </summary>
public AWSCredentials Credentials
{
get { return _config.Credentials; }
set { _config.Credentials = value; }
}
/// <summary>
/// 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.
/// </summary>
public string Region
{
get { return _config.Region; }
set { _config.Region = value; }
}
/// <summary>
/// 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
/// </summary>
public string ServiceUrl
{
get { return _config.ServiceUrl; }
set { _config.ServiceUrl = value; }
}
/// <summary>
/// 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.
/// <para>
/// The default is 3 seconds.
/// </para>
/// </summary>
public TimeSpan BatchPushInterval
{
get { return _config.BatchPushInterval; }
set { _config.BatchPushInterval = value; }
}
/// <summary>
/// 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.
/// <para>
/// The default is 100 Kilobytes.
/// </para>
/// </summary>
public int BatchSizeInBytes
{
get { return _config.BatchSizeInBytes; }
set { _config.BatchSizeInBytes = value; }
}
/// <summary>
/// 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.
/// <para>
/// The default is 10000.
/// </para>
/// </summary>
public int MaxQueuedMessages
{
get { return _config.MaxQueuedMessages; }
set { _config.MaxQueuedMessages = value; }
}
/// <summary>
/// Gets and sets the LogStreamName property. When this is set the full name of the log stream will be equal to this value,
/// as opposed to the computed value using <see cref="LogStreamNamePrefix"/> and <see cref="LogStreamNameSuffix"/>, which will be ignored.
/// </summary>
/// <para>
/// The default is an empty string.
/// </para>
public string LogStreamName
{
get { return _config.LogStreamName; }
set { _config.LogStreamName = value; }
}
/// <summary>
/// Gets and sets the LogStreamNameSuffix property. The LogStreamName consists of an optional user-defined prefix segment, then a DateTimeStamp as the
/// system-defined prefix segment, and a user defined suffix value that can be set using the LogStreamNameSuffix property defined here.
/// <para>
/// The default is going to a Guid.
/// </para>
/// </summary>
public string LogStreamNameSuffix
{
get { return _config.LogStreamNameSuffix; }
set { _config.LogStreamNameSuffix = value; }
}
/// <summary>
/// Gets and sets the LogStreamNamePrefix property. The LogStreamName consists of an optional user-defined prefix segment (defined here), then a
/// DateTimeStamp as the system-defined prefix segment, and a user defined suffix value that can be set using the LogStreamNameSuffix property.
/// <para>
/// The default will use an empty string for this user-defined portion, meaning the log stream name will start with the system-defined portion of the prefix (yyyy/MM/dd ... )
/// </para>
/// </summary>
public string LogStreamNamePrefix
{
get { return _config.LogStreamNamePrefix; }
set { _config.LogStreamNamePrefix = value; }
}
/// <summary>
/// Gets and sets the LibraryLogErrors property. This is the boolean value of whether or not you would like this library to log logging errors.
/// <para>
/// The default is "true".
/// </para>
/// </summary>
public bool LibraryLogErrors
{
get { return _config.LibraryLogErrors; }
set { _config.LibraryLogErrors = value; }
}
/// <summary>
/// Gets and sets 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.
/// <para>
/// The default is 30000 milliseconds.
/// </para>
/// </summary>
public TimeSpan FlushTimeout
{
get { return _config.FlushTimeout; }
set { _config.FlushTimeout = value; }
}
/// <summary>
/// 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.
/// <para>
/// The default is going to "aws-logger-errors.txt".
/// </para>
/// </summary>
public string LibraryLogFileName
{
get { return _config.LibraryLogFileName; }
set { _config.LibraryLogFileName = value; }
}
/// <summary>
/// Initialize the appender based on the options set.
/// </summary>
public override void ActivateOptions()
{
if (_core != null)
{
_core.Close();
_core = null;
}
_core = new AWSLoggerCore(_config, "Log4net");
}
/// <summary>
/// Append method of AppenderSkeleton is called when a new message gets logged.
/// </summary>
/// <param name="loggingEvent">
/// LoggingEvent containing information about the log message.
/// </param>
protected override void Append(LoggingEvent loggingEvent)
{
if (_core == null)
return;
_core.AddMessage(RenderLoggingEvent(loggingEvent));
}
/// <inheritdoc />
public override bool Flush(int millisecondsTimeout)
{
_core?.Flush();
return base.Flush(millisecondsTimeout);
}
}
}