Skip to content

Commit a723d95

Browse files
authored
Attempt to fix logging in plugins via ConfigureDanPluginDefaults (#37)
1 parent cc93f6a commit a723d95

File tree

7 files changed

+66
-42
lines changed

7 files changed

+66
-42
lines changed

Dan.Common/Dan.Common.csproj

+6-5
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@
3131
<ItemGroup>
3232
<PackageReference Include="AsyncKeyedLock" Version="6.2.0" />
3333
<PackageReference Include="Microsoft.Azure.Core.NewtonsoftJson" Version="1.0.0" />
34-
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.11.0-preview2" />
34+
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.12.1-preview1" />
3535
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.0.0-preview3" />
36-
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="6.0.9" />
37-
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="6.0.9" />
38-
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
36+
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="7.0.4" />
37+
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="7.0.4" />
38+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
39+
3940
<PackageReference Include="Polly.Caching.Distributed" Version="3.0.1" />
4041
<PackageReference Include="Polly.Caching.Serialization.Json" Version="3.0.0" />
41-
<PackageReference Include="StackExchange.Redis" Version="2.6.66" />
42+
<PackageReference Include="StackExchange.Redis" Version="2.6.96" />
4243
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
4344
</ItemGroup>
4445

Dan.Common/Extensions/HostBuilderExtensions.cs

+30-7
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
using Dan.Common.Interfaces;
44
using Dan.Common.Services;
55
using Microsoft.Azure.Functions.Worker;
6+
using Microsoft.Extensions.Configuration;
67
using Microsoft.Extensions.DependencyInjection;
78
using Microsoft.Extensions.Hosting;
9+
using Microsoft.Extensions.Logging;
810
using Polly;
911
using Polly.Extensions.Http;
1012
using Polly.Registry;
@@ -15,22 +17,21 @@ public static class HostBuilderExtensions
1517
{
1618
/// <summary>
1719
/// Sets up the isolated worker function with default configuration and wiring with ConfigureFunctionsWorkerDefaults(),
18-
/// handling application insights
19-
/// logging and correct JSON serialization settings. Also adds defaults services; HttpClientFactory with a
20-
/// circuit-breaker enabled named client (use Constants.SafeHttpClient)
21-
/// which should be used for outbound requests to the data source. Also expects to find a service implementing
22-
/// IEvidenceSourceMetadata.
20+
/// handling application insights, logging and correct JSON serialization settings. Also adds defaults services;
21+
/// HttpClientFactory with a circuit-breaker enabled named client (use Constants.SafeHttpClient) which should be
22+
/// used for outbound requests to the data source. Also expects to find a service implementing IEvidenceSourceMetadata.
2323
/// </summary>
2424
/// <param name="builder">The host builder</param>
2525
/// <returns>The host builder for additional chaining</returns>
2626
/// <exception cref="NotImplementedException">Thrown if IEvidenceSourceMetadata is not implemented in the same assembly</exception>
2727
public static IHostBuilder ConfigureDanPluginDefaults(this IHostBuilder builder)
2828
{
29-
builder.ConfigureFunctionsWorkerDefaults(workerBuilder =>
29+
builder
30+
.ConfigureFunctionsWorkerDefaults(workerBuilder =>
3031
{
3132
workerBuilder
3233
// Using preview package Microsoft.Azure.Functions.Worker.ApplicationInsights, see https://github.com/Azure/azure-functions-dotnet-worker/pull/944
33-
// Requires APPLICATIONINSIGHTS_CONNECTION_STRING being set. Note that host.json logging settings will have to be replicated to worker.json
34+
// Requires APPLICATIONINSIGHTS_CONNECTION_STRING being set. Note that host.json logging settings are not loaded to worker, and requires
3435
.AddApplicationInsights()
3536
.AddApplicationInsightsLogger();
3637
}, options =>
@@ -43,11 +44,33 @@ public static IHostBuilder ConfigureDanPluginDefaults(this IHostBuilder builder)
4344
NullValueHandling = NullValueHandling.Ignore
4445
});
4546
})
47+
.ConfigureAppConfiguration((config) =>
48+
{
49+
config.AddJsonFile("host.json", optional: true);
50+
config.AddJsonFile("worker.json", optional: true);
51+
})
4652
.ConfigureServices((context, services) =>
4753
{
4854
services.AddLogging();
4955
services.AddHttpClient();
5056

57+
// You will need extra configuration because AI will only log per default Warning (default AI configuration). As this is a provider-specific
58+
// setting, it will override all non-provider (Logging:LogLevel)-based configurations.
59+
// https://github.com/microsoft/ApplicationInsights-dotnet/blob/main/NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs#L427
60+
// https://github.com/microsoft/ApplicationInsights-dotnet/issues/2610#issuecomment-1316672650
61+
// https://github.com/Azure/azure-functions-dotnet-worker/issues/1182#issuecomment-1319035412
62+
// So remove the default logger rule (warning and above). This will result that the default will be Information.
63+
services.Configure<LoggerFilterOptions>(options =>
64+
{
65+
var toRemove = options.Rules.FirstOrDefault(rule => rule.ProviderName
66+
== "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
67+
68+
if (toRemove is not null)
69+
{
70+
options.Rules.Remove(toRemove);
71+
}
72+
});
73+
5174
var openCircuitTimeSeconds =
5275
int.TryParse(context.Configuration["DefaultCircuitBreakerOpenCircuitTimeSeconds"], out var result)
5376
? result

Dan.Core/Dan.Core.csproj

+20-14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<PropertyGroup>
1111
<SourceRevisionId>build$([System.DateTime]::UtcNow.ToString("yyyyMMddHHmmss"))</SourceRevisionId>
1212
</PropertyGroup>
13+
<PropertyGroup>
14+
<GitVersion>false</GitVersion>
15+
</PropertyGroup>
1316
<ItemGroup>
1417
<None Remove="Views\ConsentGiven.html" />
1518
<None Remove="Views\Error.html" />
@@ -29,25 +32,28 @@
2932
</EmbeddedResource>
3033
</ItemGroup>
3134
<ItemGroup>
32-
<PackageReference Include="Azure.Identity" Version="1.7.0" />
33-
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.4.0" />
34-
<PackageReference Include="GitInfo" Version="2.2.0" />
35+
<PackageReference Include="Azure.Identity" Version="1.8.2" />
36+
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.5.0" />
37+
<PackageReference Include="GitInfo" Version="2.2.0">
38+
<PrivateAssets>all</PrivateAssets>
39+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
40+
</PackageReference>
3541
<PackageReference Include="JmesPath.Net" Version="1.0.205" />
36-
<PackageReference Include="jose-jwt" Version="4.0.1" />
37-
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.31.0" />
38-
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.11.0-preview2" />
42+
<PackageReference Include="jose-jwt" Version="4.1.0" />
43+
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.32.2" />
44+
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.12.1-preview1" />
3945
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.0.0-preview3" />
40-
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.CosmosDB" Version="3.0.9" />
46+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.CosmosDB" Version="4.0.1" />
4147
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
4248
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.1.0" />
43-
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.8.0-preview2" />
44-
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="6.0.0" />
45-
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="6.0.9" />
46-
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
47-
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="6.24.0" />
48-
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.24.0" />
49+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.9.0-preview1" />
50+
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="7.0.0" />
51+
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="7.0.4" />
52+
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0" />
53+
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="6.27.0" />
54+
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.27.0" />
4955
<PackageReference Include="Polly" Version="7.2.3" />
50-
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.24.0" />
56+
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.27.0" />
5157

5258
<!-- WCF related things -->
5359
<PackageReference Include="System.Private.ServiceModel" Version="4.10.0" />

Dan.Core/FuncMetadataEvidence.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public async Task<HttpResponseData> Run(
4848
return response;
4949
}
5050
}
51-
51+
#if !DEBUG
5252
/// <summary>
5353
/// Runs every five minutes to trigger a forced refresh of all evidence codes. This prevents disruption of calls to the HTTP endpoint,
5454
/// which will not block while updating.
@@ -60,4 +60,5 @@ public async Task Refresh([TimerTrigger("0 */5 * * * *")] TimerInfo timerInfo)
6060
{
6161
await _availableEvidenceCodesService.GetAvailableEvidenceCodes(true);
6262
}
63-
}
63+
#endif
64+
}

Dan.Core/Helpers/VersionHelper.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ public static VersionInfo GetVersionInfo()
1515
_versionInfo = new VersionInfo
1616
{
1717
Name = assembly.GetName().Name ?? "dancore",
18-
Built = GetBuildDate(assembly).ToString("u"),
19-
Commit = ThisAssembly.Git.Commit
18+
Built = GetBuildDate(assembly).ToString(@"yyyy-MM-dd\THH:mm:sszzz"),
19+
Commit = ThisAssembly.Git.Commit,
20+
CommitDate = ThisAssembly.Git.CommitDate
2021
};
2122

2223
return _versionInfo;

Dan.Core/Models/VersionInfo.cs

+3
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@ public class VersionInfo
1212

1313
[JsonProperty("commit")]
1414
public string Commit { get; set; } = string.Empty;
15+
16+
[JsonProperty("commitDate")]
17+
public string CommitDate { get; set; } = string.Empty;
1518
}

