Skip to content
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

Use LoggerMessage for logging in ActorContext #1907

Merged
merged 1 commit into from
Feb 8, 2023
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
39 changes: 12 additions & 27 deletions src/Proto.Actor/Context/ActorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,12 @@ public void Respond(object message)
{
if (Sender is not null)
{
if (Logger.IsEnabled(LogLevel.Debug))
{
Logger.LogDebug("{Self} Responding to {Sender} with message {Message}", Self, Sender, message);
}

Logger.ActorResponds(Self, Sender, Message);
SendUserMessage(Sender, message);
}
else
{
Logger.LogWarning("{Self} Tried to respond but sender is null, with message {Message}", Self, message);
Logger.ActorRespondsButSenderIsNull(Self, Message);
}
}

Expand All @@ -106,10 +102,9 @@ public PID SpawnNamed(Props props, string name, Action<IContext>? callback = nul

return pid;
}
catch (Exception x)
catch (Exception)
{
Logger.LogError(x, "{Self} Failed to spawn child actor {Name}", Self, name);

Logger.FailedToSpawnChildActor(Self, name);
throw;
}
}
Expand Down Expand Up @@ -181,11 +176,11 @@ public void Forward(PID target)
switch (_messageOrEnvelope)
{
case null:
Logger.LogWarning("Message is null");
Logger.MessageIsNull();

return;
case SystemMessage _:
Logger.LogWarning("SystemMessage cannot be forwarded. {Message}", _messageOrEnvelope);
Logger.SystemMessageCannotBeForwarded(_messageOrEnvelope);

return;
default:
Expand Down Expand Up @@ -348,10 +343,7 @@ public void EscalateFailure(Exception reason, object? message)
Console.WriteLine(
$"[Supervision] Actor {Self} : {Actor.GetType().Name} failed with message:{message} exception:{reason}");

Logger.LogError(reason,
"[Supervision] Actor {Self} : {ActorType} failed with message:{Message} exception:{Reason}", Self,
Actor.GetType().Name, message, reason
);
Logger.EscalateFailure(reason, Self, Actor.GetType().Name, message);
}

ActorMetrics.ActorFailureCount.Add(1, _metricTags);
Expand Down Expand Up @@ -390,7 +382,7 @@ public ValueTask InvokeSystemMessageAsync(SystemMessage msg)
}
catch (Exception x)
{
Logger.LogError(x, "Error handling SystemMessage {Message}", msg);
Logger.ErrorHandlingSystemMessage(x, msg);

throw;
}
Expand Down Expand Up @@ -563,7 +555,7 @@ static async Task Continue(Task target, Continuation cont, IContext ctx)
private static ValueTask HandleUnknownSystemMessage(object msg)
{
//TODO: sounds like a pretty severe issue if we end up here? what todo?
Logger.LogWarning("Unknown system message {Message}", msg);
Logger.UnknownSystemMessage(msg);

return default;
}
Expand All @@ -575,16 +567,9 @@ private async ValueTask HandleContinuation(Continuation cont)
// an older Actor instance.
if (cont.Actor != Actor && cont is not { Actor: null })
{
if (Logger.IsEnabled(LogLevel.Warning))
{
Logger.LogWarning(
"{Self} Dropping Continuation (ReenterAfter) of {Message}",
Self,
MessageEnvelope.UnwrapMessage(cont.Message)
);
}
Logger.DroppingContinuation(Self, MessageEnvelope.UnwrapMessage(cont.Message));

return;
return;
}

_messageOrEnvelope = cont.Message;
Expand Down Expand Up @@ -751,7 +736,7 @@ static async ValueTask Await(ActorContext self)
}
catch (Exception e)
{
Logger.LogError(e, "{Self} Error while handling Stopping message", self.Self);
Logger.ErrorHandlingStopingMessage(e, self.Self);
// do not rethrow - prevent exceptions thrown from stopping handler from restarting the actor
}

Expand Down
40 changes: 40 additions & 0 deletions src/Proto.Actor/Context/ActorContextLogMessages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// // Copyright (c) Dolittle. All rights reserved.
// // Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using Microsoft.Extensions.Logging;

namespace Proto.Context;

internal static partial class ActorContextLogMessages
{
[LoggerMessage(0, LogLevel.Debug, "{Self} Responding to {Sender} with message {Message}")]
internal static partial void ActorResponds(this ILogger logger, PID self, PID sender, object? message);

[LoggerMessage(1, LogLevel.Warning, "{Self} Tried to respond but sender is null, with message {Message}")]
internal static partial void ActorRespondsButSenderIsNull(this ILogger logger, PID self, object? message);

[LoggerMessage(2, LogLevel.Error, "{Self} Failed to spawn child actor {Name}")]
internal static partial void FailedToSpawnChildActor(this ILogger logger, PID self, string name);

[LoggerMessage(3, LogLevel.Warning, "Message is null")]
internal static partial void MessageIsNull(this ILogger logger);

[LoggerMessage(4, LogLevel.Warning, "SystemMessage cannot be forwarded. {Message}")]
internal static partial void SystemMessageCannotBeForwarded(this ILogger logger, object? message);

[LoggerMessage(5, LogLevel.Error, "[Supervision] Actor {Self} : {ActorType} failed with message:{Message}")]
internal static partial void EscalateFailure(this ILogger logger, Exception reason, PID self, string actorType, object? message);

[LoggerMessage(6, LogLevel.Error, "Error handling SystemMessage {Message}")]
internal static partial void ErrorHandlingSystemMessage(this ILogger logger, Exception ex, object? message);

[LoggerMessage(7, LogLevel.Warning, "Unknown system message {Message}")]
internal static partial void UnknownSystemMessage(this ILogger logger, object? message);

[LoggerMessage(8, LogLevel.Warning, "{Self} Dropping Continuation (ReenterAfter) of {Message}")]
internal static partial void DroppingContinuation(this ILogger logger, PID self, object? message);

[LoggerMessage(9, LogLevel.Error, "{Self} Error while handling Stopping message")]
internal static partial void ErrorHandlingStopingMessage(this ILogger logger, Exception ex, PID self);
}