Skip to content

feat: Implement simple in-memory Serilog sink. #1657

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
43 changes: 43 additions & 0 deletions src/Agent/NewRelic/Agent/Core/Logging/InMemorySink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

using System;
using Serilog.Configuration;
using Serilog;
using Serilog.Core;
using Serilog.Events;
using System.Collections.Concurrent;

namespace NewRelic.Agent.Core.Logging
{
public class InMemorySink : ILogEventSink, IDisposable
{
private static readonly InMemorySink _instance = new InMemorySink();

public readonly ConcurrentQueue<LogEvent> LogEvents;

public static InMemorySink Instance
{
get
{
return _instance;
}
}

protected InMemorySink()
{
LogEvents = new ConcurrentQueue<LogEvent>();
}

public void Emit(LogEvent logEvent)
{
LogEvents.Enqueue(logEvent);
}

public void Dispose()
{
while (LogEvents.TryDequeue(out _))
{ }
}
}
}
31 changes: 17 additions & 14 deletions src/Agent/NewRelic/Agent/Core/Logging/LoggerBootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Serilog.Core;
using Serilog.Formatting;
using Logger = NewRelic.Agent.Core.Logging.Logger;
using NewRelic.Agent.Core.Logging;

namespace NewRelic.Agent.Core
{
Expand Down Expand Up @@ -53,11 +54,9 @@ public static void Initialize()
.Enrich.With(new ThreadIdEnricher())
.Enrich.With(new ProcessIdEnricher())
.MinimumLevel.Information()
.ConfigureInMemoryLogSink()
.ConfigureInMemoryLogSink();
// TODO: implement event log sink
//.ConfigureEventLogSink()
// TODO: Remove console log sink when in-memory sink is implemented
.ConfigureConsoleSink();
//.ConfigureEventLogSink();

// set the global Serilog logger to our startup logger instance, this gets replaced when ConfigureLogger() is called
Serilog.Log.Logger = startupLoggerConfig.CreateLogger();
Expand Down Expand Up @@ -103,13 +102,12 @@ public static void ConfigureLogger(ILogConfig config)

private static void EchoInMemoryLogsToConfiguredLogger(Serilog.ILogger configuredLogger)
{
// TODO: copy logs from inMemory logger and emit them to Serilog.Log.Logger
// possible example:
//foreach (LogEvent logEvent in InMemorySink.Instance.LogEvents)
//{
// configuredLogger.Write(logEvent.Level, logEvent.Exception, logEvent.MessageTemplate.Render(logEvent.Properties));
//}
//InMemorySink.Instance.Dispose();
while (InMemorySink.Instance.LogEvents.TryDequeue(out var logEvent))
{
configuredLogger.Write(logEvent);
}

InMemorySink.Instance.Dispose();
}

/// <summary>
Expand All @@ -133,9 +131,14 @@ private static void SetupLogLevel(ILogConfig config)

private static LoggerConfiguration ConfigureInMemoryLogSink(this LoggerConfiguration loggerConfiguration)
{
// TODO Configure the (yet-to-be-implemented) in-memory sink

return loggerConfiguration;
// formatter not needed since this will be pushed to other sinks for output.
return loggerConfiguration
.WriteTo.Logger(configuration =>
{
configuration
.ExcludeAuditLog()
.WriteTo.Sink(InMemorySink.Instance);
});
}

// TODO: Implement EventLog support, see commented package reference in Core.csproj
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using log4net.Appender;
using log4net.Core;
using System.IO;
using NewRelic.Agent.Core.Logging;

namespace NewRelic.Agent.Core
{
Expand Down Expand Up @@ -368,7 +369,38 @@ public static void Logging_sets_threadid_property_for_LogException()
Assert.AreEqual(log4net.ThreadContext.Properties["threadid"], Thread.CurrentThread.ManagedThreadId);
}

#region InMemorySink Tests

[Test]
public static void InMemorySink_IsStartupLogger()
{
ILogConfig config = GetLogConfig("all");
LoggerBootstrapper.Initialize();

// Write a log message to global Serilog logger
Serilog.Log.Logger.Information("InMemorySink log");

// Check that the log message was stored in the InMemorySink
Assert.AreEqual(1, InMemorySink.Instance.LogEvents.Count);
}

[Test]
public static void InMemorySink_ReplacedBy_AgentLogger()
{
ILogConfig config = GetLogConfig("all");
LoggerBootstrapper.Initialize();

// Configure the real agent logger and replace the start up logger
LoggerBootstrapper.ConfigureLogger(config);

// Write a log message to global Serilog logger
Serilog.Log.Logger.Information("InMemorySink log");

// Check that the log message was not stored in the InMemorySink
Assert.AreEqual(0, InMemorySink.Instance.LogEvents.Count);
}

#endregion

static private ILogConfig GetLogConfig(string logLevel)
{
Expand Down