Skip to content

Commit

Permalink
IEtlContextLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
wickedmachinator committed Nov 11, 2024
1 parent 8a5ade4 commit 19a144c
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 110 deletions.
44 changes: 33 additions & 11 deletions EtLast.CommandService/CommandService.RunTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,37 @@ protected override IExecutionResult RunTasks(string commandId, string moduleName
foreach (var manifestProcessor in sessionBuilder.ManifestProcessors)
manifestProcessor?.RegisterToManifestEvents(context, context.Manifest);

try
if (sessionBuilder.EtlContextLoggerCreators?.Count > 0)
{
try
{
foreach (var creator in sessionBuilder.EtlContextLoggerCreators)
{
var logger = creator?.Invoke();
if (logger != null)
{
logger.Start();
context.Loggers.Add(logger);
}
}
}
catch (Exception ex)
{
var formattedMessage = ex.FormatWithEtlDetails();
context.Log(LogSeverity.Error, null, "{ErrorMessage}", formattedMessage);
context.LogOps(LogSeverity.Error, null, "{ErrorMessage}", formattedMessage);
}
}

if (EtlContextListenerCreators?.Count > 0)
{
if (EtlContextListenerCreators?.Count > 0)
try
{
foreach (var listenerCreator in EtlContextListenerCreators)
foreach (var creator in EtlContextListenerCreators)
{
if (listenerCreator != null)
var listener = creator?.Invoke(context);
if (listener != null)
{
var listener = listenerCreator.Invoke(context);
listener.Start();
context.Listeners.Add(listener);

Expand All @@ -65,12 +87,12 @@ protected override IExecutionResult RunTasks(string commandId, string moduleName
}
}
}
}
catch (Exception ex)
{
var formattedMessage = ex.FormatWithEtlDetails();
context.Log(LogSeverity.Error, null, "{ErrorMessage}", formattedMessage);
context.LogOps(LogSeverity.Error, null, "{ErrorMessage}", formattedMessage);
catch (Exception ex)
{
var formattedMessage = ex.FormatWithEtlDetails();
context.Log(LogSeverity.Error, null, "{ErrorMessage}", formattedMessage);
context.LogOps(LogSeverity.Error, null, "{ErrorMessage}", formattedMessage);
}
}

context.Log(LogSeverity.Information, null, "context {ContextName} started with ID: {ContextId}", context.Manifest.ContextName, context.Manifest.ContextId);
Expand Down
32 changes: 2 additions & 30 deletions EtLast.Hosting/Logging/EtlContextDevToFileLogger.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace FizzCode.EtLast;

internal class EtlContextDevToFileLogger : IEtlContextListener
internal class EtlContextDevToFileLogger : IEtlContextLogger
{
private readonly ILogger _logger;
private readonly ILogger _ioLogger;
Expand Down Expand Up @@ -256,34 +256,6 @@ public void OnContextIoCommandEnd(IoCommand ioCommand)
_ioLogger.Write(LogEventLevel.Information, sb.ToString());
}

public void OnRowCreated(IReadOnlyRow row)
{
}

public void OnRowOwnerChanged(IReadOnlyRow row, IProcess previousProcess, IProcess currentProcess)
{
}

public void OnRowValueChanged(IReadOnlyRow row, params KeyValuePair<string, object>[] values)
{
}

public void OnSinkStarted(IProcess process, Sink sink)
{
}

public void OnWriteToSink(Sink sink, IReadOnlyRow row)
{
}

public void OnProcessStart(IProcess process)
{
}

public void OnProcessEnd(IProcess process)
{
}

public void OnContextClosed()
{
try
Expand Down Expand Up @@ -332,7 +304,7 @@ public static class EtlContextDevToFileLoggerFluent
{
public static ISessionBuilder LogDevToFile(this ISessionBuilder builder, LogSeverity minimumLogLevel = LogSeverity.Debug, int retentionHours = 24 * 31)
{
builder.Context.Listeners.Add(new EtlContextDevToFileLogger(builder.Context, builder.DevLogDirectory, minimumLogLevel, retentionHours));
builder.AddLoggerCreator(() => new EtlContextDevToFileLogger(builder.Context, builder.DevLogDirectory, minimumLogLevel, retentionHours));
return builder;
}
}
36 changes: 4 additions & 32 deletions EtLast.Hosting/Logging/EtlContextDevToSeqLogger.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace FizzCode.EtLast;

internal class EtlContextDevToSeqLogger : IEtlContextListener
internal class EtlContextDevToSeqLogger : IEtlContextLogger
{
private readonly ILogger _logger;

Expand Down Expand Up @@ -83,34 +83,6 @@ public void OnContextIoCommandEnd(IoCommand ioCommand)
{
}

public void OnRowCreated(IReadOnlyRow row)
{
}

public void OnRowOwnerChanged(IReadOnlyRow row, IProcess previousProcess, IProcess currentProcess)
{
}

public void OnRowValueChanged(IReadOnlyRow row, params KeyValuePair<string, object>[] values)
{
}

public void OnSinkStarted(IProcess process, Sink sink)
{
}

public void OnWriteToSink(Sink sink, IReadOnlyRow row)
{
}

public void OnProcessStart(IProcess process)
{
}

public void OnProcessEnd(IProcess process)
{
}

public void OnContextClosed()
{
try
Expand All @@ -124,9 +96,9 @@ public void OnContextClosed()
[EditorBrowsable(EditorBrowsableState.Never)]
public static class EtlContextDevToSeqLoggerFluent
{
public static ISessionBuilder LogDevToSeq(this ISessionBuilder builder, string url, string apiKey, LogSeverity minimumLogLevel = LogSeverity.Debug)
public static ISessionBuilder LogDevToSeq(this ISessionBuilder session, string url, string apiKey, LogSeverity minimumLogLevel = LogSeverity.Debug)
{
builder.Context.Listeners.Add(new EtlContextDevToSeqLogger(builder.Context, url, apiKey, minimumLogLevel));
return builder;
session.AddLoggerCreator(() => new EtlContextDevToSeqLogger(session.Context, url, apiKey, minimumLogLevel));
return session;
}
}
38 changes: 5 additions & 33 deletions EtLast.Hosting/Logging/EtlContextOpsToFileLogger.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace FizzCode.EtLast;

internal class EtlContextOpsToFileLogger : IEtlContextListener
internal class EtlContextOpsToFileLogger : IEtlContextLogger
{
private readonly ILogger _logger;
private readonly string _directory;
private readonly object _customFileLock = new();
private readonly Lock _customFileLock = new();

public EtlContextOpsToFileLogger(IEtlContext context, string directory, int importantFileCount = 30, int infoFileCount = 14)
{
Expand Down Expand Up @@ -173,34 +173,6 @@ public void OnContextIoCommandEnd(IoCommand ioCommand)
{
}

public void OnRowCreated(IReadOnlyRow row)
{
}

public void OnRowOwnerChanged(IReadOnlyRow row, IProcess previousProcess, IProcess currentProcess)
{
}

public void OnRowValueChanged(IReadOnlyRow row, params KeyValuePair<string, object>[] values)
{
}

public void OnSinkStarted(IProcess process, Sink sink)
{
}

public void OnWriteToSink(Sink sink, IReadOnlyRow row)
{
}

public void OnProcessStart(IProcess process)
{
}

public void OnProcessEnd(IProcess process)
{
}

public void OnContextClosed()
{
try
Expand All @@ -214,9 +186,9 @@ public void OnContextClosed()
[EditorBrowsable(EditorBrowsableState.Never)]
public static class EtlContextOpsToFileLoggerFluent
{
public static ISessionBuilder LogOpsToFile(this ISessionBuilder builder, int importantFileCount = 30, int infoFileCount = 14)
public static ISessionBuilder LogOpsToFile(this ISessionBuilder session, int importantFileCount = 30, int infoFileCount = 14)
{
builder.Context.Listeners.Add(new EtlContextOpsToFileLogger(builder.Context, builder.OpsLogDirectory, importantFileCount, infoFileCount));
return builder;
session.AddLoggerCreator(() => new EtlContextOpsToFileLogger(session.Context, session.OpsLogDirectory, importantFileCount, infoFileCount));
return session;
}
}
27 changes: 27 additions & 0 deletions EtLast/Context/EtlContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public sealed class EtlContext : IEtlContext

public List<IEtlContextListener> Listeners { get; }
public List<IEtlContextRowListener> RowListeners { get; } = [];
public List<IEtlContextLogger> Loggers { get; } = [];

/// <summary>
/// Default value: 4 hours, but .NET maximizes the timeout in 10 minutes.
Expand Down Expand Up @@ -126,30 +127,45 @@ public void Terminate()

public void Log(string transactionId, LogSeverity severity, IProcess process, string text, params object[] args)
{
foreach (var logger in Loggers)
logger.OnLog(severity, false, transactionId, process, text, args);

foreach (var listener in Listeners)
listener.OnLog(severity, false, transactionId, process, text, args);
}

public void Log(LogSeverity severity, IProcess process, string text, params object[] args)
{
foreach (var logger in Loggers)
logger.OnLog(severity, false, null, process, text, args);

foreach (var listener in Listeners)
listener.OnLog(severity, false, null, process, text, args);
}

public void LogOps(LogSeverity severity, IProcess process, string text, params object[] args)
{
foreach (var logger in Loggers)
logger.OnLog(severity, true, null, process, text, args);

foreach (var listener in Listeners)
listener.OnLog(severity, true, null, process, text, args);
}

public void LogCustom(string fileName, IProcess process, string text, params object[] args)
{
foreach (var logger in Loggers)
logger.OnCustomLog(false, fileName, process, text, args);

foreach (var listener in Listeners)
listener.OnCustomLog(false, fileName, process, text, args);
}

public void LogCustomOps(string fileName, IProcess process, string text, params object[] args)
{
foreach (var logger in Loggers)
logger.OnCustomLog(true, fileName, process, text, args);

foreach (var listener in Listeners)
listener.OnCustomLog(true, fileName, process, text, args);
}
Expand All @@ -159,6 +175,9 @@ public IoCommand RegisterIoCommand(IoCommand ioCommand)
ioCommand.Id = Interlocked.Increment(ref _nextIoCommandId);
ioCommand.Context = this;

foreach (var logger in Loggers)
logger.OnContextIoCommandStart(ioCommand);

foreach (var listener in Listeners)
listener.OnContextIoCommandStart(ioCommand);

Expand Down Expand Up @@ -290,6 +309,14 @@ public List<Sink> QuerySinks(string location, string path)

public void Close()
{
foreach (var logger in Loggers)
{
logger.OnContextClosed();
if (logger is IDisposable disp)
disp.Dispose();
}
Loggers.Clear();

foreach (var listener in Listeners)
{
listener.OnContextClosed();
Expand Down
3 changes: 3 additions & 0 deletions EtLast/Context/IoCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public class IoCommand

public void End()
{
foreach (var logger in Context.Loggers)
logger.OnContextIoCommandEnd(this);

foreach (var listener in Context.Listeners)
listener.OnContextIoCommandEnd(this);
}
Expand Down
10 changes: 6 additions & 4 deletions EtLast/Flow/FlowState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ public void AddException(IProcess process, Exception ex)
if (ex is not EtlException)
ex = new ProcessExecutionException(process, ex);

foreach (var logger in Context.Loggers)
logger.OnException(process, ex);

foreach (var listener in Context.Listeners)
{
listener.OnException(process, ex);
}

Exceptions.Add(ex);
}
Expand All @@ -58,10 +59,11 @@ public void AddException(IProcess process, Exception ex, IReadOnlySlimRow row)
ex = new ProcessExecutionException(process, row, ex);
}

foreach (var logger in Context.Loggers)
logger.OnException(process, ex);

foreach (var listener in Context.Listeners)
{
listener.OnException(process, ex);
}

Exceptions.Add(ex);
}
Expand Down
1 change: 1 addition & 0 deletions EtLast/Interfaces/IEtlContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public interface IEtlContext : ICaller
public ContextManifest Manifest { get; }
public List<IEtlContextListener> Listeners { get; }
public List<IEtlContextRowListener> RowListeners { get; }
public List<IEtlContextLogger> Loggers { get; }

public IRow CreateRow(IProcess process);
public IRow CreateRow(IProcess process, IEnumerable<KeyValuePair<string, object>> initialValues);
Expand Down
14 changes: 14 additions & 0 deletions EtLast/Interfaces/IEtlContextLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace FizzCode.EtLast;

public interface IEtlContextLogger
{
void Start();

void OnLog(LogSeverity severity, bool forOps, string transactionId, IProcess process, string text, params object[] args);
void OnCustomLog(bool forOps, string fileName, IProcess process, string text, params object[] args);
void OnException(IProcess process, Exception exception);
void OnContextIoCommandStart(IoCommand ioCommand);
void OnContextIoCommandEnd(IoCommand ioCommand);

void OnContextClosed(); // all pending state must be flushed in this method
}
2 changes: 2 additions & 0 deletions EtLast/Session/ISessionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ public interface ISessionBuilder
public IEtlContext Context { get; }
public IEtlTask[] Tasks { get; }
public string TasksDirectoryName { get; }

public string DevLogDirectory { get; }
public string OpsLogDirectory { get; }

public ISessionBuilder AddManifestProcessor(IManifestProcessor manifestProcessor);
public ISessionBuilder AddLoggerCreator(Func<IEtlContextLogger> creator);
public ISessionBuilder UseTransactionScopeTimeout(TimeSpan timeout);
}
Loading

0 comments on commit 19a144c

Please sign in to comment.