Skip to content

Commit

Permalink
comments & minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tippmar-nr committed Jan 16, 2025
1 parent a3ee359 commit c907fb4
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions src/Agent/NewRelic/Agent/Core/Logging/LoggerBootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,16 @@ public static class LoggerBootstrapper

public static void SetLoggingLevel(string newLogLevel) => _loggingLevelSwitch.MinimumLevel = newLogLevel.MapToSerilogLogLevel();

private static bool _isWindows;

public static void Initialize()
{
#if NETSTANDARD2_0
_isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
#else
_isWindows = true;
#endif

var startupLoggerConfig = new LoggerConfiguration()
.Enrich.With(new ThreadIdEnricher(), new ProcessIdEnricher(), new NrLogLevelEnricher(), new UTCTimestampEnricher())
.MinimumLevel.Information()
Expand All @@ -48,7 +56,7 @@ public static void Initialize()
/// <remarks>This should only be called once, as soon as you have a valid config.</remarks>
public static void ConfigureLogger(ILogConfig config)
{
// if logging is disabled, it's disabled. We don't need to do anything else.
// if logging is disabled, we don't log anywhere
if (!config.Enabled)
{
Log.Logger = Serilog.Core.Logger.None; // a logger that does nothing
Expand Down Expand Up @@ -89,6 +97,9 @@ private static void EchoInMemoryLogsToConfiguredLogger(ILogger configuredLogger)
_inMemorySink.Dispose();
}

/// <summary>
/// Configures the in-memory log sink used during bootstrapping.
/// </summary>
private static LoggerConfiguration ConfigureInMemoryLogSink(this LoggerConfiguration loggerConfiguration)
{
// formatter not needed since this will be pushed to other sinks for output.
Expand All @@ -115,12 +126,7 @@ private static LoggerConfiguration ConfigureInMemoryLogSink(this LoggerConfigura
/// <param name="loggerConfiguration"></param>
private static LoggerConfiguration ConfigureEventLogSink(this LoggerConfiguration loggerConfiguration)
{
#if NETSTANDARD2_0
var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
#else
var isWindows = true;
#endif
if (isWindows)
if (_isWindows)
{
const string eventLogName = "Application";
const string eventLogSourceName = "New Relic .NET Agent";
Expand Down Expand Up @@ -199,21 +205,24 @@ private static LoggerConfiguration ConfigureFileSink(this LoggerConfiguration lo
.WriteTo
.Async(a =>
a.Logger(configuration =>
{
configuration
.ExcludeAuditLog()
.ConfigureRollingLogSink(logFileName, FileLogLayout, config);
})
);
{
configuration
.ExcludeAuditLog()
.ConfigureRollingLogSink(logFileName, FileLogLayout, config);
})
);
}
catch (Exception ex)
{
Log.Logger.Warning(ex, "Unexpected exception when configuring file sink.");

// Fallback to the event log sink if we cannot setup a file logger.
Extensions.Logging.Log.FileLoggingHasFailed = true;
Log.Logger.Warning("Falling back to EventLog sink.");
loggerConfiguration.ConfigureEventLogSink();
if (_isWindows)
{
// Fallback to the event log sink if we cannot setup a file logger.
Extensions.Logging.Log.FileLoggingHasFailed = true;
Log.Logger.Warning("Falling back to EventLog sink.");
loggerConfiguration.ConfigureEventLogSink();
}
}

return loggerConfiguration;
Expand Down

0 comments on commit c907fb4

Please sign in to comment.