예제 #1
0
        public FunctionInstanceLoggerTests()
        {
            var metadataManager = new Mock <IFunctionMetadataManager>(MockBehavior.Strict);

            _metrics = new TestMetricsLogger();
            _functionInstanceLogger = new FunctionInstanceLogger(metadataManager.Object, _metrics);
        }
        protected override void OnInitializeConfig(ScriptHostConfiguration config)
        {
            base.OnInitializeConfig(config);

            // Note: this method can be called many times for the same ScriptHostConfiguration
            // so no changes should be made to the configuration itself. It is safe to modify
            // ScriptHostConfiguration.Host config though, since the inner JobHostConfiguration
            // is created on each restart.

            // Add our WebHost specific services
            var hostConfig = config.HostConfig;

            hostConfig.AddService <IMetricsLogger>(_metricsLogger);
            hostConfig.AddService <IWebHookProvider>(_bindingWebHookProvider);

            // Add our exception handler
            hostConfig.AddService <IWebJobsExceptionHandler>(_exceptionHandler);

            // HostId may be missing in local test scenarios.
            var hostId = hostConfig.HostId ?? "default";
            Func <string, FunctionDescriptor> funcLookup = (name) => this.Instance.GetFunctionOrNull(name);
            var loggingConnectionString = config.HostConfig.DashboardConnectionString;

            // TODO: This is asking for a LoggerFactory before the LoggerFactory is ready. Pass a Null instance for now.
            var instanceLogger = new FunctionInstanceLogger(funcLookup, _metricsLogger, hostId, loggingConnectionString, NullLoggerFactory.Instance);

            hostConfig.AddService <IAsyncCollector <FunctionInstanceLogEntry> >(instanceLogger);

            // disable standard Dashboard logging (enabling Table logging above)
            hostConfig.DashboardConnectionString = null;
        }
        private void EnsureLoggers()
        {
            if (_loggersSet)
            {
                return;
            }

            IFunctionInstanceLogger functionLogger = new FunctionInstanceLogger(_loggerFactory);


            if (_storageAccountOptions.Dashboard != null)
            {
                var dashboardAccount = _storageAccountOptions.GetDashboardStorageAccount();

                // Create logging against a live Azure account.
                var dashboardBlobClient = dashboardAccount.CreateCloudBlobClient();
                IPersistentQueueWriter <PersistentQueueMessage> queueWriter = new PersistentQueueWriter <PersistentQueueMessage>(dashboardBlobClient);
                PersistentQueueLogger queueLogger = new PersistentQueueLogger(queueWriter);
                _hostInstanceLogger     = queueLogger;
                _functionInstanceLogger = new CompositeFunctionInstanceLogger(queueLogger, functionLogger);
                _functionOutputLogger   = new BlobFunctionOutputLogger(dashboardBlobClient);
            }
            else
            {
                // No auxillary logging. Logging interfaces are nops or in-memory.
                _hostInstanceLogger     = new NullHostInstanceLogger();
                _functionInstanceLogger = functionLogger;
                _functionOutputLogger   = new NullFunctionOutputLogger();
            }

            _loggersSet = true;
        }
        protected override void OnInitializeConfig(ScriptHostConfiguration config)
        {
            base.OnInitializeConfig(config);

            // Note: this method can be called many times for the same ScriptHostConfiguration
            // so no changes should be made to the configuration itself. It is safe to modify
            // ScriptHostConfiguration.Host config though, since the inner JobHostConfiguration
            // is created anew on each restart.

            // Add our WebHost specific services
            var hostConfig = config.HostConfig;

            hostConfig.AddService <IMetricsLogger>(_metricsLogger);

            config.HostConfig.AddService <IWebHookProvider>(this._bindingWebHookProvider);

            // Add our exception handler
            hostConfig.AddService <IWebJobsExceptionHandler>(_exceptionHandler);

            // Register the new "FastLogger" for Dashboard support
            var dashboardString = AmbientConnectionStringProvider.Instance.GetConnectionString(ConnectionStringNames.Dashboard);

            // hostId may be missing in local test scenarios.
            var hostId = config.HostConfig.HostId ?? "default";
            Func <string, FunctionDescriptor> funcLookup = (name) => this.Instance.GetFunctionOrNull(name);
            var instanceLogger = new FunctionInstanceLogger(funcLookup, _metricsLogger, hostId, dashboardString, config.TraceWriter);

            hostConfig.AddService <IAsyncCollector <FunctionInstanceLogEntry> >(instanceLogger);

            hostConfig.DashboardConnectionString = null; // disable slow logging
        }
예제 #5
0
        public FunctionInstanceLoggerTests()
        {
            ILoggerFactory loggerFactory = new LoggerFactory();

            loggerFactory.AddProvider(_provider);

            _instanceLogger = new FunctionInstanceLogger(loggerFactory);
        }
