-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathCustomLogFormatter.cs
More file actions
65 lines (61 loc) · 1.92 KB
/
Copy pathCustomLogFormatter.cs
File metadata and controls
65 lines (61 loc) · 1.92 KB
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
// This file is referenced by docs/core/logging.md
// via pymdownx.snippets (mkdocs).
namespace AWS.Lambda.Powertools.Docs.Snippets.Logging;
// --8<-- [start:custom_log_formatter_function]
/**
* Handler for requests to Lambda function.
*/
public class Function
{
/// <summary>
/// Function constructor
/// </summary>
public Function()
{
Logger.Configure(options =>
{
options.LogFormatter = new CustomLogFormatter();
});
}
[Logging(CorrelationIdPath = "/headers/my_request_id_header", SamplingRate = 0.7)]
public async Task<APIGatewayProxyResponse> FunctionHandler
(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
{
...
}
}
// --8<-- [end:custom_log_formatter_function]
// --8<-- [start:custom_log_formatter_class]
public class CustomLogFormatter : ILogFormatter
{
public object FormatLogEntry(LogEntry logEntry)
{
return new
{
Message = logEntry.Message,
Service = logEntry.Service,
CorrelationIds = new
{
AwsRequestId = logEntry.LambdaContext?.AwsRequestId,
XRayTraceId = logEntry.XRayTraceId,
CorrelationId = logEntry.CorrelationId
},
LambdaFunction = new
{
Name = logEntry.LambdaContext?.FunctionName,
Arn = logEntry.LambdaContext?.InvokedFunctionArn,
MemoryLimitInMB = logEntry.LambdaContext?.MemoryLimitInMB,
Version = logEntry.LambdaContext?.FunctionVersion,
ColdStart = logEntry.ColdStart,
},
Level = logEntry.Level.ToString(),
Timestamp = logEntry.Timestamp.ToString("o"),
Logger = new
{
Name = logEntry.Name,
SampleRate = logEntry.SamplingRate
},
};
}
}
// --8<-- [end:custom_log_formatter_class]