forked from icgam/Easify.Azure
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request icgam#5 from icgam/Feature/AzureLog
Feature/azure log
- Loading branch information
Showing
5 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters