From d460163951976d9f692551b82a4787c2b8d4351d Mon Sep 17 00:00:00 2001 From: Mohammad Moattar Date: Mon, 26 Jul 2021 16:11:05 +0100 Subject: [PATCH 1/3] Initial support for Azure logs --- .../AzureLogAnalyticsOptions.cs | 11 ++++ .../AzureTableStorageOptions.cs | 7 +++ .../Easify.Azure.Serilog.csproj | 20 ++++++++ .../SinkBuilderContext.cs | 50 +++++++++++++++++++ src/Easify.Azure.sln | 6 +++ 5 files changed, 94 insertions(+) create mode 100644 src/Easify.Azure.Serilog/AzureLogAnalyticsOptions.cs create mode 100644 src/Easify.Azure.Serilog/AzureTableStorageOptions.cs create mode 100644 src/Easify.Azure.Serilog/Easify.Azure.Serilog.csproj create mode 100644 src/Easify.Azure.Serilog/SinkBuilderContext.cs diff --git a/src/Easify.Azure.Serilog/AzureLogAnalyticsOptions.cs b/src/Easify.Azure.Serilog/AzureLogAnalyticsOptions.cs new file mode 100644 index 0000000..31aa997 --- /dev/null +++ b/src/Easify.Azure.Serilog/AzureLogAnalyticsOptions.cs @@ -0,0 +1,11 @@ +namespace Easify.Azure.Serilog +{ + public sealed class AzureLogAnalyticsOptions + { + public string WorkspaceId { get; set; } + public string AuthenticationId { get; set; } + public string LogName { get; set; } + public int? LogBufferSize { get; set; } + public int? BatchSize { get; set; } + } +} \ No newline at end of file diff --git a/src/Easify.Azure.Serilog/AzureTableStorageOptions.cs b/src/Easify.Azure.Serilog/AzureTableStorageOptions.cs new file mode 100644 index 0000000..0ef8a12 --- /dev/null +++ b/src/Easify.Azure.Serilog/AzureTableStorageOptions.cs @@ -0,0 +1,7 @@ +namespace Easify.Azure.Serilog +{ + public sealed class AzureTableStorageOptions + { + public string ConnectionString { get; set; } + } +} \ No newline at end of file diff --git a/src/Easify.Azure.Serilog/Easify.Azure.Serilog.csproj b/src/Easify.Azure.Serilog/Easify.Azure.Serilog.csproj new file mode 100644 index 0000000..c462c7c --- /dev/null +++ b/src/Easify.Azure.Serilog/Easify.Azure.Serilog.csproj @@ -0,0 +1,20 @@ + + + + netstandard2.0 + true + Easify.Azure.Serilog + Mohammad Moattar + https://github.com/icgam/Easify.Azure + https://github.com/icgam/Easify.Azure + default + + + + + + + + + + diff --git a/src/Easify.Azure.Serilog/SinkBuilderContext.cs b/src/Easify.Azure.Serilog/SinkBuilderContext.cs new file mode 100644 index 0000000..d4b8dbf --- /dev/null +++ b/src/Easify.Azure.Serilog/SinkBuilderContext.cs @@ -0,0 +1,50 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using Easify.Logging.SeriLog; +using Microsoft.Extensions.Configuration; +using Serilog; + +namespace Easify.Azure.Serilog +{ + [ExcludeFromCodeCoverage] + public static class SinkBuilderContext + { + public static ISinkBuilderContext UseAzureTableStorage(this ISinkBuilderContext sinkBuilderContext, IConfiguration configuration, Action configure = null) + { + if (configuration == null) throw new ArgumentNullException(nameof(configuration)); + + var options = new AzureTableStorageOptions(); + configuration.GetSection(nameof(AzureTableStorageOptions)).Bind(options); + configure?.Invoke(options); + + if (string.IsNullOrWhiteSpace(options.ConnectionString)) + throw new ArgumentException($"{nameof(options.ConnectionString)} is missing in AzureTableStorageOptions"); + + var loggerConfiguration = sinkBuilderContext.LoggerConfiguration.WriteTo.AzureTableStorage(options.ConnectionString); + + return sinkBuilderContext.Clone(loggerConfiguration); + } + + public static ISinkBuilderContext UseAzureLogAnalytics(this ISinkBuilderContext sinkBuilderContext, IConfiguration configuration, Action configure = null) + { + if (configuration == null) throw new ArgumentNullException(nameof(configuration)); + + var options = new AzureLogAnalyticsOptions(); + configuration.GetSection(nameof(AzureLogAnalyticsOptions)).Bind(options); + configure?.Invoke(options); + + if (string.IsNullOrWhiteSpace(options.WorkspaceId)) + throw new ArgumentException($"{nameof(options.WorkspaceId)} is missing in AzureLogAnalyticsOptions"); + + if (string.IsNullOrWhiteSpace(options.AuthenticationId)) + throw new ArgumentException($"{nameof(options.AuthenticationId)} is missing in AzureLogAnalyticsOptions"); + + var loggerConfiguration = sinkBuilderContext + .LoggerConfiguration + .WriteTo + .AzureAnalytics(options.WorkspaceId,options.AuthenticationId); + + return sinkBuilderContext.Clone(loggerConfiguration); + } + } +} \ No newline at end of file diff --git a/src/Easify.Azure.sln b/src/Easify.Azure.sln index a0bd9fa..6de0b57 100644 --- a/src/Easify.Azure.sln +++ b/src/Easify.Azure.sln @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Easify.Azure.AspNetCore", " EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Easify.Azure.AspNetCore.UnitTests", "Easify.Azure.AspNetCore.UnitTests\Easify.Azure.AspNetCore.UnitTests.csproj", "{F647B941-F688-4C7F-A45B-5F0AD5772FE1}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Easify.Azure.Serilog", "Easify.Azure.Serilog\Easify.Azure.Serilog.csproj", "{0DBB081F-8EA1-410D-9340-2D5D5003E657}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {F647B941-F688-4C7F-A45B-5F0AD5772FE1}.Debug|Any CPU.Build.0 = Debug|Any CPU {F647B941-F688-4C7F-A45B-5F0AD5772FE1}.Release|Any CPU.ActiveCfg = Release|Any CPU {F647B941-F688-4C7F-A45B-5F0AD5772FE1}.Release|Any CPU.Build.0 = Release|Any CPU + {0DBB081F-8EA1-410D-9340-2D5D5003E657}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0DBB081F-8EA1-410D-9340-2D5D5003E657}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0DBB081F-8EA1-410D-9340-2D5D5003E657}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0DBB081F-8EA1-410D-9340-2D5D5003E657}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From 6ad7273212877ea4449d31cae26b699675e4909e Mon Sep 17 00:00:00 2001 From: Mohammad Moattar Date: Thu, 29 Jul 2021 13:16:58 +0100 Subject: [PATCH 2/3] Azure Logging --- ...{SinkBuilderContext.cs => SinkBuilderContextExtensions.cs} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename src/Easify.Azure.Serilog/{SinkBuilderContext.cs => SinkBuilderContextExtensions.cs} (96%) diff --git a/src/Easify.Azure.Serilog/SinkBuilderContext.cs b/src/Easify.Azure.Serilog/SinkBuilderContextExtensions.cs similarity index 96% rename from src/Easify.Azure.Serilog/SinkBuilderContext.cs rename to src/Easify.Azure.Serilog/SinkBuilderContextExtensions.cs index d4b8dbf..7594648 100644 --- a/src/Easify.Azure.Serilog/SinkBuilderContext.cs +++ b/src/Easify.Azure.Serilog/SinkBuilderContextExtensions.cs @@ -7,7 +7,7 @@ namespace Easify.Azure.Serilog { [ExcludeFromCodeCoverage] - public static class SinkBuilderContext + public static class SinkBuilderContextExtensions { public static ISinkBuilderContext UseAzureTableStorage(this ISinkBuilderContext sinkBuilderContext, IConfiguration configuration, Action configure = null) { @@ -42,7 +42,7 @@ public static ISinkBuilderContext UseAzureLogAnalytics(this ISinkBuilderContext var loggerConfiguration = sinkBuilderContext .LoggerConfiguration .WriteTo - .AzureAnalytics(options.WorkspaceId,options.AuthenticationId); + .AzureAnalytics(options.WorkspaceId,options.AuthenticationId, logName:options.LogName); return sinkBuilderContext.Clone(loggerConfiguration); } From 5f9e51a46bbed6774c674b91a26bd76895c13a54 Mon Sep 17 00:00:00 2001 From: Mohammad Moattar Date: Thu, 29 Jul 2021 13:31:46 +0100 Subject: [PATCH 3/3] Add comments for additional work --- src/Easify.Azure.Serilog/SinkBuilderContextExtensions.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Easify.Azure.Serilog/SinkBuilderContextExtensions.cs b/src/Easify.Azure.Serilog/SinkBuilderContextExtensions.cs index 7594648..eaffd14 100644 --- a/src/Easify.Azure.Serilog/SinkBuilderContextExtensions.cs +++ b/src/Easify.Azure.Serilog/SinkBuilderContextExtensions.cs @@ -44,6 +44,8 @@ public static ISinkBuilderContext UseAzureLogAnalytics(this ISinkBuilderContext .WriteTo .AzureAnalytics(options.WorkspaceId,options.AuthenticationId, logName:options.LogName); + // TODO: Sanitization of the LogName + return sinkBuilderContext.Clone(loggerConfiguration); } }