Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to fix logging in plugins via ConfigureDanPluginDefaults #37

Merged
merged 2 commits into from
Mar 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions Dan.Common/Dan.Common.csproj
Original file line number Diff line number Diff line change
@@ -31,14 +31,15 @@
<ItemGroup>
<PackageReference Include="AsyncKeyedLock" Version="6.2.0" />
<PackageReference Include="Microsoft.Azure.Core.NewtonsoftJson" Version="1.0.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.11.0-preview2" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.12.1-preview1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.0.0-preview3" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="6.0.9" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="6.0.9" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="7.0.4" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="7.0.4" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />

<PackageReference Include="Polly.Caching.Distributed" Version="3.0.1" />
<PackageReference Include="Polly.Caching.Serialization.Json" Version="3.0.0" />
<PackageReference Include="StackExchange.Redis" Version="2.6.66" />
<PackageReference Include="StackExchange.Redis" Version="2.6.96" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>

37 changes: 30 additions & 7 deletions Dan.Common/Extensions/HostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -3,8 +3,10 @@
using Dan.Common.Interfaces;
using Dan.Common.Services;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Polly;
using Polly.Extensions.Http;
using Polly.Registry;
@@ -15,22 +17,21 @@ public static class HostBuilderExtensions
{
/// <summary>
/// Sets up the isolated worker function with default configuration and wiring with ConfigureFunctionsWorkerDefaults(),
/// handling application insights
/// logging and correct JSON serialization settings. Also adds defaults services; HttpClientFactory with a
/// circuit-breaker enabled named client (use Constants.SafeHttpClient)
/// which should be used for outbound requests to the data source. Also expects to find a service implementing
/// IEvidenceSourceMetadata.
/// handling application insights, logging and correct JSON serialization settings. Also adds defaults services;
/// HttpClientFactory with a circuit-breaker enabled named client (use Constants.SafeHttpClient) which should be
/// used for outbound requests to the data source. Also expects to find a service implementing IEvidenceSourceMetadata.
/// </summary>
/// <param name="builder">The host builder</param>
/// <returns>The host builder for additional chaining</returns>
/// <exception cref="NotImplementedException">Thrown if IEvidenceSourceMetadata is not implemented in the same assembly</exception>
public static IHostBuilder ConfigureDanPluginDefaults(this IHostBuilder builder)
{
builder.ConfigureFunctionsWorkerDefaults(workerBuilder =>
builder
.ConfigureFunctionsWorkerDefaults(workerBuilder =>
{
workerBuilder
// Using preview package Microsoft.Azure.Functions.Worker.ApplicationInsights, see https://github.com/Azure/azure-functions-dotnet-worker/pull/944
// Requires APPLICATIONINSIGHTS_CONNECTION_STRING being set. Note that host.json logging settings will have to be replicated to worker.json
// Requires APPLICATIONINSIGHTS_CONNECTION_STRING being set. Note that host.json logging settings are not loaded to worker, and requires
.AddApplicationInsights()
.AddApplicationInsightsLogger();
}, options =>
@@ -43,11 +44,33 @@ public static IHostBuilder ConfigureDanPluginDefaults(this IHostBuilder builder)
NullValueHandling = NullValueHandling.Ignore
});
})
.ConfigureAppConfiguration((config) =>
{
config.AddJsonFile("host.json", optional: true);
config.AddJsonFile("worker.json", optional: true);
})
.ConfigureServices((context, services) =>
{
services.AddLogging();
services.AddHttpClient();

// You will need extra configuration because AI will only log per default Warning (default AI configuration). As this is a provider-specific
// setting, it will override all non-provider (Logging:LogLevel)-based configurations.
// https://github.com/microsoft/ApplicationInsights-dotnet/blob/main/NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs#L427
// https://github.com/microsoft/ApplicationInsights-dotnet/issues/2610#issuecomment-1316672650
// https://github.com/Azure/azure-functions-dotnet-worker/issues/1182#issuecomment-1319035412
// So remove the default logger rule (warning and above). This will result that the default will be Information.
services.Configure<LoggerFilterOptions>(options =>
{
var toRemove = options.Rules.FirstOrDefault(rule => rule.ProviderName
== "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");

if (toRemove is not null)
{
options.Rules.Remove(toRemove);
}
});

var openCircuitTimeSeconds =
int.TryParse(context.Configuration["DefaultCircuitBreakerOpenCircuitTimeSeconds"], out var result)
? result
34 changes: 20 additions & 14 deletions Dan.Core/Dan.Core.csproj
Original file line number Diff line number Diff line change
@@ -10,6 +10,9 @@
<PropertyGroup>
<SourceRevisionId>build$([System.DateTime]::UtcNow.ToString("yyyyMMddHHmmss"))</SourceRevisionId>
</PropertyGroup>
<PropertyGroup>
<GitVersion>false</GitVersion>
</PropertyGroup>
<ItemGroup>
<None Remove="Views\ConsentGiven.html" />
<None Remove="Views\Error.html" />
@@ -29,25 +32,28 @@
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.7.0" />
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.4.0" />
<PackageReference Include="GitInfo" Version="2.2.0" />
<PackageReference Include="Azure.Identity" Version="1.8.2" />
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.5.0" />
<PackageReference Include="GitInfo" Version="2.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="JmesPath.Net" Version="1.0.205" />
<PackageReference Include="jose-jwt" Version="4.0.1" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.31.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.11.0-preview2" />
<PackageReference Include="jose-jwt" Version="4.1.0" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.32.2" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.12.1-preview1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.0.0-preview3" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.CosmosDB" Version="3.0.9" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.CosmosDB" Version="4.0.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.8.0-preview2" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="6.0.9" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="6.24.0" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.24.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.9.0-preview1" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="7.0.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0" />
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="6.27.0" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.27.0" />
<PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.24.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.27.0" />

<!-- WCF related things -->
<PackageReference Include="System.Private.ServiceModel" Version="4.10.0" />
5 changes: 3 additions & 2 deletions Dan.Core/FuncMetadataEvidence.cs
Original file line number Diff line number Diff line change
@@ -48,7 +48,7 @@ public async Task<HttpResponseData> Run(
return response;
}
}

#if !DEBUG
/// <summary>
/// Runs every five minutes to trigger a forced refresh of all evidence codes. This prevents disruption of calls to the HTTP endpoint,
/// which will not block while updating.
@@ -60,4 +60,5 @@ public async Task Refresh([TimerTrigger("0 */5 * * * *")] TimerInfo timerInfo)
{
await _availableEvidenceCodesService.GetAvailableEvidenceCodes(true);
}
}
#endif
}
5 changes: 3 additions & 2 deletions Dan.Core/Helpers/VersionHelper.cs
Original file line number Diff line number Diff line change
@@ -15,8 +15,9 @@ public static VersionInfo GetVersionInfo()
_versionInfo = new VersionInfo
{
Name = assembly.GetName().Name ?? "dancore",
Built = GetBuildDate(assembly).ToString("u"),
Commit = ThisAssembly.Git.Commit
Built = GetBuildDate(assembly).ToString(@"yyyy-MM-dd\THH:mm:sszzz"),
Commit = ThisAssembly.Git.Commit,
CommitDate = ThisAssembly.Git.CommitDate
};

