Skip to content

Commit

Permalink
Merge pull request #72 from AkkaNetContrib/dev
Browse files Browse the repository at this point in the history
v1.4.5 Release
  • Loading branch information
Aaronontheweb authored May 4, 2020
2 parents e0253cb + 2dc585c commit 9be586c
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 54 deletions.
24 changes: 10 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,17 @@ This is the NLog integration plugin for Akka.NET.
### Configuration via code
```C#
// Step 1. Create configuration object
var config = new LoggingConfiguration();
var config = new NLog.Config.LoggingConfiguration();

// Step 2. Create targets and add them to the configuration
var consoleTarget = new ColoredConsoleTarget();
config.AddTarget("console", consoleTarget);
// Step 2. Create targets and configure properties
var logconsole = new NLog.Targets.ConsoleTarget("logconsole");
logconsole.Layout = @"${date:format=HH\:mm\:ss} ${level} ${logger} ${message}";

// Step 3. Set target properties
consoleTarget.Layout = @"${date:format=HH\:mm\:ss} ${logger} ${message}";

// Step 4. Define rules
var rule1 = new LoggingRule("*", LogLevel.Debug, consoleTarget);
config.LoggingRules.Add(rule1);

// Step 5. Activate the configuration
LogManager.Configuration = config;
// Step 3. Define filtering rules
config.AddRule(LogLevel.Debug, LogLevel.Fatal, logconsole);

// Step 4. Activate the configuration
NLog.LogManager.Configuration = config;

Config myConfig = @"akka.loglevel = DEBUG
akka.loggers=[""Akka.Logger.NLog.NLogLogger, Akka.Logger.NLog""]";
Expand All @@ -36,7 +32,7 @@ Add NLog.config file to your project
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="console" xsi:type="Console" layout="[${logger}] [${level:uppercase=true}] : ${message}"/>
<target name="console" xsi:type="Console" layout="[${logger}] [${level:uppercase=true}] [${event-properties:item=logSource}] [${event-properties:item=actorPath}] [${event-properties:item=threadId:format=D4}] : ${message}"/>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="console"/>
Expand Down
16 changes: 13 additions & 3 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
#### 1.3.5 Januari 12 2020 ####
#### 1.4.5 May 4 2020 ####

- Update Akka to 1.4.5
- Update NLog to 4.7.0

#### 1.4.2 March 14 2020 ####

Updated to Akka 1.4.2
Changed TargetFramework to NetStandard2.0

#### 1.3.5 January 28 2020 ####

Changed TargetFramework from net462 to net452

#### 1.3.4 Januari 12 2020 ####
#### 1.3.4 January 12 2020 ####

Update to Akka 1.3.17
Update to NLog 4.5.11
Expand All @@ -27,7 +37,7 @@ Support for Akka 1.3.0 and .NET Core
Support for Akka 1.2.0
Updated to NLog 4.4.6

#### 1.1.3 Januari 26 2017 ####
#### 1.1.3 January 26 2017 ####

Support for Akka 1.1.3
Updated to NLog 4.3.2
Expand Down
2 changes: 1 addition & 1 deletion build-system/windows-pr-validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ jobs:
- template: azure-pipeline.template.yaml
parameters:
name: Windows
vmImage: 'vs2017-win2016'
vmImage: 'windows-2019'
scriptFileName: build.cmd
scriptArgs: all
2 changes: 1 addition & 1 deletion src/Akka.Logger.NLog.Tests/Akka.Logger.NLog.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitVersion)" />
<PackageReference Include="xunit" Version="$(XunitVersion)" />
<PackageReference Include="Akka.TestKit.Xunit2" Version="1.3.17" />
<PackageReference Include="Akka.TestKit.Xunit2" Version="1.4.5" />
</ItemGroup>

<ItemGroup>
Expand Down
55 changes: 42 additions & 13 deletions src/Akka.Logger.NLog.Tests/NLogFormattingSpecs.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
using System;
using System.Linq;
using System.Threading;
using Akka.Actor;
using Akka.Configuration;
using Akka.Event;
using Xunit;
using Xunit.Abstractions;
using LogLevel = Akka.Event.LogLevel;

namespace Akka.Logger.Serilog.Tests
namespace Akka.Logger.NLog.Tests
{
public class NLogFormattingSpecs : TestKit.Xunit2.TestKit
{
public static readonly Config Config = @"akka.loglevel = DEBUG";
private static readonly Config Config = @"akka.loglevel = DEBUG";

private readonly ILoggingAdapter _loggingAdapter;
private readonly global::NLog.Targets.MemoryTarget _loggingTarget = new global::NLog.Targets.MemoryTarget() { Layout = "${level}|${message}" };
const string ActorSystemName = "my-test-system";

public NLogFormattingSpecs(ITestOutputHelper helper) : base(Config, output: helper)
{
Config myConfig = @"akka.loglevel = DEBUG
akka.loggers=[""Akka.Logger.NLog.NLogLogger, Akka.Logger.NLog""]";

var system = ActorSystem.Create("my-test-system", myConfig);
var system = ActorSystem.Create(ActorSystemName, myConfig);

_loggingAdapter = Logging.GetLogger(system.EventStream, system.Name);

global::NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(_loggingTarget);

Sys.EventStream.Subscribe(TestActor, typeof(LogEvent));
}

Expand All @@ -38,19 +37,49 @@ public NLogFormattingSpecs(ITestOutputHelper helper) : base(Config, output: help
[InlineData(LogLevel.ErrorLevel, "test case {c}", new object[] { 3.0 }, "Error|test case 3")]
public void LoggingTest(LogLevel level, string formatStr, object[] formatArgs, string resultStr)
{
_loggingTarget.Logs.Clear();
var loggingTarget = new global::NLog.Targets.MemoryTarget { Layout = "${level}|${message}" };
global::NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(loggingTarget);

loggingTarget.Logs.Clear();
_loggingAdapter.Log(level, formatStr, formatArgs);

for (var i = 0; i < 100; ++i)
{
if (loggingTarget.Logs.Count != 0)
break;

Thread.Sleep(10);
}

Assert.NotEmpty(loggingTarget.Logs);
Assert.Equal(resultStr, loggingTarget.Logs.Last());
}

[Theory]
[InlineData(LogLevel.InfoLevel, "test case {0}", new object[] { 1 }, "{0}|{1}|test case 1")]
public void LoggingTestWithEventProperties(LogLevel level, string formatStr, object[] formatArgs, string resultStr)
{
_loggingAdapter.Log(level, formatStr, formatArgs);
var loggingTarget = new global::NLog.Targets.MemoryTarget
{Layout = "${event-properties:item=logSource}|${event-properties:item=threadId:format=D4}|${message}" };
global::NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(loggingTarget);

loggingTarget.Logs.Clear();
_loggingAdapter.Log(level, formatStr, formatArgs);

for (int i = 0; i < 100; ++i)
for (var i = 0; i < 100; ++i)
{
if (_loggingTarget.Logs.Count != 0)
if (loggingTarget.Logs.Count != 0)
break;

System.Threading.Thread.Sleep(10);
Thread.Sleep(10);
}

Assert.NotEmpty(_loggingTarget.Logs);
Assert.Equal(resultStr, _loggingTarget.Logs.Last());
var formattedResultString = string.Format(resultStr, ActorSystemName,
Thread.CurrentThread.ManagedThreadId.ToString().PadLeft(4, '0'));

Assert.NotEmpty(loggingTarget.Logs);
Assert.Equal(formattedResultString, loggingTarget.Logs.Last());
}
}
}
6 changes: 3 additions & 3 deletions src/Akka.Logger.NLog/Akka.Logger.NLog.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\common.props" />
<PropertyGroup>
<TargetFrameworks>net452;netstandard1.6</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<Description>NLog logging adapter for Akka.NET.</Description>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Akka" Version="1.3.17" />
<PackageReference Include="NLog" Version="4.5.11" />
<PackageReference Include="Akka" Version="1.4.5" />
<PackageReference Include="NLog" Version="4.7.0" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Akka.Logger.NLog/NLog.Example.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="console" xsi:type="Console" layout="[${logger}] [${level:uppercase=true}] : ${message}"/>
<target name="logFile" xsi:type="File" fileName="log.txt" layout="[${longdate}] [${logger}] ${level:uppercase=true}] : ${event-properties:SourceContext} ${message} ${exception:format=tostring}" />
<target name="logFile" xsi:type="File" fileName="log.txt" layout="[${longdate}] [${logger}] ${level:uppercase=true}] : ${event-properties:actorPath} ${message} ${exception:format=tostring}" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="console"/>
Expand Down
30 changes: 14 additions & 16 deletions src/Akka.Logger.NLog/NLogLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,31 @@ private static void Log(LogEvent logEvent, Action<NLogger, LogEvent> logStatemen
/// </summary>
public NLogLogger()
{
Receive<Error>(m => Log(m, (logger, logEvent) => LogEvent(logger, NLogLevel.Error, logEvent.LogSource, m.Cause, logEvent.Message)));
Receive<Warning>(m => Log(m, (logger, logEvent) => LogEvent(logger, NLogLevel.Warn, logEvent.LogSource, logEvent.Message)));
Receive<Info>(m => Log(m, (logger, logEvent) => LogEvent(logger, NLogLevel.Info, logEvent.LogSource, logEvent.Message)));
Receive<Debug>(m => Log(m, (logger, logEvent) => LogEvent(logger, NLogLevel.Debug, logEvent.LogSource, logEvent.Message)));
Receive<Error>(m => Log(m, (logger, logEvent) => LogEvent(logger, NLogLevel.Error, logEvent.Cause, logEvent)));
Receive<Warning>(m => Log(m, (logger, logEvent) => LogEvent(logger, NLogLevel.Warn, logEvent.Cause, logEvent)));
Receive<Info>(m => Log(m, (logger, logEvent) => LogEvent(logger, NLogLevel.Info, logEvent.Cause, logEvent)));
Receive<Debug>(m => Log(m, (logger, logEvent) => LogEvent(logger, NLogLevel.Debug, logEvent.Cause, logEvent)));
Receive<InitializeLogger>(m =>
{
_log.Info("NLogLogger started");
Sender.Tell(new LoggerInitialized());
});
}

private static void LogEvent(NLogger logger, NLogLevel level, string logSource, object message)
{
LogEvent(logger, level, logSource, null, message);
}

private static void LogEvent(NLogger logger, NLogLevel level, string logSource, Exception exception, object message)
private static void LogEvent(NLogger logger, NLogLevel level, Exception exception, LogEvent logEvent)
{
if (logger.IsEnabled(level))
{
LogMessage logMessage = message as LogMessage;
var logEvent = (logMessage != null && logMessage.Args?.Length > 0) ?
LogMessage logMessage = logEvent.Message as LogMessage;
var logEventInfo = (logMessage != null && logMessage.Args?.Length > 0) ?
new LogEventInfo(level, logger.Name, null, logMessage.Format, logMessage.Args, exception) :
new LogEventInfo(level, logger.Name, null, "{0}", new[] { message }, exception);
logEvent.Properties["logSource"] = logSource; // TODO logSource is the same as logger.Name, now adding twice
logEvent.Properties["SourceContext"] = Context?.Sender?.Path?.ToString() ?? string.Empty; // Same as Serilog
logger.Log(logEvent);
new LogEventInfo(level, logger.Name, null, "{0}", new[] { logEvent.Message.ToString() }, exception);
if (logEventInfo.TimeStamp.Kind == logEvent.Timestamp.Kind)
logEventInfo.TimeStamp = logEvent.Timestamp; // Timestamp of original LogEvent (instead of async Logger thread timestamp)
logEventInfo.Properties["logSource"] = logEvent.LogSource;
logEventInfo.Properties["actorPath"] = Context?.Sender?.Path?.ToString() ?? string.Empty; // Same as Serilog
logEventInfo.Properties["threadId"] = logEvent.Thread.ManagedThreadId; // ThreadId of the original LogEvent (instead of async Logger threadid)
logger.Log(logEventInfo);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
<PackageTags>akka;actors;actor model;Akka;concurrency;nlog</PackageTags>
<Copyright>Copyright © 2013-2020 Akka.NET Contrib Team</Copyright>
<Authors>Akka.NET Contrib Team</Authors>
<PackageReleaseNotes>Dependency update for Akka 1.3.17 and NLog 4.5.11 for net452</PackageReleaseNotes>
<VersionPrefix>1.3.0</VersionPrefix>
<PackageReleaseNotes>- Update Akka to 1.4.5
- Update NLog to 4.7.0</PackageReleaseNotes>
<VersionPrefix>1.4.5</VersionPrefix>
<PackageIconUrl>https://getakka.net/images/akkalogo.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/AkkaNetContrib/Akka.Logger.Nlog</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/AkkaNetContrib/Akka.Logger.Nlog/blob/master/LICENSE</PackageLicenseUrl>
Expand Down

0 comments on commit 9be586c

Please sign in to comment.