Esempio n. 1
0
        /// <summary>
        /// Adds an Azure Web Apps diagnostics logger.
        /// </summary>
        /// <param name="builder">The extension method argument</param>
        public static ILoggingBuilder AddAzureWebAppDiagnostics(this ILoggingBuilder builder)
        {
            var context = WebAppContext.Default;

            if (!context.IsRunningInAzureWebApp)
            {
                return(builder);
            }

            var config   = SiteConfigurationProvider.GetAzureLoggingConfiguration(context);
            var services = builder.Services;

            services.TryAddEnumerable(Singleton <IConfigureOptions <LoggerFilterOptions> >(CreateFileFilterConfigureOptions(config)));
            services.TryAddEnumerable(Singleton <IConfigureOptions <LoggerFilterOptions> >(CreateBlobFilterConfigureOptions(config)));

            services.TryAddEnumerable(Singleton <IOptionsChangeTokenSource <LoggerFilterOptions> >(
                                          new ConfigurationChangeTokenSource <LoggerFilterOptions>(config)));

            services.TryAddEnumerable(Singleton <IConfigureOptions <AzureBlobLoggerOptions> >(
                                          new BlobLoggerConfigureOptions(config, context)));
            services.TryAddEnumerable(Singleton <IOptionsChangeTokenSource <AzureBlobLoggerOptions> >(
                                          new ConfigurationChangeTokenSource <AzureBlobLoggerOptions>(config)));

            services.TryAddEnumerable(Singleton <IConfigureOptions <AzureFileLoggerOptions> >(new FileLoggerConfigureOptions(config, context)));
            services.TryAddEnumerable(Singleton <IOptionsChangeTokenSource <AzureFileLoggerOptions> >(
                                          new ConfigurationChangeTokenSource <AzureFileLoggerOptions>(config)));

            services.TryAddEnumerable(Singleton <IWebAppContext>(context));

            // Only add the provider if we're in Azure WebApp. That cannot change once the apps started
            services.TryAddEnumerable(Singleton <ILoggerProvider, FileLoggerProvider>());
            services.TryAddEnumerable(Singleton <ILoggerProvider, BlobLoggerProvider>());

            return(builder);
        }
        public void NoConfigFile()
        {
            var tempFolder = Path.Combine(Path.GetTempPath(), "AzureWebAppLoggerThisFolderShouldNotExist");

            var contextMock = new Mock <IWebAppContext>();

            contextMock.SetupGet(c => c.HomeFolder)
            .Returns(tempFolder);

            var config = SiteConfigurationProvider.GetAzureLoggingConfiguration(contextMock.Object);

            Assert.NotNull(config);
        }
