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);
        }
Example #2
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());
        }
Example #3
0
        public async Task AddAsync(FunctionInstanceLogEntry item, CancellationToken cancellationToken = default(CancellationToken))
        {
            FunctionInstanceMonitor state;

            item.Properties.TryGetValue(Key, out state);

            if (item.EndTime.HasValue)
            {
                // Completed
                bool success = item.ErrorDetails == null;
                state.End(success);
            }
            else
            {
                // Started
                if (state == null)
                {
                    string shortName = Utility.GetFunctionShortName(item.FunctionName);

                    FunctionDescriptor descr = _funcLookup(shortName);
                    if (descr == null)
                    {
                        // This exception will cause the function to not get executed.
                        throw new InvalidOperationException($"Missing function.json for '{shortName}'.");
                    }
                    FunctionLogger logInfo = descr.Invoker.LogInfo;
                    state = new FunctionInstanceMonitor(descr.Metadata, _metrics, item.FunctionInstanceId, logInfo);

                    item.Properties[Key] = state;

                    state.Start();
                }
            }

            if (_writer != null)
            {
                await _writer.AddAsync(new FunctionInstanceLogItem
                {
                    FunctionInstanceId = item.FunctionInstanceId,
                    FunctionName       = Utility.GetFunctionShortName(item.FunctionName),
                    StartTime          = item.StartTime,
                    EndTime            = item.EndTime,
                    TriggerReason      = item.TriggerReason,
                    Arguments          = item.Arguments,
                    ErrorDetails       = item.ErrorDetails,
                    LogOutput          = item.LogOutput,
                    ParentId           = item.ParentId
                });
            }
        }
        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);
        }