Skip to content

Commit

Permalink
Merge pull request #56 from akkadotnet/dev
Browse files Browse the repository at this point in the history
Akka.Logger.Serilog v1.3.9 Release
  • Loading branch information
Aaronontheweb authored Aug 23, 2018
2 parents 697c12c + a3f9f3c commit 3038518
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This is the Serilog integration plugin for Akka.NET. Please check out our [documentation](http://getakka.net/articles/utilities/serilog.html) on how to get the most out of this plugin.

Targets Serilog 2.6.0.
Targets Serilog 2.7.1.

### Semantic Logging Syntax
If you intend on using any of the Serilog semantic logging formats in your logging strings, __you need to use the SerilogLoggingAdapter__ inside your instrumented code or there could be elsewhere inside parts of your `ActorSystem`:
Expand Down
5 changes: 5 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#### 1.3.9 August 23 2018 ####
* [Fixed: Regression: ForContext API doesn't apply changes](https://github.com/akkadotnet/Akka.Logger.Serilog/issues/51)
* Upgraded to Akka.NET v1.3.9.
* Upgraded to Serilog v2.7.1.

#### 1.3.6 April 28 2018 ####
* Restored `SerilogLogMessageFormatter` in order to fix [Bug: `LogEvent.ToString()` blows up when using Serilog semantic formatting](https://github.com/akkadotnet/Akka.Logger.Serilog/issues/43).
* Upgraded to [Akka.NET v1.3.6](https://github.com/akkadotnet/akka.net/releases/tag/v1.3.6).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Akka.TestKit.Xunit2" Version="1.3.6" />
<PackageReference Include="Akka.TestKit.Xunit2" Version="1.3.9" />
<PackageReference Include="FluentAssertions" Version="5.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
<PackageReference Include="Serilog.Sinks.ColoredConsole" Version="3.0.1" />
Expand Down
120 changes: 120 additions & 0 deletions src/Akka.Logger.Serilog.Tests/ForContextSpecs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Configuration;
using Akka.Event;
using FluentAssertions;
using Serilog;
using Serilog.Events;
using Xunit;
using Xunit.Abstractions;
using LogEvent = Akka.Event.LogEvent;

namespace Akka.Logger.Serilog.Tests
{
public class ForContextSpecs : TestKit.Xunit2.TestKit
{
public static readonly Config Config = @"akka.loglevel = DEBUG
akka.loggers=[""Akka.Logger.Serilog.SerilogLogger, Akka.Logger.Serilog""]";
private ILoggingAdapter _loggingAdapter;
private readonly TestSink _sink = new TestSink();

public ForContextSpecs(ITestOutputHelper helper) : base(Config, output: helper)
{
global::Serilog.Log.Logger = new LoggerConfiguration()
.WriteTo.Sink(_sink)
.MinimumLevel.Information()
.CreateLogger();

var logSource = Sys.Name;
var logClass = typeof(ActorSystem);

_loggingAdapter = new SerilogLoggingAdapter(Sys.EventStream, logSource, logClass);
}

[Fact]
public void ShouldPassAlongAdditionalContext()
{
var traceId = Guid.NewGuid();
var spanId = Guid.NewGuid();
var context1 = _loggingAdapter.ForContext("traceId", traceId);
var context2 = context1.ForContext("spanId", spanId);

_sink.Clear();
AwaitCondition(() => _sink.Writes.Count == 0);

context1.Info("hi");
AwaitCondition(() => _sink.Writes.Count == 1);

_sink.Writes.TryDequeue(out var logEvent).Should().BeTrue();
logEvent.Level.Should().Be(LogEventLevel.Information);
logEvent.Properties.ContainsKey("traceId").Should().BeTrue();
logEvent.Properties["traceId"].ToString().Should().BeEquivalentTo(traceId.ToString());

_sink.Clear();
AwaitCondition(() => _sink.Writes.Count == 0);

context2.Info("bye");

AwaitCondition(() => _sink.Writes.Count == 1);

_sink.Writes.TryDequeue(out var logEvent2).Should().BeTrue();

logEvent2.Level.Should().Be(LogEventLevel.Information);

// needs to still have the context from context1
logEvent2.Properties.ContainsKey("traceId").Should().BeTrue();
logEvent2.Properties["traceId"].ToString().Should().BeEquivalentTo(traceId.ToString());

// and its own context from context2
logEvent2.Properties.ContainsKey("spanId").Should().BeTrue();
logEvent2.Properties["spanId"].ToString().Should().BeEquivalentTo(spanId.ToString());
}

[Fact]
public void ShouldPassAlongAdditionalContextWorkaround()
{
var traceId = Guid.NewGuid();
var spanId = Guid.NewGuid();
var context1 = (SerilogLoggingAdapter)_loggingAdapter.ForContext("traceId", traceId);
var context2 = (SerilogLoggingAdapter)context1.ForContext("spanId", spanId);

_sink.Clear();
AwaitCondition(() => _sink.Writes.Count == 0);

context1.Info("hi");
AwaitCondition(() => _sink.Writes.Count == 1);

_sink.Writes.TryDequeue(out var logEvent).Should().BeTrue();
logEvent.Level.Should().Be(LogEventLevel.Information);
logEvent.Properties.ContainsKey("traceId").Should().BeTrue();
logEvent.Properties["traceId"].ToString().Should().BeEquivalentTo(traceId.ToString());

_sink.Clear();
AwaitCondition(() => _sink.Writes.Count == 0);

context2.Info("bye");

AwaitCondition(() => _sink.Writes.Count == 1);

_sink.Writes.TryDequeue(out var logEvent2).Should().BeTrue();

logEvent2.Level.Should().Be(LogEventLevel.Information);

// needs to still have the context from context1
logEvent2.Properties.ContainsKey("traceId").Should().BeTrue();
logEvent2.Properties["traceId"].ToString().Should().BeEquivalentTo(traceId.ToString());

// and its own context from context2
logEvent2.Properties.ContainsKey("spanId").Should().BeTrue();
logEvent2.Properties["spanId"].ToString().Should().BeEquivalentTo(spanId.ToString());

}
}
}



28 changes: 28 additions & 0 deletions src/Akka.Logger.Serilog.Tests/TestSink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections.Concurrent;
using Serilog.Core;
using Serilog.Events;

namespace Akka.Logger.Serilog.Tests
{
/// <inheritdoc />
/// <summary>
/// Basic concurrent sink implementation for testing the final output from Serilog
/// </summary>
public sealed class TestSink : ILogEventSink
{
public ConcurrentQueue<global::Serilog.Events.LogEvent> Writes { get; private set; } = new ConcurrentQueue<global::Serilog.Events.LogEvent>();

/// <summary>
/// Resets the contents of the queue
/// </summary>
public void Clear()
{
Writes = new ConcurrentQueue<LogEvent>();
}

public void Emit(global::Serilog.Events.LogEvent logEvent)
{
Writes.Enqueue(logEvent);
}
}
}
4 changes: 2 additions & 2 deletions src/Akka.Logger.Serilog/Akka.Logger.Serilog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Akka" Version="1.3.6" />
<PackageReference Include="Serilog" Version="2.6.0" />
<PackageReference Include="Serilog" Version="2.7.1" />
<PackageReference Include="Akka" Version="1.3.9" />
</ItemGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
Expand Down
18 changes: 9 additions & 9 deletions src/Akka.Logger.Serilog/SerilogLoggingAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private SerilogLoggingAdapter(LoggingBus bus, string logSource, Type logClass, C
/// </summary>
/// <param name="format">The message that is being logged.</param>
/// <param name="args">An optional list of items used to format the message.</param>
public new void Debug(string format, params object[] args)
public override void Debug(string format, params object[] args)
{
base.Debug(format, BuildArgs(args));
}
Expand All @@ -52,17 +52,17 @@ private SerilogLoggingAdapter(LoggingBus bus, string logSource, Type logClass, C
/// </summary>
/// <param name="format">The message that is being logged.</param>
/// <param name="args">An optional list of items used to format the message.</param>
public new void Info(string format, params object[] args)
public override void Info(string format, params object[] args)
{
base.Info(format, BuildArgs(args));
}

/// <summary>
/// Obsolete. Use <see cref="M:Akka.Event.ILoggingAdapter.Warning(System.String,System.Object[])" /> instead!
/// </summary>
/// <param name="format">N/A</param>
/// <param name="args">N/A</param>
public new void Warn(string format, params object[] args)
/// <param name="format">The message that is being logged.</param>
/// <param name="args">An optional list of items used to format the message.</param>
public override void Warn(string format, params object[] args)
{
base.Warning(format, BuildArgs(args));
}
Expand All @@ -72,7 +72,7 @@ private SerilogLoggingAdapter(LoggingBus bus, string logSource, Type logClass, C
/// </summary>
/// <param name="format">The message that is being logged.</param>
/// <param name="args">An optional list of items used to format the message.</param>
public new void Warning(string format, params object[] args)
public override void Warning(string format, params object[] args)
{
base.Warning(format, BuildArgs(args));
}
Expand All @@ -82,7 +82,7 @@ private SerilogLoggingAdapter(LoggingBus bus, string logSource, Type logClass, C
/// </summary>
/// <param name="format">The message that is being logged.</param>
/// <param name="args">An optional list of items used to format the message.</param>
public new void Error(string format, params object[] args)
public override void Error(string format, params object[] args)
{
base.Error(format, BuildArgs(args));
}
Expand All @@ -93,7 +93,7 @@ private SerilogLoggingAdapter(LoggingBus bus, string logSource, Type logClass, C
/// <param name="cause">The exception associated with this message.</param>
/// <param name="format">The message that is being logged.</param>
/// <param name="args">An optional list of items used to format the message.</param>
public new void Error(Exception cause, string format, params object[] args)
public override void Error(Exception cause, string format, params object[] args)
{
base.Error(cause, format, BuildArgs(args));
}
Expand All @@ -102,7 +102,7 @@ private SerilogLoggingAdapter(LoggingBus bus, string logSource, Type logClass, C
/// <param name="logLevel">The level used to log the message.</param>
/// <param name="format">The message that is being logged.</param>
/// <param name="args">An optional list of items used to format the message.</param>
public new void Log(LogLevel logLevel, string format, params object[] args)
public override void Log(LogLevel logLevel, string format, params object[] args)
{
base.Log(logLevel, format, BuildArgs(args));
}
Expand Down
3 changes: 1 addition & 2 deletions src/Akka.Logger.Serilog/SerilogLoggingAdapterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public static class SerilogLoggingAdapterExtensions
/// <param name="destructureObjects">If true, the value will be serialized as a structured object if possible; if false, the object will be recorded as a scalar or simple array.</param>
public static ILoggingAdapter ForContext(this ILoggingAdapter adapter, string propertyName, object value, bool destructureObjects = false)
{
var customAdapter = adapter as SerilogLoggingAdapter;
return customAdapter == null ? adapter : customAdapter.SetContextProperty(propertyName, value, destructureObjects);
return !(adapter is SerilogLoggingAdapter customAdapter) ? adapter : customAdapter.SetContextProperty(propertyName, value, destructureObjects);
}

/// <summary>
Expand Down
13 changes: 4 additions & 9 deletions src/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
<PackageTags>akka;actors;actor model;Akka;concurrency;serilog</PackageTags>
<Copyright>Copyright © 2013-2018 Akka.NET Team</Copyright>
<Authors>Akka.NET Team</Authors>
<PackageReleaseNotes>Restored `SerilogLogMessageFormatter` in order to fix [Bug: `LogEvent.ToString()` blows up when using Serilog semantic formatting](https://github.com/akkadotnet/Akka.Logger.Serilog/issues/43).
Upgraded to [Akka.NET v1.3.6](https://github.com/akkadotnet/akka.net/releases/tag/v1.3.6).
If you intend on using any of the Serilog semantic logging formats in your logging strings, __you need to use the SerilogLoggingAdapter__ inside your instrumented code or there could be elsewhere inside parts of your `ActorSystem`:
```csharp
var log = Context.GetLogger&lt;SerilogLoggingAdapter&gt;(); // correct
log.Info("My boss makes me use {semantic} logging", "semantic"); // serilog semantic logging format
```
This will allow all logging events to be consumed anywhere inside the `ActorSystem`, including places like the Akka.NET TestKit, without throwing `FormatException`s when they encounter semantic logging syntax outside of the `SerilogLogger`.</PackageReleaseNotes>
<VersionPrefix>1.3.6</VersionPrefix>
<PackageReleaseNotes>[Fixed: Regression: ForContext API doesn't apply changes](https://github.com/akkadotnet/Akka.Logger.Serilog/issues/51)
Upgraded to Akka.NET v1.3.9.
Upgraded to Serilog v2.7.1.</PackageReleaseNotes>
<VersionPrefix>1.3.9</VersionPrefix>
<PackageIconUrl>http://getakka.net/images/akkalogo.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/akkadotnet/Akka.Logger.Serilog</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/akkadotnet/Akka.Logger.Serilog/blob/master/LICENSE</PackageLicenseUrl>
Expand Down

0 comments on commit 3038518

Please sign in to comment.