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

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

namespace NewRelic.Agent.Core.Logging
{
public class InMemorySink : ILogEventSink, IDisposable
{
private readonly ConcurrentQueue<LogEvent> _logEvents;

public InMemorySink()
{
_logEvents = new ConcurrentQueue<LogEvent>();
}

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

public IEnumerable<LogEvent> LogEvents
{
get
{
return _logEvents;
}
}

public void Clear()
{
while (_logEvents.TryDequeue(out _))
{ }
}

public void Dispose()
{
Clear();
}
}
}
33 changes: 19 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 @@ -42,6 +43,8 @@ public static class LoggerBootstrapper

private static LoggingLevelSwitch _loggingLevelSwitch = new LoggingLevelSwitch();

private static InMemorySink _inMemorySink = new InMemorySink();

public static void UpdateLoggingLevel(string newLogLevel)
{
_loggingLevelSwitch.MinimumLevel = newLogLevel.MapToSerilogLogLevel();
Expand All @@ -53,11 +56,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 +104,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();
foreach (var logEvent in _inMemorySink.LogEvents)
{
configuredLogger.Write(logEvent);
}

_inMemorySink.Dispose();
}

/// <summary>
Expand All @@ -133,9 +133,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);
});
}

// 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,8 +369,6 @@ public static void Logging_sets_threadid_property_for_LogException()
Assert.AreEqual(log4net.ThreadContext.Properties["threadid"], Thread.CurrentThread.ManagedThreadId);
}



static private ILogConfig GetLogConfig(string logLevel)
{
var xml = string.Format(
Expand Down