Dan.Core/Program.cs

+1-12
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
using Microsoft.Extensions.Hosting;
1919
using Microsoft.Extensions.Hosting.Internal;
2020
using Microsoft.Extensions.Logging;
21-
using Microsoft.Extensions.Logging.Console;
2221
using Newtonsoft.Json;
2322
using Polly;
2423
using Polly.Caching;
@@ -45,16 +44,7 @@
4544
})
4645
.ConfigureLogging((hostingContext, logging) =>
4746
{
48-
if (danHostingEnvironment.IsLocalDevelopment())
49-
{
50-
logging.AddSimpleConsole(options =>
51-
{
52-
options.ColorBehavior = LoggerColorBehavior.Enabled;
53-
options.SingleLine = true;
54-
options.IncludeScopes = false;
55-
});
56-
}
57-
47+
// This is required for the logging-options in worker-logging.json to ble applied
5848
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
5949
})
6050
.ConfigureFunctionsWorkerDefaults(builder =>
@@ -83,7 +73,6 @@
8373
})
8474
.ConfigureServices((_, services) =>
8575
{
86-
8776
// You will need extra configuration because above will only log per default Warning (default AI configuration). As this is a provider-specific
8877
// setting, it will override all non-provider (Logging:LogLevel)-based configurations.
8978
// https://github.com/microsoft/ApplicationInsights-dotnet/blob/main/NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs#L427

0 commit comments

Comments
 (0)