Skip to content

Commit

Permalink
Merge pull request #81 from adamhathcock/logging
Browse files Browse the repository at this point in the history
Logging
  • Loading branch information
rogeralsing authored Mar 19, 2017
2 parents d6dc9d2 + 94b2e4f commit 7b41116
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 12 deletions.
16 changes: 11 additions & 5 deletions examples/Supervision/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
using System.Threading;
using System.Threading.Tasks;
using Proto;
using Microsoft.Extensions.Logging;

class Program
{
static void Main(string[] args)
{
Proto.Log.SetLoggerFactory(new LoggerFactory()
.AddConsole(minLevel: LogLevel.Debug));

var props = Actor.FromProducer(() => new ParentActor()).WithSupervisor(new OneForOneStrategy(Decider.Decide, 1, null));

var actor = Actor.Spawn(props);
Expand Down Expand Up @@ -83,30 +87,32 @@ public Task ReceiveAsync(IContext context)

internal class ChildActor : IActor
{
private ILogger logger = Log.CreateLogger<ChildActor>();

public Task ReceiveAsync(IContext context)
{
var msg = context.Message;

switch (context.Message)
{
case Hello r:
Console.WriteLine($"Hello {r.Who}");
logger.LogDebug($"Hello {r.Who}");
break;
case Recoverable r:
throw new RecoverableException();
case Fatal r:
throw new FatalException();
case Started r:
Console.WriteLine("Started, initialize actor here");
logger.LogDebug("Started, initialize actor here");
break;
case Stopping r:
Console.WriteLine("Stopping, actor is about shut down");
logger.LogDebug("Stopping, actor is about shut down");
break;
case Stopped r:
Console.WriteLine("Stopped, actor and it's children are stopped");
logger.LogDebug("Stopped, actor and it's children are stopped");
break;
case Restarting r:
Console.WriteLine("Restarting, actor is about restart");
logger.LogDebug("Restarting, actor is about restart");
break;
}
return Actor.Done;
Expand Down
4 changes: 4 additions & 0 deletions examples/Supervision/Supervision.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Proto.Actor\Proto.Actor.csproj" />
</ItemGroup>
Expand Down
8 changes: 7 additions & 1 deletion src/Proto.Actor/EventStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,25 @@

using System;
using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;

namespace Proto
{
public class EventStream : EventStream<object>
{
public static readonly EventStream Instance = new EventStream();

private readonly ILogger logger;

public EventStream()
{
logger = Log.CreateLogger<EventStream>();

Subscribe(msg =>
{
if (msg is DeadLetterEvent letter)
{
Console.WriteLine("[DeadLetter] '{0}' got '{1}:{2}' from '{3}'", letter.Pid.ToShortString(),
logger.LogInformation("[DeadLetter] '{0}' got '{1}:{2}' from '{3}'", letter.Pid.ToShortString(),
letter.Message.GetType().Name, letter.Message, letter.Sender?.ToShortString());
}
});
Expand Down
7 changes: 4 additions & 3 deletions src/Proto.Actor/LocalContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Proto.Mailbox;
using System.Collections.ObjectModel;

namespace Proto
{
Expand All @@ -31,6 +31,7 @@ public class Context : IMessageInvoker, IContext, ISupervisor
private bool _stopping;
private HashSet<PID> _watchers;
private HashSet<PID> _watching;
private readonly ILogger logger = Log.CreateLogger<Context>();


public Context(Func<IActor> producer, ISupervisorStrategy supervisorStrategy, Receive receiveMiddleware, Sender senderMiddleware, PID parent)
Expand Down Expand Up @@ -221,13 +222,13 @@ public Task InvokeSystemMessageAsync(object msg)
case ResumeMailbox rm:
return Task.FromResult(0);
default:
Console.WriteLine("Unknown system message {0}", msg);
logger.LogWarning("Unknown system message {0}", msg);
return Task.FromResult(0);
}
}
catch (Exception x)
{
Console.WriteLine("Error handling SystemMessage {0}", x);
logger.LogError("Error handling SystemMessage {0}", x);
return Task.FromResult(0);
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/Proto.Actor/Log.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.Extensions.Logging;

namespace Proto
{
public static class Log
{
private static ILoggerFactory loggerFactory = new NullLoggerFactory();

public static void SetLoggerFactory(ILoggerFactory loggerFactory)
{
Log.loggerFactory = loggerFactory;
}

public static ILogger CreateLogger(string categoryName)
{
return loggerFactory.CreateLogger(categoryName);
}

public static ILogger CreateLogger<T>()
{
return loggerFactory.CreateLogger<T>();
}
}
}
23 changes: 23 additions & 0 deletions src/Proto.Actor/NullLoggerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

namespace Proto
{
public class NullLoggerFactory : ILoggerFactory
{
public static readonly NullLoggerFactory Instance = new NullLoggerFactory();

public ILogger CreateLogger(string name)
{
return NullLogger.Instance;
}

public void AddProvider(ILoggerProvider provider)
{
}

public void Dispose()
{
}
}
}
1 change: 1 addition & 0 deletions src/Proto.Actor/Proto.Actor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.1.1" />
<PackageReference Include="System.ValueTuple" Version="4.3.0" />
</ItemGroup>
<ItemGroup>
Expand Down
8 changes: 5 additions & 3 deletions src/Proto.Actor/Supervision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Proto.Mailbox;

namespace Proto
Expand Down Expand Up @@ -45,6 +46,7 @@ public class OneForOneStrategy : ISupervisorStrategy
public readonly int _maxNrOfRetries;
private readonly TimeSpan? _withinTimeSpan;
private readonly Decider _decider;
private readonly ILogger logger = Log.CreateLogger<OneForOneStrategy>();

public OneForOneStrategy(Decider decider, int maxNrOfRetries, TimeSpan? withinTimeSpan)
{
Expand All @@ -64,17 +66,17 @@ public void HandleFailure(ISupervisor supervisor, PID child, RestartStatistics r
case SupervisorDirective.Restart:
if (RequestRestartPermission(rs))
{
Console.WriteLine($"Restarting {child.ToShortString()} Reason {reason}");
logger.LogInformation($"Restarting {child.ToShortString()} Reason {reason}");
supervisor.RestartChildren(child);
}
else
{
Console.WriteLine($"Stopping {child.ToShortString()} Reason { reason}");
logger.LogInformation($"Stopping {child.ToShortString()} Reason { reason}");
supervisor.StopChildren(child);
}
break;
case SupervisorDirective.Stop:
Console.WriteLine($"Stopping {child.ToShortString()} Reason {reason}");
logger.LogInformation($"Stopping {child.ToShortString()} Reason {reason}");
supervisor.StopChildren(child);
break;
case SupervisorDirective.Escalate:
Expand Down

0 comments on commit 7b41116

Please sign in to comment.