Skip to content

Commit

Permalink
Add option for dynamic logger [#14]
Browse files Browse the repository at this point in the history
  • Loading branch information
ccheetham committed May 24, 2021
1 parent 95e9b0c commit 0584d43
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
"longName": "docker",
"shortName": ""
},
"DynamicLogger": {
"longName": "dynamic-logger",
"shortName": ""
},
"Framework": {
"longName": "framework"
},
Expand Down
6 changes: 6 additions & 0 deletions src/DotNetNew.WebApi/CSharp/.template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@
"description": "Add Docker support.",
"defaultValue": "false"
},
"DynamicLogger": {
"type": "parameter",
"datatype": "bool",
"description": "Use dynamic logger.",
"defaultValue": "false"
},
"DockerImageTag": {
"type": "generated",
"generator": "switch",
Expand Down
14 changes: 7 additions & 7 deletions src/DotNetNew.WebApi/CSharp/Company.WebApplication1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="$(NetCoreApp21Version)" />
</ItemGroup>
<!--#endif -->
<!--#if (CloudConfigClient || CloudFoundryHosting || PlaceholderConfiguration || RandomValueConfiguration) -->
<!--#if (CloudConfigClient || CloudFoundryHosting || DynamicLogger || PlaceholderConfiguration || RandomValueConfiguration) -->

<ItemGroup>
<PackageReference Include="Steeltoe.Common.Hosting" Condition="'$(CloudFoundryHosting)'" Version="$(SteeltoeVersion)" />
<PackageReference Include="Steeltoe.Extensions.Configuration.CloudFoundryCore" Condition="'$(CloudFoundryHosting)'" Version="$(SteeltoeVersion)" />
<PackageReference Include="Steeltoe.Extensions.Configuration.ConfigServerCore" Condition="'$(CloudConfigClient)'" Version="$(SteeltoeVersion)" />
<PackageReference Include="Steeltoe.Extensions.Configuration.PlaceholderCore" Condition="'$(PlaceholderConfiguration)'" Version="$(SteeltoeVersion)" />
<PackageReference Include="Steeltoe.Extensions.Configuration.RandomValueBase" Condition="'$(RandomValueConfiguration)'" Version="$(SteeltoeVersion)" />
<PackageReference Include="Steeltoe.Extensions.Logging.DynamicLogger" Condition="'$(CloudFoundryHosting)'" Version="$(SteeltoeVersion)" />
<PackageReference Include="Steeltoe.Extensions.Configuration.CloudFoundryCore" Condition="'$(CloudFoundryHosting)' == 'True'" Version="$(SteeltoeVersion)" />
<PackageReference Include="Steeltoe.Extensions.Configuration.ConfigServerCore" Condition="'$(CloudConfigClient)' == 'True'" Version="$(SteeltoeVersion)" />
<PackageReference Include="Steeltoe.Extensions.Configuration.PlaceholderCore" Condition="'$(PlaceholderConfiguration)' == 'True'" Version="$(SteeltoeVersion)" />
<PackageReference Include="Steeltoe.Extensions.Configuration.RandomValueBase" Condition="'$(RandomValueConfiguration)' == 'True'" Version="$(SteeltoeVersion)" />
<PackageReference Include="Steeltoe.Extensions.Logging.DynamicLogger" Condition="'$(CloudFoundryHosting)' == 'True' OR '$(DynamicLogger)' == 'True'" Version="$(SteeltoeVersion)" />
</ItemGroup>
<!--#endif -->
<!--#if (AzureSpringCloudHosting && !FrameworkNetCoreApp21) -->

<ItemGroup>
<PackageReference Include="Microsoft.Azure.SpringCloud.Client" Condition="'$(AzureSpringCloudHosting)'" Version="$(MicrosoftAzureSpringCloudClientVersion)" />
<PackageReference Include="Microsoft.Azure.SpringCloud.Client" Condition="'$(AzureSpringCloudHosting)' == 'True'" Version="$(MicrosoftAzureSpringCloudClientVersion)" />
</ItemGroup>
<!--#endif -->

Expand Down
1 change: 1 addition & 0 deletions src/DotNetNew.WebApi/CSharp/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<AzureSpringCloudHosting>false</AzureSpringCloudHosting>
<CloudConfigClient>false</CloudConfigClient>
<CloudFoundryHosting>false</CloudFoundryHosting>
<DynamicLogger>false</DynamicLogger>
<FrameworkNet50>true</FrameworkNet50>
<FrameworkNetCoreApp21>false</FrameworkNetCoreApp21>
<MicrosoftAzureSpringCloudClientVersion>2.0.0-preview.1</MicrosoftAzureSpringCloudClientVersion>
Expand Down
16 changes: 16 additions & 0 deletions src/DotNetNew.WebApi/CSharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#if (AzureSpringCloudHosting && !FrameworkNetCoreApp21)
using Microsoft.Azure.SpringCloud.Client;
#endif
#if (DynamicLogger && FrameworkNetCoreApp21)
using Microsoft.Extensions.Logging;
#endif
#if (CloudFoundryHosting)
using Steeltoe.Common.Hosting;
using Steeltoe.Extensions.Configuration.CloudFoundry;
Expand All @@ -22,6 +25,9 @@
using Steeltoe.Extensions.Configuration.Placeholder;
#endif
#endif
#if (DynamicLogger)
using Steeltoe.Extensions.Logging;
#endif

namespace Company.WebApplication1
{
Expand Down Expand Up @@ -49,6 +55,13 @@ public static IWebHostBuilder CreateWebHostBuilder(string[] args)
#if (CloudFoundryHosting)
.UseCloudHosting()
.AddCloudFoundryConfiguration()
#endif
#if (DynamicLogger)
.ConfigureLogging((hostingContext, loggingBuilder) =>
{
loggingBuilder.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
loggingBuilder.AddDynamicConsole();
})
#endif
.UseStartup<Startup>();
return builder;
Expand All @@ -68,6 +81,9 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
#endif
#if (CloudConfigClient)
.AddConfigServer()
#endif
#if (DynamicLogger)
.ConfigureLogging((context, builder) => builder.AddDynamicConsole())
#endif
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
#endif
Expand Down
73 changes: 73 additions & 0 deletions test/DotNetNew.SteeltoeWebApi.Test/DynamicLoggerOptionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using FluentAssertions;
using Steeltoe.DotNetNew.Test.Utilities.Assertions;
using Xunit;
using Xunit.Abstractions;

namespace Steeltoe.DotNetNew.SteeltoeWebApi.Test
{
public class DynamicLoggerOptionTest : Test
{
public DynamicLoggerOptionTest(ITestOutputHelper logger) : base("dynamic-logger", logger)
{
}

[Fact]
public override async void TestHelp()
{
using var sandbox = await TemplateSandbox("--help");
sandbox.CommandOutput.Should().ContainSnippet(@"
--dynamic-logger Use dynamic logger.
bool - Optional
Default: false
");
}

[Fact]
public async void TestCsproj()
{
using var sandbox = await TemplateSandbox();
var xDoc = await sandbox.GetXmlDocumentAsync($"{sandbox.Name}.csproj");
var expectedPackageRefs = new List<string>
{
"Steeltoe.Extensions.Logging.DynamicLogger",
};
var actualPackageRefs =
(
from e in xDoc.Elements().Elements("ItemGroup").Elements("PackageReference").Attributes("Include")
select e.Value
).ToList();
actualPackageRefs.Should().Contain(expectedPackageRefs);
}

[Fact]
public async void TestProgramCs()
{
using var sandbox = await TemplateSandbox();
var programSource = await sandbox.GetFileTextAsync("Program.cs");
programSource.Should().ContainSnippet("using Steeltoe.Extensions.Logging;");
programSource.Should()
.ContainSnippet(".ConfigureLogging((context, builder) => builder.AddDynamicConsole())");
}

[Fact]
public async void TestProgramCsNetCoreApp21()
{
using var sandbox = await TemplateSandbox("--framework netcoreapp2.1");
var programSource = await sandbox.GetFileTextAsync("Program.cs");
programSource.Should().ContainSnippet("using Steeltoe.Extensions.Logging;");
programSource.Should()
.ContainSnippet(
"loggingBuilder.AddConfiguration(hostingContext.Configuration.GetSection(\"Logging\"));");
programSource.Should().ContainSnippet(@"
.ConfigureLogging((hostingContext, loggingBuilder) =>
{
loggingBuilder.AddConfiguration(hostingContext.Configuration.GetSection(""Logging""));
loggingBuilder.AddDynamicConsole();
})
");
}
}
}

0 comments on commit 0584d43

Please sign in to comment.