Skip to content

Commit

Permalink
Merge pull request #36 from nblumhardt/netstd-2
Browse files Browse the repository at this point in the history
Re-enable .NET Framework (.NET Standard 2.0) support by excluding CreateBootstrapLogger()/ReloadableLogger on that platform
  • Loading branch information
nblumhardt authored Feb 23, 2021
2 parents e5c7464 + 38ef5f9 commit 14a5432
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"sdk": {
"allowPrerelease": false,
"version": "3.1.100",
"version": "5.0.102",
"rollForward": "latestFeature"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if !NO_RELOADABLE_LOGGER

using System;
using System.Collections.Generic;
using System.Threading;
Expand Down Expand Up @@ -516,4 +518,6 @@ public bool BindProperty(string propertyName, object value, bool destructureObje
return canBind;
}
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if !NO_RELOADABLE_LOGGER

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -667,3 +669,5 @@ internal bool CreateChild(
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if !NO_RELOADABLE_LOGGER

using Microsoft.Extensions.Hosting;
using Serilog.Extensions.Hosting;
using System;
Expand All @@ -38,3 +40,5 @@ public static ReloadableLogger CreateBootstrapLogger(this LoggerConfiguration lo
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

<PropertyGroup>
<Description>Serilog support for .NET Core logging in hosted services</Description>
<VersionPrefix>4.0.1</VersionPrefix>
<VersionPrefix>4.1.0</VersionPrefix>
<Authors>Microsoft;Serilog Contributors</Authors>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<LangVersion>8</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>Serilog.Extensions.Hosting</AssemblyName>
Expand All @@ -22,6 +23,10 @@
<RootNamespace>Serilog</RootNamespace>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<DefineConstants>$(DefineConstants);NO_RELOADABLE_LOGGER</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
Expand Down
10 changes: 8 additions & 2 deletions src/Serilog.Extensions.Hosting/SerilogHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ public static IHostBuilder UseSerilog(
/// <param name="writeToProviders">By default, Serilog does not write events to <see cref="ILoggerProvider"/>s registered through
/// the Microsoft.Extensions.Logging API. Normally, equivalent Serilog sinks are used in place of providers. Specify
/// <c>true</c> to write events to all providers.</param>
/// <remarks>If the static <see cref="Log.Logger"/> is a bootstrap logger (created using
/// <see cref="LoggerConfigurationExtensions.CreateBootstrapLogger"/>), and <paramref name="preserveStaticLogger"/> is
/// <remarks>If the static <see cref="Log.Logger"/> is a bootstrap logger (see
/// <c>LoggerConfigurationExtensions.CreateBootstrapLogger()</c>), and <paramref name="preserveStaticLogger"/> is
/// not specified, the the bootstrap logger will be reconfigured through the supplied delegate, rather than being
/// replaced entirely or ignored.</remarks>
/// <returns>The host builder.</returns>
Expand All @@ -140,8 +140,12 @@ public static IHostBuilder UseSerilog(
if (configureLogger == null) throw new ArgumentNullException(nameof(configureLogger));

// This check is eager; replacing the bootstrap logger after calling this method is not supported.
#if !NO_RELOADABLE_LOGGER
var reloadable = Log.Logger as ReloadableLogger;
var useReload = reloadable != null && !preserveStaticLogger;
#else
const bool useReload = false;
#endif

builder.ConfigureServices((context, collection) =>
{
Expand All @@ -154,6 +158,7 @@ public static IHostBuilder UseSerilog(
collection.AddSingleton(services =>
{
ILogger logger;
#if !NO_RELOADABLE_LOGGER
if (useReload)
{
reloadable!.Reload(cfg =>
Expand All @@ -168,6 +173,7 @@ public static IHostBuilder UseSerilog(
logger = reloadable.Freeze();
}
else
#endif
{
var loggerConfiguration = new LoggerConfiguration();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Xunit;
#if !NO_RELOADABLE_LOGGER

using Xunit;

namespace Serilog.Extensions.Hosting.Tests
{
Expand All @@ -20,3 +22,5 @@ public void AFrozenLoggerYieldsSerilogLoggers()
}
}
}

#endif
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>netcoreapp3.1;net5.0;net4.8</TargetFrameworks>
<AssemblyName>Serilog.Extensions.Hosting.Tests</AssemblyName>
<AssemblyOriginatorKeyFile>../../assets/Serilog.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
Expand All @@ -10,6 +10,10 @@
<LangVersion>latest</LangVersion>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net4.8' ">
<DefineConstants>$(DefineConstants);NO_RELOADABLE_LOGGER</DefineConstants>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Serilog.Extensions.Hosting\Serilog.Extensions.Hosting.csproj" />
</ItemGroup>
Expand Down

0 comments on commit 14a5432

Please sign in to comment.