|
| 1 | +// This file is referenced by docs/core/logging.md |
| 2 | +// via pymdownx.snippets (mkdocs). |
| 3 | + |
| 4 | +namespace AWS.Lambda.Powertools.Docs.Snippets.Logging; |
| 5 | + |
| 6 | +// --8<-- [start:json_serializer_options_aot] |
| 7 | +Logger.Configure(logger => |
| 8 | +{ |
| 9 | + logger.JsonOptions = new JsonSerializerOptions |
| 10 | + { |
| 11 | + TypeInfoResolver = YourJsonSerializerContext.Default |
| 12 | + }; |
| 13 | +}); |
| 14 | +// --8<-- [end:json_serializer_options_aot] |
| 15 | + |
| 16 | +// --8<-- [start:before_aot] |
| 17 | + Func<APIGatewayHttpApiV2ProxyRequest, ILambdaContext, Task<APIGatewayHttpApiV2ProxyResponse>> handler = FunctionHandler; |
| 18 | + await LambdaBootstrapBuilder.Create(handler, new SourceGeneratorLambdaJsonSerializer<MyCustomJsonSerializerContext>()) |
| 19 | + .Build() |
| 20 | + .RunAsync(); |
| 21 | +// --8<-- [end:before_aot] |
| 22 | + |
| 23 | +// --8<-- [start:after_aot] |
| 24 | +Func<APIGatewayHttpApiV2ProxyRequest, ILambdaContext, Task<APIGatewayHttpApiV2ProxyResponse>> handler = FunctionHandler; |
| 25 | +await LambdaBootstrapBuilder.Create(handler, new PowertoolsSourceGeneratorSerializer<MyCustomJsonSerializerContext>()) |
| 26 | + .Build() |
| 27 | + .RunAsync(); |
| 28 | +// --8<-- [end:after_aot] |
| 29 | + |
| 30 | +// --8<-- [start:demo_class] |
| 31 | +public class Demo |
| 32 | +{ |
| 33 | + public string Name { get; set; } |
| 34 | + public Headers Headers { get; set; } |
| 35 | +} |
| 36 | +// --8<-- [end:demo_class] |
| 37 | + |
| 38 | +// --8<-- [start:json_serializer_context] |
| 39 | +[JsonSerializable(typeof(APIGatewayHttpApiV2ProxyRequest))] |
| 40 | +[JsonSerializable(typeof(APIGatewayHttpApiV2ProxyResponse))] |
| 41 | +[JsonSerializable(typeof(Demo))] |
| 42 | +public partial class MyCustomJsonSerializerContext : JsonSerializerContext |
| 43 | +{ |
| 44 | +} |
| 45 | +// --8<-- [end:json_serializer_context] |
| 46 | + |
| 47 | +// --8<-- [start:custom_log_formatter_aot_function] |
| 48 | + |
| 49 | +Func<APIGatewayHttpApiV2ProxyRequest, ILambdaContext, Task<APIGatewayHttpApiV2ProxyResponse>> handler = FunctionHandler; |
| 50 | +await LambdaBootstrapBuilder.Create(handler, |
| 51 | + new PowertoolsSourceGeneratorSerializer<LambdaFunctionJsonSerializerContext> |
| 52 | + ( |
| 53 | + new CustomLogFormatter() |
| 54 | + ) |
| 55 | +) |
| 56 | +.Build() |
| 57 | +.RunAsync(); |
| 58 | + |
| 59 | +// --8<-- [end:custom_log_formatter_aot_function] |
| 60 | + |
| 61 | +// --8<-- [start:custom_log_formatter_aot_class] |
| 62 | +public class CustomLogFormatter : ILogFormatter |
| 63 | +{ |
| 64 | + public object FormatLogEntry(LogEntry logEntry) |
| 65 | + { |
| 66 | + return new |
| 67 | + { |
| 68 | + Message = logEntry.Message, |
| 69 | + Service = logEntry.Service, |
| 70 | + CorrelationIds = new |
| 71 | + { |
| 72 | + AwsRequestId = logEntry.LambdaContext?.AwsRequestId, |
| 73 | + XRayTraceId = logEntry.XRayTraceId, |
| 74 | + CorrelationId = logEntry.CorrelationId |
| 75 | + }, |
| 76 | + LambdaFunction = new |
| 77 | + { |
| 78 | + Name = logEntry.LambdaContext?.FunctionName, |
| 79 | + Arn = logEntry.LambdaContext?.InvokedFunctionArn, |
| 80 | + MemoryLimitInMB = logEntry.LambdaContext?.MemoryLimitInMB, |
| 81 | + Version = logEntry.LambdaContext?.FunctionVersion, |
| 82 | + ColdStart = logEntry.ColdStart, |
| 83 | + }, |
| 84 | + Level = logEntry.Level.ToString(), |
| 85 | + Timestamp = logEntry.Timestamp.ToString("o"), |
| 86 | + Logger = new |
| 87 | + { |
| 88 | + Name = logEntry.Name, |
| 89 | + SampleRate = logEntry.SamplingRate |
| 90 | + }, |
| 91 | + }; |
| 92 | + } |
| 93 | +} |
| 94 | +// --8<-- [end:custom_log_formatter_aot_class] |
0 commit comments