예제 #1
0
        public DiagnosticsController(IEnvironment environment, ITracer tracer, IApplicationLogsReader applicationLogsReader)
        {
            // Setup the diagnostics service to collect information from the following paths:
            // 1. The deployments folder
            // 2. The profile dump
            // 3. The npm log
            _paths = new[] {
                environment.DeploymentsPath,
                Path.Combine(environment.RootPath, Constants.LogFilesPath),
                Path.Combine(environment.WebRootPath, Constants.NpmDebugLogFile),
            };

            _applicationLogsReader = applicationLogsReader;
            _tracer = tracer;
            _settingsManager = new DiagnosticsSettingsManager(Path.Combine(environment.DiagnosticsPath, Constants.SettingsJsonFile), _tracer);
        }
        public void BasicReadWriteTests()
        {
            BuildFileSystemMock((Mock<IFileSystem> fileSystemMock) =>
            {
                FileSystemHelpers.Instance = fileSystemMock.Object;
                var mockTracer = new Mock<ITracer>();

                var manager = new DiagnosticsSettingsManager(@"x:\test.json", mockTracer.Object);

                // should see default value
                DiagnosticsSettings settings = manager.GetSettings();
                Assert.Equal(false, settings.AzureDriveEnabled);
                Assert.Equal(TraceEventType.Error, settings.AzureDriveTraceLevel);
                Assert.Equal(false, settings.AzureTableEnabled);
                Assert.Equal(TraceEventType.Error, settings.AzureTableTraceLevel);
                Assert.Equal(false, settings.AzureBlobEnabled);
                Assert.Equal(TraceEventType.Error, settings.AzureBlobTraceLevel);

                // update with good value
                manager.UpdateSetting(DiagnosticsSettings.AzureDriveEnabledKey, true);
                manager.UpdateSetting(DiagnosticsSettings.AzureDriveTraceLevelKey, TraceEventType.Verbose);
                manager.UpdateSetting(DiagnosticsSettings.AzureTableEnabledKey, true);
                manager.UpdateSetting(DiagnosticsSettings.AzureTableTraceLevelKey, TraceEventType.Warning);
                manager.UpdateSetting(DiagnosticsSettings.AzureBlobEnabledKey, true);
                manager.UpdateSetting(DiagnosticsSettings.AzureBlobTraceLevelKey, TraceEventType.Information);

                settings = manager.GetSettings();
                Assert.Equal(true, settings.AzureDriveEnabled);
                Assert.Equal(TraceEventType.Verbose, settings.AzureDriveTraceLevel);
                Assert.Equal(true, settings.AzureTableEnabled);
                Assert.Equal(TraceEventType.Warning, settings.AzureTableTraceLevel);
                Assert.Equal(true, settings.AzureBlobEnabled);
                Assert.Equal(TraceEventType.Information, settings.AzureBlobTraceLevel);

                // number and string should be good too
                manager.UpdateSetting(DiagnosticsSettings.AzureDriveTraceLevelKey, 2);
                settings = manager.GetSettings();
                Assert.Equal(TraceEventType.Error, settings.AzureDriveTraceLevel);

                JsonSerializationException exception = null;

                try
                {
                    manager.UpdateSetting(DiagnosticsSettings.AzureTableTraceLevelKey, "Error");
                }
                catch (JsonSerializationException ex)
                {
                    exception = ex;
                }

                Assert.NotNull(exception);
                Assert.Equal("Error getting value from 'AzureTableTraceLevel' on 'Kudu.Contracts.Settings.DiagnosticsSettings'.", exception.Message);

                try
                {
                    manager.UpdateSetting(DiagnosticsSettings.AzureTableTraceLevelKey, "8");
                }
                catch (JsonSerializationException ex)
                {
                    exception = ex;
                }

                Assert.Equal("Error getting value from 'AzureTableTraceLevel' on 'Kudu.Contracts.Settings.DiagnosticsSettings'.", exception.Message);

                try
                {
                    manager.UpdateSetting(DiagnosticsSettings.AzureTableTraceLevelKey, "foo");
                }
                catch (JsonSerializationException ex)
                {
                    exception = ex;
                }

                Assert.Equal("Error getting value from 'AzureTableTraceLevel' on 'Kudu.Contracts.Settings.DiagnosticsSettings'.", exception.Message);

                try
                {
                    manager.UpdateSetting(DiagnosticsSettings.AzureBlobTraceLevelKey, 999);
                }
                catch (JsonSerializationException ex)
                {
                    exception = ex;
                }

                Assert.Equal(string.Format(CultureInfo.InvariantCulture, "Error converting value '{0}'", 999), exception.Message);

                settings = manager.GetSettings();
                Assert.Equal(true, settings.AzureDriveEnabled);
                Assert.Equal(TraceEventType.Error, settings.AzureDriveTraceLevel);
                Assert.Equal(true, settings.AzureTableEnabled);
                Assert.Equal(TraceEventType.Warning, settings.AzureTableTraceLevel);
                Assert.Equal(true, settings.AzureBlobEnabled);
                Assert.Equal(TraceEventType.Information, settings.AzureBlobTraceLevel);

                // with corrupted content, kudu will auto-correct content and reset to default value
                manager = new DiagnosticsSettingsManager(@"x:\bad-content.json", mockTracer.Object);
                settings = manager.GetSettings();
                Assert.Equal(false, settings.AzureDriveEnabled);
                Assert.Equal(TraceEventType.Error, settings.AzureDriveTraceLevel);
                Assert.Equal(false, settings.AzureTableEnabled);
                Assert.Equal(TraceEventType.Error, settings.AzureTableTraceLevel);
                Assert.Equal(false, settings.AzureBlobEnabled);
                Assert.Equal(TraceEventType.Error, settings.AzureBlobTraceLevel);
            });
        }