generated from arcus-azure/arcus.github.template
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature - clear logger providers except functions extension (#134)
* Feature - clear logger providers except functions * pr-sug: update docs w/ created project
- Loading branch information
1 parent
8594c34
commit 3eec37f
Showing
9 changed files
with
203 additions
and
2 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
32 changes: 32 additions & 0 deletions
32
...bservability.Telemetry.AzureFunctions/Arcus.Observability.Telemetry.AzureFunctions.csproj
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,32 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0;netcoreapp3.1</TargetFrameworks> | ||
<Authors>Arcus</Authors> | ||
<Company>Arcus</Company> | ||
<Description>Provides capability to improve telemetry with Serilog in Azure Functions applications</Description> | ||
<Copyright>Copyright (c) Arcus</Copyright> | ||
<PackageLicenseUrl>https://github.com/arcus-azure/arcus.observability/blob/master/LICENSE</PackageLicenseUrl> | ||
<PackageProjectUrl>https://github.com/arcus-azure/arcus.observability</PackageProjectUrl> | ||
<PackageIconUrl>https://raw.githubusercontent.com/arcus-azure/arcus/master/media/arcus.png</PackageIconUrl> | ||
<RepositoryUrl>https://github.com/arcus-azure/arcus.observability</RepositoryUrl> | ||
<RepositoryType>Git</RepositoryType> | ||
<PackageTags>Azure;Observability;Telemetry;Azure Functions</PackageTags> | ||
<PackageId>Arcus.Observability.Telemetry.AzureFunctions</PackageId> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'"> | ||
<FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition="'$(TargetFramework)' != 'netcoreapp3.1'"> | ||
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Guard.Net" Version="1.2.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
39 changes: 39 additions & 0 deletions
39
src/Arcus.Observability.Telemetry.AzureFunctions/Extensions/ILoggerBuilderExtensions.cs
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,39 @@ | ||
using GuardNet; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.Extensions.Logging | ||
{ | ||
/// <summary> | ||
/// <see cref="ILoggingBuilder"/> extensions related to Azure Functions projects. | ||
/// </summary> | ||
// ReSharper disable once InconsistentNaming | ||
public static class ILoggingBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Clears the <see cref="ILoggerProvider"/> registrations from the given <paramref name="loggingBuilder"/>, | ||
/// except the specific Azure Functions registrations. | ||
/// </summary> | ||
/// <param name="loggingBuilder">The builder containing the <see cref="ILoggerProvider"/> registrations.</param> | ||
public static ILoggingBuilder ClearProvidersExceptFunctionProviders(this ILoggingBuilder loggingBuilder) | ||
{ | ||
Guard.NotNull(loggingBuilder, nameof(loggingBuilder)); | ||
|
||
// Kudos to katrash: https://stackoverflow.com/questions/45986517/remove-console-and-debug-loggers-in-asp-net-core-2-0-when-in-production-mode | ||
foreach (ServiceDescriptor serviceDescriptor in loggingBuilder.Services) | ||
{ | ||
if (serviceDescriptor.ServiceType == typeof(ILoggerProvider)) | ||
{ | ||
if (serviceDescriptor.ImplementationType.FullName != "Microsoft.Azure.WebJobs.Script.Diagnostics.HostFileLoggerProvider" | ||
&& serviceDescriptor.ImplementationType.FullName != "Microsoft.Azure.WebJobs.Script.Diagnostics.FunctionFileLoggerProvider") | ||
{ | ||
loggingBuilder.Services.Remove(serviceDescriptor); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return loggingBuilder; | ||
} | ||
} | ||
} |
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
32 changes: 32 additions & 0 deletions
32
...s.Observability.Tests.Unit/Telemetry/AzureFunctions/Fixture/FunctionFileLoggerProvider.cs
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,32 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Microsoft.Extensions.Logging; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.Azure.WebJobs.Script.Diagnostics | ||
{ | ||
/// <summary> | ||
/// <see cref="ILoggerProvider"/> to verify if the surrogate of the real provider doesn't get removed. | ||
/// </summary> | ||
public class FunctionFileLoggerProvider : ILoggerProvider | ||
{ | ||
/// <summary> | ||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new <see cref="T:Microsoft.Extensions.Logging.ILogger" /> instance. | ||
/// </summary> | ||
/// <param name="categoryName">The category name for messages produced by the logger.</param> | ||
/// <returns>The instance of <see cref="T:Microsoft.Extensions.Logging.ILogger" /> that was created.</returns> | ||
public ILogger CreateLogger(string categoryName) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...Arcus.Observability.Tests.Unit/Telemetry/AzureFunctions/Fixture/HostFileLoggerProvider.cs
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,30 @@ | ||
using System; | ||
using Microsoft.Extensions.Logging; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.Azure.WebJobs.Script.Diagnostics | ||
{ | ||
/// <summary> | ||
/// <see cref="ILoggerProvider"/> to verify if the surrogate of the real provider doesn't get removed. | ||
/// </summary> | ||
public class HostFileLoggerProvider : ILoggerProvider | ||
{ | ||
/// <summary> | ||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new <see cref="T:Microsoft.Extensions.Logging.ILogger" /> instance. | ||
/// </summary> | ||
/// <param name="categoryName">The category name for messages produced by the logger.</param> | ||
/// <returns>The instance of <see cref="T:Microsoft.Extensions.Logging.ILogger" /> that was created.</returns> | ||
public ILogger CreateLogger(string categoryName) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Arcus.Observability.Tests.Unit/Telemetry/AzureFunctions/Fixture/TestLoggerProvider.cs
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,29 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Arcus.Observability.Tests.Unit.Telemetry.AzureFunctions.Fixture | ||
{ | ||
/// <summary> | ||
/// <see cref="ILoggerProvider"/> implementation to verify if the provider gets removed during the <see cref="ILoggerBuilderExtensionsTests"/>. | ||
/// </summary> | ||
public class TestLoggerProvider : ILoggerProvider | ||
{ | ||
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary> | ||
public void Dispose() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new <see cref="T:Microsoft.Extensions.Logging.ILogger" /> instance. | ||
/// </summary> | ||
/// <param name="categoryName">The category name for messages produced by the logger.</param> | ||
/// <returns>The instance of <see cref="T:Microsoft.Extensions.Logging.ILogger" /> that was created.</returns> | ||
public ILogger CreateLogger(string categoryName) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Arcus.Observability.Tests.Unit/Telemetry/AzureFunctions/ILoggerBuilderExtensionsTests.cs
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,32 @@ | ||
using Arcus.Observability.Tests.Unit.Telemetry.AzureFunctions.Fixture; | ||
using Microsoft.Azure.WebJobs.Script.Diagnostics; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Moq; | ||
using Microsoft.Extensions.Logging; | ||
using Xunit; | ||
|
||
namespace Arcus.Observability.Tests.Unit.Telemetry.AzureFunctions | ||
{ | ||
public class ILoggerBuilderExtensionsTests | ||
{ | ||
[Fact] | ||
public void ClearProvidersExceptFunctions_WithLoggerBuilderContainingProviders_RemovesNonFunctionProviders() | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection(); | ||
services.AddSingleton<ILoggerProvider, HostFileLoggerProvider>(); | ||
services.AddSingleton<ILoggerProvider, FunctionFileLoggerProvider>(); | ||
services.AddSingleton<ILoggerProvider, TestLoggerProvider>(); | ||
var builder = new Mock<ILoggingBuilder>(); | ||
builder.Setup(b => b.Services).Returns(services); | ||
|
||
// Act | ||
builder.Object.ClearProvidersExceptFunctionProviders(); | ||
|
||
// Assert | ||
Assert.NotEmpty(services); | ||
Assert.Equal(2, services.Count); | ||
Assert.All(services, desc => Assert.NotEqual(typeof(TestLoggerProvider), desc.ImplementationType)); | ||
} | ||
} | ||
} |
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