-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Use Serilog instead of log4net for internal logging. (#1661)
- Loading branch information
1 parent
0882d47
commit 51080df
Showing
35 changed files
with
1,090 additions
and
1,113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,37 @@ | ||
// Copyright 2020 New Relic, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System; | ||
using Serilog; | ||
using ILogger = Serilog.ILogger; | ||
|
||
namespace NewRelic.Agent.Core.Logging | ||
{ | ||
public static class AuditLog | ||
{ | ||
private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(typeof(AuditLog)); | ||
// a lazy ILogger instance that injects an "Audit" property | ||
private static Lazy<ILogger> _lazyAuditLogger = new Lazy<ILogger>(() => | ||
Serilog.Log.Logger.ForContext(LogLevelExtensions.AuditLevel, LogLevelExtensions.AuditLevel)); | ||
|
||
/// <summary> | ||
/// Logs <paramref name="message"/> at the AUDIT level, a custom log level that is not well-defined in popular logging providers like log4net. This log level should be used only as dictated by the security team to satisfy auditing requirements. | ||
/// Logs <paramref name="message"/> at the AUDIT level. This log level should be used only as dictated by the security team to satisfy auditing requirements. | ||
/// </summary> | ||
public static void Log(string message) | ||
{ | ||
var auditLogLevel = LoggerBootstrapper.GetAuditLevel(); | ||
Logger.Logger.Log(typeof(AuditLog), auditLogLevel, message, null); | ||
// use Fatal log level to ensure audit log messages never get filtered due to level restrictions | ||
_lazyAuditLogger.Value.Fatal(message); | ||
} | ||
|
||
internal static LoggerConfiguration IncludeOnlyAuditLog(this LoggerConfiguration loggerConfiguration) | ||
{ | ||
return loggerConfiguration.Filter.ByIncludingOnly($"{LogLevelExtensions.AuditLevel} is not null"); | ||
|
||
//return loggerConfiguration.Filter.ByIncludingOnly(logEvent => | ||
// logEvent.Properties.ContainsKey(LogLevelExtensions.AuditLevel)); | ||
} | ||
internal static LoggerConfiguration ExcludeAuditLog(this LoggerConfiguration loggerConfiguration) | ||
{ | ||
return loggerConfiguration.Filter.ByIncludingOnly($"{LogLevelExtensions.AuditLevel} is null"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
Oops, something went wrong.