Skip to content

Commit

Permalink
fix: Ensure log lines are written to the audit log file when audit lo…
Browse files Browse the repository at this point in the history
…gging is enabled. (#2028) (#2029)
  • Loading branch information
tippmar-nr committed Nov 6, 2023
1 parent 2693125 commit c6b940d
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 26 deletions.
6 changes: 1 addition & 5 deletions src/Agent/NewRelic/Agent/Core/Logging/AuditLog.cs
Expand Up @@ -12,7 +12,7 @@ public static class AuditLog
// a lazy ILogger instance that injects an "Audit" property
private static Lazy<ILogger> _lazyAuditLogger = LazyAuditLogger();

public static bool IsAuditLogEnabled { get; set; } //setter is public only for unit tests, not expected to be use anywhere else
public static bool IsAuditLogEnabled { get; set; }

// for unit tests only
public static void ResetLazyLogger()
Expand All @@ -38,15 +38,11 @@ public static void Log(string message)

public static LoggerConfiguration IncludeOnlyAuditLog(this LoggerConfiguration loggerConfiguration)
{
IsAuditLogEnabled = true; // set a flag so Log() can short-circuit when audit log is not enabled

return loggerConfiguration.Filter.ByIncludingOnly($"{LogLevelExtensions.AuditLevel} is not null");
}

public static LoggerConfiguration ExcludeAuditLog(this LoggerConfiguration loggerConfiguration)
{
IsAuditLogEnabled = false; // set a flag so Log() can short-circuit when audit log is not enabled

return loggerConfiguration.Filter.ByIncludingOnly($"{LogLevelExtensions.AuditLevel} is null");
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Agent/NewRelic/Agent/Core/Logging/LoggerBootstrapper.cs
Expand Up @@ -57,6 +57,8 @@ public static void ConfigureLogger(ILogConfig config)
{
SetupLogLevel(config);

AuditLog.IsAuditLogEnabled = config.IsAuditLogEnabled;

var loggerConfig = new LoggerConfiguration()
.MinimumLevel.ControlledBy(_loggingLevelSwitch)
.ConfigureAuditLogSink(config)
Expand Down
22 changes: 1 addition & 21 deletions tests/Agent/UnitTests/Core.UnitTest/Logging/AuditLogTests.cs
@@ -1,4 +1,4 @@
// Copyright 2020 New Relic, Inc. All rights reserved.
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

using NUnit.Framework;
Expand Down Expand Up @@ -30,26 +30,6 @@ public void TearDown()
AuditLog.ResetLazyLogger();
}

[Test]
public void IncludeOnlyAuditLog_EnablesAuditLog()
{
Assert.False(AuditLog.IsAuditLogEnabled);

var _ = new LoggerConfiguration().IncludeOnlyAuditLog();

Assert.True(AuditLog.IsAuditLogEnabled);
}

[Test]
public void ExcludeAuditLog_DisablesAuditLog()
{
AuditLog.IsAuditLogEnabled = true;

var _ = new LoggerConfiguration().ExcludeAuditLog();

Assert.False(AuditLog.IsAuditLogEnabled);
}

[TestCase(true)]
[TestCase(false)]
public void Log_OnlyLogsWhenAuditLogEnabled(bool logEnabled)
Expand Down

0 comments on commit c6b940d

Please sign in to comment.