I have implemented a ServicesCollection DI pattern into my AWS Lambda Function. I want to add Serilog to the ServicesCollection, but I need the ILambdaContext in order to configure the Serilog logger the way that I want.
I currently configure the logger using the stack name. I would like to get the logger from the ServicesCollection.
I currently have a helper method as below.
protected virtual async Task<ILogger> GetLogger([NotNull] ILambdaContext context)
{
if (context == null) throw new ArgumentNullException(nameof(context));
if (_logger == null)
{
var region = context.InvokedFunctionArn.Split(':')[3];
AWSLoggerConfig configuration = new AWSLoggerConfig(await this.GetStackName(context))
{
Region = region
};
_logger = new LoggerConfiguration()
.WriteTo.AWSSeriLog(configuration)
.CreateLogger();
}
return _logger;
}
As you can see it requires the ILambdaContext. I would like to add the ILambdaContext to the ServicesCollection before calling ServiceProvider.GetService().
How do I create a factory class (to add to the services collection) for the Serilog Logger?