From d3110c056fb7aa5fb41e0e3a045cf1a376aea6ab Mon Sep 17 00:00:00 2001 From: YogiGrantz Date: Thu, 20 Aug 2020 10:03:01 -0700 Subject: [PATCH 1/6] Added an option to output Azure blob in flat folder structure with custom filename prefix --- .../AzureAppServicesLoggerFactoryExtensions.cs | 16 ++++++++++++++++ .../src/BlobLoggerProvider.cs | 12 +++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Logging.AzureAppServices/src/AzureAppServicesLoggerFactoryExtensions.cs b/src/Logging.AzureAppServices/src/AzureAppServicesLoggerFactoryExtensions.cs index 9b680e913817..123423c2189a 100644 --- a/src/Logging.AzureAppServices/src/AzureAppServicesLoggerFactoryExtensions.cs +++ b/src/Logging.AzureAppServices/src/AzureAppServicesLoggerFactoryExtensions.cs @@ -17,6 +17,22 @@ namespace Microsoft.Extensions.Logging /// public static class AzureAppServicesLoggerFactoryExtensions { + internal static string CustomPrefix; + /// + /// + /// + /// + /// + /// + public static ILoggingBuilder AddAzureWebAppDiagnostics(this ILoggingBuilder builder, string customPrefix) + { + CustomPrefix = customPrefix; + var context = WebAppContext.Default; + + // Only add the provider if we're in Azure WebApp. That cannot change once the apps started + return AddAzureWebAppDiagnostics(builder, context); + } + /// /// Adds an Azure Web Apps diagnostics logger. /// diff --git a/src/Logging.AzureAppServices/src/BlobLoggerProvider.cs b/src/Logging.AzureAppServices/src/BlobLoggerProvider.cs index 3d62ea2ac6e4..4dc89c8b0740 100644 --- a/src/Logging.AzureAppServices/src/BlobLoggerProvider.cs +++ b/src/Logging.AzureAppServices/src/BlobLoggerProvider.cs @@ -58,10 +58,20 @@ internal BlobLoggerProvider( internal override async Task WriteMessagesAsync(IEnumerable messages, CancellationToken cancellationToken) { var eventGroups = messages.GroupBy(GetBlobKey); + DateTime currDate = DateTime.Now; foreach (var eventGroup in eventGroups) { var key = eventGroup.Key; - var blobName = $"{_appName}/{key.Year}/{key.Month:00}/{key.Day:00}/{key.Hour:00}/{_fileName}"; + //var blobName = $"{_appName}/{key.Year}/{key.Month:00}/{key.Day:00}/{key.Hour:00}/{_fileName}"; + string blobName; + + if (!string.IsNullOrEmpty(AzureAppServicesLoggerFactoryExtensions.CustomPrefix)) + { + string filename = $"{AzureAppServicesLoggerFactoryExtensions.CustomPrefix}{currDate.Year}{currDate.Month:00}{currDate.Day:00}.txt"; + blobName = $"{_appName}/{filename}"; + } + else + blobName = $"{_appName}/{key.Year}/{key.Month:00}/{key.Day:00}/{key.Hour:00}/{_fileName}"; var blob = _blobReferenceFactory(blobName); From e89228d9062c10e21031e93440359081b4032ba8 Mon Sep 17 00:00:00 2001 From: YogiGrantz Date: Wed, 26 Aug 2020 09:07:14 -0700 Subject: [PATCH 2/6] Remove the commented code - not needed anymore --- src/Logging.AzureAppServices/src/BlobLoggerProvider.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Logging.AzureAppServices/src/BlobLoggerProvider.cs b/src/Logging.AzureAppServices/src/BlobLoggerProvider.cs index 4dc89c8b0740..ca8988b0367e 100644 --- a/src/Logging.AzureAppServices/src/BlobLoggerProvider.cs +++ b/src/Logging.AzureAppServices/src/BlobLoggerProvider.cs @@ -62,7 +62,6 @@ internal override async Task WriteMessagesAsync(IEnumerable messages foreach (var eventGroup in eventGroups) { var key = eventGroup.Key; - //var blobName = $"{_appName}/{key.Year}/{key.Month:00}/{key.Day:00}/{key.Hour:00}/{_fileName}"; string blobName; if (!string.IsNullOrEmpty(AzureAppServicesLoggerFactoryExtensions.CustomPrefix)) From 9f320f3bf4d7d4c85743700ce1fe99bf5ad283c8 Mon Sep 17 00:00:00 2001 From: YogiGrantz Date: Thu, 15 Oct 2020 12:35:34 -0700 Subject: [PATCH 3/6] Changed approach Changed approach - Removed mutable static object. Instead store the object (customPrefixFileName) in AzureBlobLoggerOptions and initialize it at DI entry point --- ...AzureAppServicesLoggerFactoryExtensions.cs | 26 +++++-------------- .../src/AzureBlobLoggerOptions.cs | 11 +++++++- .../src/BlobLoggerConfigureOptions.cs | 5 +++- .../src/BlobLoggerProvider.cs | 6 +++-- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/Logging.AzureAppServices/src/AzureAppServicesLoggerFactoryExtensions.cs b/src/Logging.AzureAppServices/src/AzureAppServicesLoggerFactoryExtensions.cs index 123423c2189a..2d3ba16441b7 100644 --- a/src/Logging.AzureAppServices/src/AzureAppServicesLoggerFactoryExtensions.cs +++ b/src/Logging.AzureAppServices/src/AzureAppServicesLoggerFactoryExtensions.cs @@ -17,35 +17,21 @@ namespace Microsoft.Extensions.Logging /// public static class AzureAppServicesLoggerFactoryExtensions { - internal static string CustomPrefix; - /// - /// - /// - /// - /// - /// - public static ILoggingBuilder AddAzureWebAppDiagnostics(this ILoggingBuilder builder, string customPrefix) - { - CustomPrefix = customPrefix; - var context = WebAppContext.Default; - - // Only add the provider if we're in Azure WebApp. That cannot change once the apps started - return AddAzureWebAppDiagnostics(builder, context); - } - /// /// Adds an Azure Web Apps diagnostics logger. /// /// The extension method argument - public static ILoggingBuilder AddAzureWebAppDiagnostics(this ILoggingBuilder builder) + /// + /// + public static ILoggingBuilder AddAzureWebAppDiagnostics(this ILoggingBuilder builder, string customPrefix = null) { var context = WebAppContext.Default; // Only add the provider if we're in Azure WebApp. That cannot change once the apps started - return AddAzureWebAppDiagnostics(builder, context); + return AddAzureWebAppDiagnostics(builder, context, customPrefix); } - internal static ILoggingBuilder AddAzureWebAppDiagnostics(this ILoggingBuilder builder, IWebAppContext context) + internal static ILoggingBuilder AddAzureWebAppDiagnostics(this ILoggingBuilder builder, IWebAppContext context, string customPrefix) { if (!context.IsRunningInAzureWebApp) { @@ -79,7 +65,7 @@ internal static ILoggingBuilder AddAzureWebAppDiagnostics(this ILoggingBuilder b if (addedBlobLogger) { services.AddSingleton>(CreateBlobFilterConfigureOptions(config)); - services.AddSingleton>(new BlobLoggerConfigureOptions(config, context)); + services.AddSingleton>(new BlobLoggerConfigureOptions(config, context, customPrefix)); services.AddSingleton>( new ConfigurationChangeTokenSource(config)); LoggerProviderOptions.RegisterProviderOptions(builder.Services); diff --git a/src/Logging.AzureAppServices/src/AzureBlobLoggerOptions.cs b/src/Logging.AzureAppServices/src/AzureBlobLoggerOptions.cs index 1e1285b35815..9387f9f6e7b3 100644 --- a/src/Logging.AzureAppServices/src/AzureBlobLoggerOptions.cs +++ b/src/Logging.AzureAppServices/src/AzureBlobLoggerOptions.cs @@ -12,7 +12,7 @@ namespace Microsoft.Extensions.Logging.AzureAppServices public class AzureBlobLoggerOptions: BatchingLoggerOptions { private string _blobName = "applicationLog.txt"; - + private string _customPrefixFileName = null; /// /// Gets or sets the last section of log blob name. /// Defaults to "applicationLog.txt". @@ -30,6 +30,15 @@ public string BlobName } } + internal string CustomPrefixFileName + { + get { return _customPrefixFileName; } + set + { + _customPrefixFileName = value; + } + } + internal string ContainerUrl { get; set; } internal string ApplicationName { get; set; } diff --git a/src/Logging.AzureAppServices/src/BlobLoggerConfigureOptions.cs b/src/Logging.AzureAppServices/src/BlobLoggerConfigureOptions.cs index f9a186872b7b..6cff97f32c01 100644 --- a/src/Logging.AzureAppServices/src/BlobLoggerConfigureOptions.cs +++ b/src/Logging.AzureAppServices/src/BlobLoggerConfigureOptions.cs @@ -11,12 +11,14 @@ internal class BlobLoggerConfigureOptions : BatchLoggerConfigureOptions, IConfig { private readonly IConfiguration _configuration; private readonly IWebAppContext _context; + private readonly string _customPrefix; - public BlobLoggerConfigureOptions(IConfiguration configuration, IWebAppContext context) + public BlobLoggerConfigureOptions(IConfiguration configuration, IWebAppContext context, string customPrefix) : base(configuration, "AzureBlobEnabled") { _configuration = configuration; _context = context; + _customPrefix = customPrefix; } public void Configure(AzureBlobLoggerOptions options) @@ -25,6 +27,7 @@ public void Configure(AzureBlobLoggerOptions options) options.ContainerUrl = _configuration.GetSection("APPSETTING_DIAGNOSTICS_AZUREBLOBCONTAINERSASURL")?.Value; options.ApplicationName = _context.SiteName; options.ApplicationInstanceId = _context.SiteInstanceId; + options.CustomPrefixFileName = _customPrefix; } } } diff --git a/src/Logging.AzureAppServices/src/BlobLoggerProvider.cs b/src/Logging.AzureAppServices/src/BlobLoggerProvider.cs index ca8988b0367e..f63a9c568e19 100644 --- a/src/Logging.AzureAppServices/src/BlobLoggerProvider.cs +++ b/src/Logging.AzureAppServices/src/BlobLoggerProvider.cs @@ -24,6 +24,7 @@ public class BlobLoggerProvider : BatchingLoggerProvider private readonly string _fileName; private readonly Func _blobReferenceFactory; private readonly HttpClient _httpClient; + private readonly string _customPrefixFileName; /// /// Creates a new instance of @@ -53,6 +54,7 @@ internal BlobLoggerProvider( _fileName = value.ApplicationInstanceId + "_" + value.BlobName; _blobReferenceFactory = blobReferenceFactory; _httpClient = new HttpClient(); + _customPrefixFileName = value.CustomPrefixFileName; } internal override async Task WriteMessagesAsync(IEnumerable messages, CancellationToken cancellationToken) @@ -64,9 +66,9 @@ internal override async Task WriteMessagesAsync(IEnumerable messages var key = eventGroup.Key; string blobName; - if (!string.IsNullOrEmpty(AzureAppServicesLoggerFactoryExtensions.CustomPrefix)) + if (!string.IsNullOrEmpty(_customPrefixFileName)) { - string filename = $"{AzureAppServicesLoggerFactoryExtensions.CustomPrefix}{currDate.Year}{currDate.Month:00}{currDate.Day:00}.txt"; + string filename = $"{_customPrefixFileName}{currDate.Year}{currDate.Month:00}{currDate.Day:00}.txt"; blobName = $"{_appName}/{filename}"; } else From 917a1d38bb995ea254502d89210cbda4c6cd0906 Mon Sep 17 00:00:00 2001 From: YogiGrantz Date: Fri, 16 Oct 2020 11:02:05 -0700 Subject: [PATCH 4/6] Fixed binary breaking change Removed the optional parameter, created overload to handle the extra parameter --- .../AzureAppServicesLoggerFactoryExtensions.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Logging.AzureAppServices/src/AzureAppServicesLoggerFactoryExtensions.cs b/src/Logging.AzureAppServices/src/AzureAppServicesLoggerFactoryExtensions.cs index 2d3ba16441b7..ba47090f47d3 100644 --- a/src/Logging.AzureAppServices/src/AzureAppServicesLoggerFactoryExtensions.cs +++ b/src/Logging.AzureAppServices/src/AzureAppServicesLoggerFactoryExtensions.cs @@ -17,13 +17,26 @@ namespace Microsoft.Extensions.Logging /// public static class AzureAppServicesLoggerFactoryExtensions { + /// + /// Adds an Azure Web Apps diagnostics logger. + /// + /// The extension method argument + /// + public static ILoggingBuilder AddAzureWebAppDiagnostics(this ILoggingBuilder builder) + { + var context = WebAppContext.Default; + + // Only add the provider if we're in Azure WebApp. That cannot change once the apps started + return AddAzureWebAppDiagnostics(builder, context, null); + } + /// /// Adds an Azure Web Apps diagnostics logger. /// /// The extension method argument /// /// - public static ILoggingBuilder AddAzureWebAppDiagnostics(this ILoggingBuilder builder, string customPrefix = null) + public static ILoggingBuilder AddAzureWebAppDiagnostics(this ILoggingBuilder builder, string customPrefix) { var context = WebAppContext.Default; From 88eb54f2c921a0d71e0eb68095db14dd41f3d97c Mon Sep 17 00:00:00 2001 From: YogiGrantz Date: Fri, 16 Oct 2020 11:35:15 -0700 Subject: [PATCH 5/6] Renamed parm Gihub repository did not take my change for removig the optional = null, so I am renaming the parm --- .../src/AzureAppServicesLoggerFactoryExtensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Logging.AzureAppServices/src/AzureAppServicesLoggerFactoryExtensions.cs b/src/Logging.AzureAppServices/src/AzureAppServicesLoggerFactoryExtensions.cs index ba47090f47d3..b5b7557f65af 100644 --- a/src/Logging.AzureAppServices/src/AzureAppServicesLoggerFactoryExtensions.cs +++ b/src/Logging.AzureAppServices/src/AzureAppServicesLoggerFactoryExtensions.cs @@ -36,12 +36,12 @@ public static ILoggingBuilder AddAzureWebAppDiagnostics(this ILoggingBuilder bui /// The extension method argument /// /// - public static ILoggingBuilder AddAzureWebAppDiagnostics(this ILoggingBuilder builder, string customPrefix) + public static ILoggingBuilder AddAzureWebAppDiagnostics(this ILoggingBuilder builder, string custPrefix) { var context = WebAppContext.Default; // Only add the provider if we're in Azure WebApp. That cannot change once the apps started - return AddAzureWebAppDiagnostics(builder, context, customPrefix); + return AddAzureWebAppDiagnostics(builder, context, custPrefix); } internal static ILoggingBuilder AddAzureWebAppDiagnostics(this ILoggingBuilder builder, IWebAppContext context, string customPrefix) From 2adb379e4ae9ffda7dd38d4751e2ae5d7ee3a865 Mon Sep 17 00:00:00 2001 From: YogiGrantz Date: Tue, 20 Oct 2020 19:41:59 -0700 Subject: [PATCH 6/6] Renamed the parm back Renamed the parm back. Hoping to get a clean build --- .../src/AzureAppServicesLoggerFactoryExtensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Logging.AzureAppServices/src/AzureAppServicesLoggerFactoryExtensions.cs b/src/Logging.AzureAppServices/src/AzureAppServicesLoggerFactoryExtensions.cs index b5b7557f65af..ba47090f47d3 100644 --- a/src/Logging.AzureAppServices/src/AzureAppServicesLoggerFactoryExtensions.cs +++ b/src/Logging.AzureAppServices/src/AzureAppServicesLoggerFactoryExtensions.cs @@ -36,12 +36,12 @@ public static ILoggingBuilder AddAzureWebAppDiagnostics(this ILoggingBuilder bui /// The extension method argument /// /// - public static ILoggingBuilder AddAzureWebAppDiagnostics(this ILoggingBuilder builder, string custPrefix) + public static ILoggingBuilder AddAzureWebAppDiagnostics(this ILoggingBuilder builder, string customPrefix) { var context = WebAppContext.Default; // Only add the provider if we're in Azure WebApp. That cannot change once the apps started - return AddAzureWebAppDiagnostics(builder, context, custPrefix); + return AddAzureWebAppDiagnostics(builder, context, customPrefix); } internal static ILoggingBuilder AddAzureWebAppDiagnostics(this ILoggingBuilder builder, IWebAppContext context, string customPrefix)