Skip to content

Commit 29d5c82

Browse files
authored
feat: Implement simple in-memory Serilog sink. (#1657)
1 parent c4b0fbc commit 29d5c82

File tree

3 files changed

+65
-16
lines changed

3 files changed

+65
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2020 New Relic, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using System;
5+
using Serilog.Core;
6+
using Serilog.Events;
7+
using System.Collections.Concurrent;
8+
using System.Collections.Generic;
9+
10+
namespace NewRelic.Agent.Core.Logging
11+
{
12+
public class InMemorySink : ILogEventSink, IDisposable
13+
{
14+
private readonly ConcurrentQueue<LogEvent> _logEvents;
15+
16+
public InMemorySink()
17+
{
18+
_logEvents = new ConcurrentQueue<LogEvent>();
19+
}
20+
21+
public void Emit(LogEvent logEvent)
22+
{
23+
_logEvents.Enqueue(logEvent);
24+
}
25+
26+
public IEnumerable<LogEvent> LogEvents
27+
{
28+
get
29+
{
30+
return _logEvents;
31+
}
32+
}
33+
34+
public void Clear()
35+
{
36+
while (_logEvents.TryDequeue(out _))
37+
{ }
38+
}
39+
40+
public void Dispose()
41+
{
42+
Clear();
43+
}
44+
}
45+
}

src/Agent/NewRelic/Agent/Core/Logging/LoggerBootstrapper.cs

+19-14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Serilog.Core;
1111
using Serilog.Formatting;
1212
using Logger = NewRelic.Agent.Core.Logging.Logger;
13+
using NewRelic.Agent.Core.Logging;
1314

1415
namespace NewRelic.Agent.Core
1516
{
@@ -42,6 +43,8 @@ public static class LoggerBootstrapper
4243

4344
private static LoggingLevelSwitch _loggingLevelSwitch = new LoggingLevelSwitch();
4445

46+
private static InMemorySink _inMemorySink = new InMemorySink();
47+
4548
public static void UpdateLoggingLevel(string newLogLevel)
4649
{
4750
_loggingLevelSwitch.MinimumLevel = newLogLevel.MapToSerilogLogLevel();
@@ -53,11 +56,9 @@ public static void Initialize()
5356
.Enrich.With(new ThreadIdEnricher())
5457
.Enrich.With(new ProcessIdEnricher())
5558
.MinimumLevel.Information()
56-
.ConfigureInMemoryLogSink()
59+
.ConfigureInMemoryLogSink();
5760
// TODO: implement event log sink
58-
//.ConfigureEventLogSink()
59-
// TODO: Remove console log sink when in-memory sink is implemented
60-
.ConfigureConsoleSink();
61+
//.ConfigureEventLogSink();
6162

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

104105
private static void EchoInMemoryLogsToConfiguredLogger(Serilog.ILogger configuredLogger)
105106
{
106-
// TODO: copy logs from inMemory logger and emit them to Serilog.Log.Logger
107-
// possible example:
108-
//foreach (LogEvent logEvent in InMemorySink.Instance.LogEvents)
109-
//{
110-
// configuredLogger.Write(logEvent.Level, logEvent.Exception, logEvent.MessageTemplate.Render(logEvent.Properties));
111-
//}
112-
//InMemorySink.Instance.Dispose();
107+
foreach (var logEvent in _inMemorySink.LogEvents)
108+
{
109+
configuredLogger.Write(logEvent);
110+
}
111+
112+
_inMemorySink.Dispose();
113113
}
114114

115115
/// <summary>
@@ -133,9 +133,14 @@ private static void SetupLogLevel(ILogConfig config)
133133

134134
private static LoggerConfiguration ConfigureInMemoryLogSink(this LoggerConfiguration loggerConfiguration)
135135
{
136-
// TODO Configure the (yet-to-be-implemented) in-memory sink
137-
138-
return loggerConfiguration;
136+
// formatter not needed since this will be pushed to other sinks for output.
137+
return loggerConfiguration
138+
.WriteTo.Logger(configuration =>
139+
{
140+
configuration
141+
.ExcludeAuditLog()
142+
.WriteTo.Sink(_inMemorySink);
143+
});
139144
}
140145

141146
// TODO: Implement EventLog support, see commented package reference in Core.csproj

tests/Agent/UnitTests/Core.UnitTest/NewRelic.Agent.Core.FromLegacy/AgentLoggerTest.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using log4net.Appender;
1212
using log4net.Core;
1313
using System.IO;
14+
using NewRelic.Agent.Core.Logging;
1415

1516
namespace NewRelic.Agent.Core
1617
{
@@ -368,8 +369,6 @@ public static void Logging_sets_threadid_property_for_LogException()
368369
Assert.AreEqual(log4net.ThreadContext.Properties["threadid"], Thread.CurrentThread.ManagedThreadId);
369370
}
370371

371-
372-
373372
static private ILogConfig GetLogConfig(string logLevel)
374373
{
375374
var xml = string.Format(

0 commit comments

Comments
 (0)