Esempio n. 3
0
        internal static ILoggingBuilder AddAzureWebAppDiagnostics(this ILoggingBuilder builder, IWebAppContext context)
        {
            if (!context.IsRunningInAzureWebApp)
            {
                return(builder);
            }

            builder.AddConfiguration();

            var config   = SiteConfigurationProvider.GetAzureLoggingConfiguration(context);
            var services = builder.Services;

            var addedFileLogger = TryAddEnumerable(services, Singleton <ILoggerProvider, FileLoggerProvider>());
            var addedBlobLogger = TryAddEnumerable(services, Singleton <ILoggerProvider, BlobLoggerProvider>());

            if (addedFileLogger || addedBlobLogger)
            {
                services.AddSingleton(context);
                services.AddSingleton <IOptionsChangeTokenSource <LoggerFilterOptions> >(
                    new ConfigurationChangeTokenSource <LoggerFilterOptions>(config));
            }

            if (addedFileLogger)
            {
                services.AddSingleton <IConfigureOptions <LoggerFilterOptions> >(CreateFileFilterConfigureOptions(config));
                services.AddSingleton <IConfigureOptions <AzureFileLoggerOptions> >(new FileLoggerConfigureOptions(config, context));
                services.AddSingleton <IOptionsChangeTokenSource <AzureFileLoggerOptions> >(
                    new ConfigurationChangeTokenSource <AzureFileLoggerOptions>(config));
                LoggerProviderOptions.RegisterProviderOptions <AzureFileLoggerOptions, FileLoggerProvider>(builder.Services);
            }

            if (addedBlobLogger)
            {
                services.AddSingleton <IConfigureOptions <LoggerFilterOptions> >(CreateBlobFilterConfigureOptions(config));
                services.AddSingleton <IConfigureOptions <AzureBlobLoggerOptions> >(new BlobLoggerConfigureOptions(config, context));
                services.AddSingleton <IOptionsChangeTokenSource <AzureBlobLoggerOptions> >(
                    new ConfigurationChangeTokenSource <AzureBlobLoggerOptions>(config));
                LoggerProviderOptions.RegisterProviderOptions <AzureBlobLoggerOptions, BlobLoggerProvider>(builder.Services);
            }

            return(builder);
        }
        public void ReadsSettingsFileAndEnvironment()
        {
            var tempFolder = Path.Combine(Path.GetTempPath(), "WebAppLoggerConfigurationDisabledInSettingsFile");

            try
            {
                var settingsFolder = Path.Combine(tempFolder, "site", "diagnostics");
                var settingsFile   = Path.Combine(settingsFolder, "settings.json");

                if (!Directory.Exists(settingsFolder))
                {
                    Directory.CreateDirectory(settingsFolder);
                }
                Environment.SetEnvironmentVariable("RANDOM_ENVIRONMENT_VARIABLE", "USEFUL_VALUE");
                File.WriteAllText(settingsFile, @"{ ""key"":""test value"" }");

                var contextMock = new Mock <IWebAppContext>();
                contextMock.SetupGet(c => c.HomeFolder)
                .Returns(tempFolder);

                var config = SiteConfigurationProvider.GetAzureLoggingConfiguration(contextMock.Object);

                Assert.Equal("test value", config["key"]);
                Assert.Equal("USEFUL_VALUE", config["RANDOM_ENVIRONMENT_VARIABLE"]);
            }
            finally
            {
                if (Directory.Exists(tempFolder))
                {
                    try
                    {
                        Directory.Delete(tempFolder, recursive: true);
                    }
                    catch
                    {
                        // Don't break the test if temp folder deletion fails.
                    }
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Adds an Azure Web Apps diagnostics logger.
        /// </summary>
        /// <param name="factory">The extension method argument</param>
        /// <param name="settings">The setting object to configure loggers.</param>
        public static ILoggerFactory AddAzureWebAppDiagnostics(this ILoggerFactory factory, AzureAppServicesDiagnosticsSettings settings)
        {
            var context = WebAppContext.Default;

            if (!context.IsRunningInAzureWebApp)
            {
                return(factory);
            }

            var config = SiteConfigurationProvider.GetAzureLoggingConfiguration(context);

            // Only add the provider if we're in Azure WebApp. That cannot change once the apps started
            var fileOptions = new OptionsMonitor <AzureFileLoggerOptions>(
                new OptionsFactory <AzureFileLoggerOptions>(
                    new IConfigureOptions <AzureFileLoggerOptions>[]
            {
                new FileLoggerConfigureOptions(config, context),
                new ConfigureOptions <AzureFileLoggerOptions>(options =>
                {
                    options.FileSizeLimit          = settings.FileSizeLimit;
                    options.RetainedFileCountLimit = settings.RetainedFileCountLimit;
                    options.BackgroundQueueSize    = settings.BackgroundQueueSize == 0 ? (int?)null : settings.BackgroundQueueSize;

                    if (settings.FileFlushPeriod != null)
                    {
                        options.FlushPeriod = settings.FileFlushPeriod.Value;
                    }
                })
            },
                    new IPostConfigureOptions <AzureFileLoggerOptions> [0]
                    ),
                new[]
            {
                new ConfigurationChangeTokenSource <AzureFileLoggerOptions>(config)
            },
                new OptionsCache <AzureFileLoggerOptions>()
                );

            var blobOptions = new OptionsMonitor <AzureBlobLoggerOptions>(
                new OptionsFactory <AzureBlobLoggerOptions>(
                    new IConfigureOptions <AzureBlobLoggerOptions>[] {
                new BlobLoggerConfigureOptions(config, context),
                new ConfigureOptions <AzureBlobLoggerOptions>(options =>
                {
                    options.BlobName            = settings.BlobName;
                    options.FlushPeriod         = settings.BlobCommitPeriod;
                    options.BatchSize           = settings.BlobBatchSize;
                    options.BackgroundQueueSize = settings.BackgroundQueueSize == 0 ? (int?)null : settings.BackgroundQueueSize;
                })
            },
                    new IPostConfigureOptions <AzureBlobLoggerOptions> [0]
                    ),
                new[]
            {
                new ConfigurationChangeTokenSource <AzureBlobLoggerOptions>(config)
            },
                new OptionsCache <AzureBlobLoggerOptions>()
                );

            var filterOptions = new OptionsMonitor <LoggerFilterOptions>(
                new OptionsFactory <LoggerFilterOptions>(
                    new[]
            {
                CreateFileFilterConfigureOptions(config),
                CreateBlobFilterConfigureOptions(config)
            },
                    new IPostConfigureOptions <LoggerFilterOptions> [0]),
                new [] { new ConfigurationChangeTokenSource <LoggerFilterOptions>(config) },
                new OptionsCache <LoggerFilterOptions>());

            factory.AddProvider(new ForwardingLoggerProvider(
                                    new LoggerFactory(
                                        new ILoggerProvider[]
            {
                new FileLoggerProvider(fileOptions),
                new BlobLoggerProvider(blobOptions)
            },
                                        filterOptions
                                        )
                                    ));
            return(factory);
        }