Skip to content

4.1.1 Release #41

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

Merged
merged 3 commits into from
Mar 2, 2021
Merged
Show file tree
Hide file tree
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
20 changes: 6 additions & 14 deletions samples/SimpleServiceSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,14 @@

namespace SimpleServiceSample
{
public class Program
public static class Program
{
public static Action<IConfigurationBuilder> BuildConfiguration =
builder => builder
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
.AddEnvironmentVariables();

public static int Main(string[] args)
{
var builder = new ConfigurationBuilder();
BuildConfiguration(builder);

Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Build())
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateLogger();
.CreateBootstrapLogger();

try
{
Expand All @@ -47,6 +36,9 @@ public static int Main(string[] args)
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices(services => services.AddHostedService<PrintTimeService>())
.UseSerilog();
.UseSerilog((context, services, loggerConfiguration) => loggerConfiguration
.ReadFrom.Configuration(context.Configuration)
.Enrich.FromLogContext()
.WriteTo.Console());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ public ILogger ForContext<TSource>()
{
if (_frozen)
return _cached.ForContext<TSource>();



if (_reloadableLogger.CreateChild(
_root,
this,
Expand Down Expand Up @@ -171,12 +170,15 @@ void Update(ILogger newRoot, ILogger newCached, bool frozen)
{
_root = newRoot;
_cached = newCached;
_frozen = frozen;

// https://github.com/dotnet/runtime/issues/20500#issuecomment-284774431
// Publish `_cached` and `_frozen`. This is useful here because it means that once the logger is frozen - which
// Publish `_cached` and then `_frozen`. This is useful here because it means that once the logger is frozen - which
// we always expect - reads don't require any synchronization/interlocked instructions.
Interlocked.MemoryBarrierProcessWide();

_frozen = frozen;

Interlocked.MemoryBarrierProcessWide();
}

public void Write(LogEvent logEvent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public Logger Freeze()
throw new InvalidOperationException("The logger is already frozen.");

_frozen = true;

// https://github.com/dotnet/runtime/issues/20500#issuecomment-284774431
// Publish `_logger` and `_frozen`. This is useful here because it means that once the logger is frozen - which
// we always expect - reads don't require any synchronization/interlocked instructions.
Expand Down Expand Up @@ -368,12 +368,21 @@ public bool BindProperty(string propertyName, object value, bool destructureObje
[MethodImpl(MethodImplOptions.AggressiveInlining)]
(ILogger, bool) UpdateForCaller(ILogger root, ILogger cached, IReloadableLogger caller, out ILogger newRoot, out ILogger newCached, out bool frozen)
{
if (_frozen)
{
// If we're frozen, then the caller hasn't observed this yet and should update.
newRoot = _logger;
newCached = caller.ReloadLogger();
frozen = true;
return (newCached, true);
}

if (cached != null && root == _logger)
{
newRoot = default;
newCached = default;
frozen = _frozen;
return (cached, frozen); // If we're frozen, then the caller hasn't observed this yet and should update.
frozen = false;
return (cached, false);
}

newRoot = _logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Description>Serilog support for .NET Core logging in hosted services</Description>
<VersionPrefix>4.1.0</VersionPrefix>
<VersionPrefix>4.1.1</VersionPrefix>
<Authors>Microsoft;Serilog Contributors</Authors>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<LangVersion>8</LangVersion>
Expand Down
13 changes: 13 additions & 0 deletions test/Serilog.Extensions.Hosting.Tests/ReloadableLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ public void AFrozenLoggerYieldsSerilogLoggers()
nested = contextual.ForContext("test", "test");
Assert.IsType<Core.Logger>(nested);
}

[Fact]
public void CachingReloadableLoggerRemainsUsableAfterFreezing()
{
var logger = new LoggerConfiguration().CreateBootstrapLogger();
var contextual = logger.ForContext<ReloadableLoggerTests>();
contextual.Information("First");
logger.Reload(c => c);
logger.Freeze();
contextual.Information("Second");
contextual.Information("Third");
contextual.Information("Fourth"); // No crash :-)
}
}
}

Expand Down