Skip to content

Instantly share code, notes, and snippets.

@hjgraca
Created January 30, 2024 11:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hjgraca/f84ff9edf32f7ccc58ef038a0c93043f to your computer and use it in GitHub Desktop.
Save hjgraca/f84ff9edf32f7ccc58ef038a0c93043f to your computer and use it in GitHub Desktop.
Powertools Logger ServiceCollection
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();
}
}
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;
}
}
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; }
}
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