예제 #6
0
        protected ScriptHostEndToEndTestFixture(string rootPath, string testId, ProxyClientExecutor proxyClient = null, bool startHost = true, ICollection <string> functions = null, string functionsWorkerLanguage = null)
        {
            if (!string.IsNullOrEmpty(functionsWorkerLanguage))
            {
                Environment.SetEnvironmentVariable(LanguageWorkerConstants.FunctionWorkerRuntimeSettingName, functionsWorkerLanguage);
            }
            _settingsManager = ScriptSettingsManager.Instance;
            FixtureId        = testId;
            string connectionString            = AmbientConnectionStringProvider.Instance.GetConnectionString(ConnectionStringNames.Storage);
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

            QueueClient = storageAccount.CreateCloudQueueClient();
            BlobClient  = storageAccount.CreateCloudBlobClient();
            TableClient = storageAccount.CreateCloudTableClient();

            CreateTestStorageEntities().Wait();

            // ApiHubTestHelper.SetDefaultConnectionFactory();

            ScriptHostConfiguration config = new ScriptHostConfiguration()
            {
                RootScriptPath  = rootPath,
                FileLoggingMode = FileLoggingMode.Always,
            };

            if (functions != null)
            {
                config.OnConfigurationApplied = c => c.Functions = functions;
            }

            RequestConfiguration = new HttpConfiguration();

            EventManager = new ScriptEventManager();
            ScriptHostEnvironmentMock = new Mock <IScriptHostEnvironment>();
            LoggerProvider            = new TestLoggerProvider();
            ILoggerProviderFactory loggerProviderFactory = new TestLoggerProviderFactory(LoggerProvider);

            // Reset the timer logs first, since one of the tests will
            // be checking them
            TestHelpers.ClearFunctionLogs("TimerTrigger");
            TestHelpers.ClearFunctionLogs("ListenerStartupException");

            InitializeConfig(config);
            Func <string, FunctionDescriptor> funcLookup = (name) => this.Host.GetFunctionOrNull(name);
            var fastLogger = new FunctionInstanceLogger(funcLookup, new MetricsLogger());

            config.HostConfig.AddService <IAsyncCollector <FunctionInstanceLogEntry> >(fastLogger);
            Host = new ScriptHost(ScriptHostEnvironmentMock.Object, EventManager, config, _settingsManager,
                                  proxyClient: proxyClient, loggerProviderFactory: loggerProviderFactory);
            Host.Initialize();

            if (startHost)
            {
                Host.HostStarted += (s, e) => _hostStartedEvent.Set();
                Host.Start();
                _hostStartedEvent.Wait(TimeSpan.FromSeconds(30));
            }
        }
            public MockInvoker(ScriptHost host, IMetricsLogger metrics, FunctionMetadata metadata, ILoggerFactory loggerFactory)
                : base(host, metadata, loggerFactory)
            {
                var metadataManagerMock = new Mock <IFunctionMetadataManager>();

                metadataManagerMock.Setup(m => m.Functions)
                .Returns(new[] { metadata }.ToImmutableArray());
                var proxyMetadataManagerMock = new Mock <IProxyMetadataManager>();

                _fastLogger = new FunctionInstanceLogger(metadataManagerMock.Object, proxyMetadataManagerMock.Object, metrics);
            }
예제 #8
0
        public void OnException_TracesException()
        {
            var trace = new TestTraceWriter(TraceLevel.Verbose);
            var ex    = new InvalidOperationException("Boom!");

            FunctionInstanceLogger.OnException(ex, trace);

            TraceEvent traceEvent = trace.Traces.Single();

            Assert.StartsWith("Error writing logs to table storage: System.InvalidOperationException: Boom!", traceEvent.Message);
            Assert.Equal(TraceLevel.Error, traceEvent.Level);
            Assert.Same(ex, traceEvent.Exception);
        }
        protected EndToEndTestFixture(string rootPath, string testId)
        {
            _settingsManager = ScriptSettingsManager.Instance;
            FixtureId        = testId;
            string connectionString            = AmbientConnectionStringProvider.Instance.GetConnectionString(ConnectionStringNames.Storage);
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

            QueueClient = storageAccount.CreateCloudQueueClient();
            BlobClient  = storageAccount.CreateCloudBlobClient();
            TableClient = storageAccount.CreateCloudTableClient();

            CreateTestStorageEntities();
            TraceWriter = new TestTraceWriter(TraceLevel.Verbose);

            ApiHubTestHelper.SetDefaultConnectionFactory();

            ScriptHostConfiguration config = new ScriptHostConfiguration()
            {
                RootScriptPath  = rootPath,
                TraceWriter     = TraceWriter,
                FileLoggingMode = FileLoggingMode.Always
            };

            RequestConfiguration = new HttpConfiguration();
            RequestConfiguration.Formatters.Add(new PlaintextMediaTypeFormatter());

            EventManager = new ScriptEventManager();
            ScriptHostEnvironmentMock = new Mock <IScriptHostEnvironment>();

            // Reset the timer logs first, since one of the tests will
            // be checking them
            TestHelpers.ClearFunctionLogs("TimerTrigger");
            TestHelpers.ClearFunctionLogs("ListenerStartupException");

            InitializeConfig(config);
            Func <string, FunctionDescriptor> funcLookup = (name) => this.Host.GetFunctionOrNull(name);
            var fastLogger = new FunctionInstanceLogger(funcLookup, new MetricsLogger());

            config.HostConfig.AddService <IAsyncCollector <FunctionInstanceLogEntry> >(fastLogger);

            Host = ScriptHost.Create(ScriptHostEnvironmentMock.Object, EventManager, config, _settingsManager);
            Host.Start();
        }
예제 #10
0
 public MockInvoker(ScriptHost host, IMetricsLogger metrics, FunctionMetadata metadata) : base(host, metadata)
 {
     _fastLogger = new FunctionInstanceLogger(
         (name) => this.Host.GetFunctionOrNull(name),
         metrics);
 }
 public MockInvoker(ScriptHost host, IMetricsLogger metrics, IFunctionMetadataManager functionMetadataManager, FunctionMetadata metadata, ILoggerFactory loggerFactory)
     : base(host, metadata, loggerFactory)
 {
     _instanceLogger = new FunctionInstanceLogger(functionMetadataManager, metrics);
 }