Skip to content

Commit

Permalink
Add alteration logs to workflow execution logs (#5897) (#5911)
Browse files Browse the repository at this point in the history
  • Loading branch information
rosca-sabina authored Aug 30, 2024
1 parent 9365bb9 commit 18b0d2e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@ public AlterationContext(
/// <summary>
/// Logs a message.
/// </summary>
/// <param name="eventName">The event name to log.</param>
/// <param name="message">The message to log.</param>
/// <param name="logLevel">The log level.</param>
public void Log(string message, LogLevel logLevel = LogLevel.Information)
public void Log(string eventName, string message, LogLevel logLevel = LogLevel.Information)
{
AlterationLog.Add(message, logLevel);
AlterationLog.Add(message, logLevel, eventName);
}

/// <summary>
Expand All @@ -104,7 +105,7 @@ public void Succeed(Func<Task> commitAction)
public void Succeed(string message)
{
HasSucceeded = true;
Log(message, LogLevel.Information);
Log($"Alteration {Alteration.GetType().Name} succeeded", message, LogLevel.Information);
}

/// <summary>
Expand All @@ -123,6 +124,6 @@ public void Succeed(string message, Func<Task> commitAction)
public void Fail(string? message = default)
{
HasFailed = true;
Log(message ?? $"{Alteration.GetType().Name} failed", LogLevel.Error);
Log($"Alteration {Alteration.GetType().Name} failed", message ?? $"{Alteration.GetType().Name} failed", LogLevel.Error);
}
}
7 changes: 4 additions & 3 deletions src/modules/Elsa.Alterations.Core/Models/AlterationLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ public AlterationLog(ISystemClock systemClock)
/// Gets the log entries.
/// </summary>
public IReadOnlyCollection<AlterationLogEntry> LogEntries => _logEntries.ToList().AsReadOnly();

/// <summary>
/// Adds a log entry.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="logLevel">The log level.</param>
public void Add(string message, LogLevel logLevel = LogLevel.Information)
/// <param name="eventName">The event that generated the log entry.</param>
public void Add(string message, LogLevel logLevel = LogLevel.Information, string? eventName = null)
{
var entry = new AlterationLogEntry(message, logLevel, _systemClock.UtcNow);
var entry = new AlterationLogEntry(message, logLevel, _systemClock.UtcNow, eventName);

_logEntries.Add(entry);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ namespace Elsa.Alterations.Core.Models;
/// <param name="Message">The log message.</param>
/// <param name="LogLevel">The log level.</param>
/// <param name="Timestamp">The timestamp when the log entry was created.</param>
public record AlterationLogEntry(string Message, LogLevel LogLevel, DateTimeOffset Timestamp);
/// <param name="EventName">The event that generated the log entry.</param>
public record AlterationLogEntry(string Message, LogLevel LogLevel, DateTimeOffset Timestamp, string? EventName = null);
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,9 @@ private async Task RunAsync(WorkflowExecutionContext workflowExecutionContext, I
// Execute commit handlers.
foreach (var commitAction in commitActions)
await commitAction();

// Add alteration logs to the workflow execution log.
foreach (var alterationLogEntry in log.LogEntries)
workflowExecutionContext.AddExecutionLogEntry(alterationLogEntry.EventName ?? alterationLogEntry.Message, alterationLogEntry.Message);
}
}
3 changes: 3 additions & 0 deletions src/modules/Elsa.MongoDb/Common/MongoDbStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public async Task<TDocument> AddAsync(TDocument document, CancellationToken canc
/// <param name="cancellationToken">The cancellation token.</param>
public async Task AddManyAsync(IEnumerable<TDocument> documents, CancellationToken cancellationToken = default)
{
if (!documents.Any())
return;

await _collection.InsertManyAsync(documents, new InsertManyOptions(), cancellationToken);
}

Expand Down

0 comments on commit 18b0d2e

Please sign in to comment.