Skip to content

Commit c6b940d

Browse files
authored
fix: Ensure log lines are written to the audit log file when audit logging is enabled. (#2028) (#2029)
1 parent 2693125 commit c6b940d

File tree

3 files changed

+4
-26
lines changed

3 files changed

+4
-26
lines changed

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

+1-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static class AuditLog
1212
// a lazy ILogger instance that injects an "Audit" property
1313
private static Lazy<ILogger> _lazyAuditLogger = LazyAuditLogger();
1414

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

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

3939
public static LoggerConfiguration IncludeOnlyAuditLog(this LoggerConfiguration loggerConfiguration)
4040
{
41-
IsAuditLogEnabled = true; // set a flag so Log() can short-circuit when audit log is not enabled
42-
4341
return loggerConfiguration.Filter.ByIncludingOnly($"{LogLevelExtensions.AuditLevel} is not null");
4442
}
4543

4644
public static LoggerConfiguration ExcludeAuditLog(this LoggerConfiguration loggerConfiguration)
4745
{
48-
IsAuditLogEnabled = false; // set a flag so Log() can short-circuit when audit log is not enabled
49-
5046
return loggerConfiguration.Filter.ByIncludingOnly($"{LogLevelExtensions.AuditLevel} is null");
5147
}
5248
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public static void ConfigureLogger(ILogConfig config)
5757
{
5858
SetupLogLevel(config);
5959

60+
AuditLog.IsAuditLogEnabled = config.IsAuditLogEnabled;
61+
6062
var loggerConfig = new LoggerConfiguration()
6163
.MinimumLevel.ControlledBy(_loggingLevelSwitch)
6264
.ConfigureAuditLogSink(config)

tests/Agent/UnitTests/Core.UnitTest/Logging/AuditLogTests.cs

+1-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020 New Relic, Inc. All rights reserved.
1+
// Copyright 2020 New Relic, Inc. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

44
using NUnit.Framework;
@@ -30,26 +30,6 @@ public void TearDown()
3030
AuditLog.ResetLazyLogger();
3131
}
3232

33-
[Test]
34-
public void IncludeOnlyAuditLog_EnablesAuditLog()
35-
{
36-
Assert.False(AuditLog.IsAuditLogEnabled);
37-
38-
var _ = new LoggerConfiguration().IncludeOnlyAuditLog();
39-
40-
Assert.True(AuditLog.IsAuditLogEnabled);
41-
}
42-
43-
[Test]
44-
public void ExcludeAuditLog_DisablesAuditLog()
45-
{
46-
AuditLog.IsAuditLogEnabled = true;
47-
48-
var _ = new LoggerConfiguration().ExcludeAuditLog();
49-
50-
Assert.False(AuditLog.IsAuditLogEnabled);
51-
}
52-
5333
[TestCase(true)]
5434
[TestCase(false)]
5535
public void Log_OnlyLogsWhenAuditLogEnabled(bool logEnabled)

0 commit comments

Comments
 (0)