return _versionInfo;
3 changes: 3 additions & 0 deletions Dan.Core/Models/VersionInfo.cs
Original file line number Diff line number Diff line change
@@ -12,4 +12,7 @@ public class VersionInfo

[JsonProperty("commit")]
public string Commit { get; set; } = string.Empty;

[JsonProperty("commitDate")]
public string CommitDate { get; set; } = string.Empty;
}
13 changes: 1 addition & 12 deletions Dan.Core/Program.cs
Original file line number Diff line number Diff line change
@@ -18,7 +18,6 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using Newtonsoft.Json;
using Polly;
using Polly.Caching;
@@ -45,16 +44,7 @@
})
.ConfigureLogging((hostingContext, logging) =>
{
if (danHostingEnvironment.IsLocalDevelopment())
{
logging.AddSimpleConsole(options =>
{
options.ColorBehavior = LoggerColorBehavior.Enabled;
options.SingleLine = true;
options.IncludeScopes = false;
});
}

// This is required for the logging-options in worker-logging.json to ble applied
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
})
.ConfigureFunctionsWorkerDefaults(builder =>
@@ -83,7 +73,6 @@
})
.ConfigureServices((_, services) =>
{

// You will need extra configuration because above will only log per default Warning (default AI configuration). As this is a provider-specific
// setting, it will override all non-provider (Logging:LogLevel)-based configurations.
// https://github.com/microsoft/ApplicationInsights-dotnet/blob/main/NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs#L427