Ejemplo n.º 1
0
        public static IServiceProvider Init(ILambdaContext context)
        {
            var debugLogger = new FunctionLogger(context);

            debugLogger.Log("Init.");

            IServiceCollection services = new ServiceCollection();

            // logging
            debugLogger.Log("Logging. LOAD");
            services.AddScoped(option => new FunctionLogger(context));

            debugLogger.Log("Logging. DONE");

            // Region Code
            var regionCode = Environment.GetEnvironmentVariable("RegionCode");
            var ssmPath    = Environment.GetEnvironmentVariable("SSMVariablesPath");

            debugLogger.Log($"Region Code : {regionCode}");
            debugLogger.Log($"SSM Path : {ssmPath}");

            debugLogger.Log("SSM Configuration. LOAD");

            // SSM Parameters
            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddSystemsManager(ssmPath, new AWSOptions {
                Region = RegionEndpoint.GetBySystemName(regionCode)
            });
            var configurations = configurationBuilder.Build();

            // Aws Credentials
            var awsCredentials =
                new BasicAWSCredentials(
                    accessKey: configurations.GetSection("Aws:Credentials:AccessKey").Value,
                    secretKey: configurations.GetSection("Aws:Credentials:SecretId").Value);

            debugLogger.Log("SSM Configuration. DONE");

            // face rekognition
            debugLogger.Log("Face Recognition. LOAD");

            services.AddScoped(o =>
                               new AmazonRekognitionClient(awsCredentials, RegionEndpoint.GetBySystemName(regionCode)));

            debugLogger.Log("Face Recognition. DONE");

            // Function DI
            services.AddScoped <ImageHandler>();

            debugLogger.Log("Init. DONE.");

            return(services.BuildServiceProvider());
        }
        public void FileLogger_FunctionName_FromScope()
        {
            var trace = new TestTraceWriter(TraceLevel.Info);

            var factoryMock = new Mock <IFunctionTraceWriterFactory>(MockBehavior.Strict);

            factoryMock
            .Setup(f => f.Create("SomeFunction", null))
            .Returns(trace);

            var logger = new FunctionLogger("SomeCategory", factoryMock.Object, (c, l) => true);

            // FunctionName comes from scope
            using (logger.BeginScope(new Dictionary <string, object>
            {
                [ScriptConstants.LoggerFunctionNameKey] = "SomeFunction"
            }))
            {
                logger.Log(LogLevel.Information, 0, new FormattedLogValues("Some Message"), null, (s, e) => s.ToString());
            }

            var traceEvent = trace.Traces.Single();

            Assert.Equal(TraceLevel.Info, traceEvent.Level);
            Assert.Equal("Some Message", traceEvent.Message);
            Assert.Equal("SomeCategory", traceEvent.Source);
        }
        public void FileLogger_NoFunctionName()
        {
            var trace = new TestTraceWriter(TraceLevel.Info);

            // we should never call this
            var factoryMock = new Mock <IFunctionTraceWriterFactory>(MockBehavior.Strict);

            var logger = new FunctionLogger("SomeCategory", factoryMock.Object, (c, l) => true);

            // FunctionName comes from scope -- call with no scope values
            logger.Log(LogLevel.Information, 0, new FormattedLogValues("Some Message"), null, (s, e) => s.ToString());

            Assert.Empty(trace.Traces);
            factoryMock.Verify(f => f.Create(It.IsAny <string>(), null), Times.Never);
        }