Created
January 30, 2024 11:49
-
-
Save hjgraca/f84ff9edf32f7ccc58ef038a0c93043f to your computer and use it in GitHub Desktop.
Powertools Logger ServiceCollection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Function | |
{ | |
private readonly ILogger<Function> _logger; | |
public Function() : this(null) | |
{ | |
} | |
// For testing | |
public Function(ILogger<Function>? logger) | |
{ | |
Startup.ConfigureServices(); | |
_logger = logger ?? Startup.Services.GetRequiredService<ILogger<Function>>(); | |
} | |
[Logging(LogEvent = true, Service = "Log Service")] | |
public string FunctionHandler(string input, ILambdaContext context) | |
{ | |
_logger.LogInformation("Hello world Powertools!"); | |
return input.ToUpper(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using AWS.Lambda.Powertools.Logging; | |
using AWS.Lambda.Powertools.Logging.Internal; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.DependencyInjection.Extensions; | |
using Microsoft.Extensions.Logging; | |
using Microsoft.Extensions.Logging.Configuration; | |
public static class PowertoolsLoggerExtensions | |
{ | |
public static ILoggingBuilder AddPowertoolsLogger( | |
this ILoggingBuilder builder) | |
{ | |
builder.AddConfiguration(); | |
builder.Services.TryAddEnumerable( | |
ServiceDescriptor.Singleton<ILoggerProvider, LoggerProvider>()); | |
LoggerProviderOptions.RegisterProviderOptions | |
<LoggerConfiguration, LoggerProvider>(builder.Services); | |
return builder; | |
} | |
public static ILoggingBuilder AddPowertoolsLogger(this ILoggingBuilder builder, | |
Action<LoggerConfiguration> configure) | |
{ | |
builder.AddPowertoolsLogger(); | |
builder.Services.Configure(configure); | |
return builder; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using AWS.Lambda.Powertools.Logging; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Logging; | |
public static class Startup | |
{ | |
public static void ConfigureServices() | |
{ | |
var serviceCollection = new ServiceCollection(); | |
serviceCollection.AddLogging(builder => | |
{ | |
builder.ClearProviders(); | |
builder.AddPowertoolsLogger(x => | |
{ | |
// Can configure here | |
x.LoggerOutputCase = LoggerOutputCase.SnakeCase; | |
}); | |
}); | |
Services = serviceCollection.BuildServiceProvider(); | |
} | |
public static ServiceProvider Services { get; private set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Amazon.Lambda.TestUtilities; | |
using AWS.Lambda.Powertools.Logging; | |
using AWS.Lambda.Powertools.Logging.Internal; | |
using Microsoft.Extensions.Logging; | |
using Xunit; | |
using Xunit.Abstractions; | |
public class Test | |
{ | |
private readonly ITestOutputHelper _output; | |
public Test(ITestOutputHelper output) | |
{ | |
_output = output; | |
} | |
[Fact] | |
public void TestHandler() | |
{ | |
var factory = _output.BuildLoggerFactory(); | |
factory.AddProvider(new LoggerProvider(new LoggerConfiguration())); | |
var _logger = factory.CreateLogger<Function>(); | |
var func = new Function(_logger); | |
var context = new TestLambdaContext(); | |
func.FunctionHandler("input", context); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment