Skip to content

Commit

Permalink
Merge pull request #5 from icgam/Feature/AzureLog
Browse files Browse the repository at this point in the history
Feature/azure log
  • Loading branch information
moattarwork authored Sep 30, 2021
2 parents d6aa7cb + 5f9e51a commit 56fd3d6
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Easify.Azure.Serilog/AzureLogAnalyticsOptions.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
7 changes: 7 additions & 0 deletions src/Easify.Azure.Serilog/AzureTableStorageOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Easify.Azure.Serilog
{
public sealed class AzureTableStorageOptions
{
public string ConnectionString { get; set; }
}
}
20 changes: 20 additions & 0 deletions src/Easify.Azure.Serilog/Easify.Azure.Serilog.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Title>Easify.Azure.Serilog</Title>
<Authors>Mohammad Moattar</Authors>
<PackageProjectUrl>https://github.com/icgam/Easify.Azure</PackageProjectUrl>
<RepositoryUrl>https://github.com/icgam/Easify.Azure</RepositoryUrl>
<LangVersion>default</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Easify.Logging.SeriLog" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.AzureAnalytics" Version="4.7.0" />
<PackageReference Include="Serilog.Sinks.AzureTableStorage" Version="5.0.0" />
</ItemGroup>

</Project>
52 changes: 52 additions & 0 deletions src/Easify.Azure.Serilog/SinkBuilderContextExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Diagnostics.CodeAnalysis;
using Easify.Logging.SeriLog;
using Microsoft.Extensions.Configuration;
using Serilog;

namespace Easify.Azure.Serilog
{
[ExcludeFromCodeCoverage]
public static class SinkBuilderContextExtensions
{
public static ISinkBuilderContext UseAzureTableStorage(this ISinkBuilderContext sinkBuilderContext, IConfiguration configuration, Action<AzureTableStorageOptions> 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<AzureLogAnalyticsOptions> 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, logName:options.LogName);

// TODO: Sanitization of the LogName

return sinkBuilderContext.Clone(loggerConfiguration);
}
}
}
6 changes: 6 additions & 0 deletions src/Easify.Azure.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 56fd3d6

Please sign in to comment.