Skip to content

Commit

Permalink
Test to replace Serilog with simple file logger
Browse files Browse the repository at this point in the history
  • Loading branch information
chynesNR committed Nov 10, 2023
1 parent 9612e37 commit 24e41d4
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/Agent/NewRelic/Agent/Core/Logging/LoggerBootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static void ConfigureLogger(ILogConfig config)

Log.Logger = configuredLogger;

NewRelic.Core.Logging.Log.Initialize(new Logger());
NewRelic.Core.Logging.Log.Initialize(new NewRelic.Core.Logging.FileLogger(config.GetFullLogFileName()));
}

private static void EchoInMemoryLogsToConfiguredLogger(ILogger configuredLogger)
Expand Down Expand Up @@ -148,8 +148,9 @@ private static LoggerConfiguration ConfigureEventLogSink(this LoggerConfiguratio
);
});
}
catch
catch (Exception ex )
{
NewRelic.Core.Logging.Log.Warn(ex, "Failed to initialize event log");
// ignored -- there's nothing we can do at this point, as EventLog is our "fallback" logger and if it fails, we're out of luck
}
}
Expand Down Expand Up @@ -213,7 +214,7 @@ private static LoggerConfiguration ConfigureFileSink(this LoggerConfiguration lo
}
catch (Exception ex)
{
Log.Logger.Warning(ex, "Unexpected exception when configuring file sink.");
NewRelic.Core.Logging.Log.Warn(ex, "Unexpected exception when configuring file sink.");

// Fallback to the event log sink if we cannot setup a file logger.
Log.Logger.Warning("Falling back to EventLog sink.");
Expand Down Expand Up @@ -263,7 +264,7 @@ private static LoggerConfiguration ConfigureRollingLogSink(this LoggerConfigurat
}
catch (Exception exception)
{
Log.Logger.Warning(exception, $"Unable to write logfile at \"{fileName}\"");
NewRelic.Core.Logging.Log.Warn(exception, $"Unable to write logfile at \"{fileName}\"");
throw;
}

Expand All @@ -283,7 +284,7 @@ private static LoggerConfiguration ConfigureRollingLogSink(this LoggerConfigurat
}
catch (Exception exception)
{
Log.Logger.Warning(exception, $"Unexpected exception while configuring file logging for \"{fileName}\"");
NewRelic.Core.Logging.Log.Warn(exception, $"Unexpected exception while configuring file logging for \"{fileName}\"");
throw;
}
}
Expand Down
121 changes: 121 additions & 0 deletions src/NewRelic.Core/Logging/FileLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

using System;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace NewRelic.Core.Logging
{
public class FileLogger : ILogger
{
private static ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
private static string _path;

public FileLogger(string path)
{
_path = path.Replace(".log", $".{Process.GetCurrentProcess().Id}.log");
}

public bool IsDebugEnabled => true;

public bool IsErrorEnabled => true;

public bool IsFinestEnabled => true;

public bool IsInfoEnabled => true;

public bool IsWarnEnabled => true;

private static void LogWrite(string message)
{
_lock.EnterWriteLock();
File.AppendAllText(_path, message);
_lock.ExitWriteLock();
}

private static void FormatLog(string level, string template, params object[] args)
{
if (string.IsNullOrEmpty(template))
return;
string message;
try
{
message = (args == null) || (args.Length == 0) ? template : string.Format(template, args);
}
catch // oops, we must be using Serilog-style logging. just dump the args
{
message = $"{template} /// {string.Join(",", args)}";
}
var pid = Process.GetCurrentProcess().Id;
var tid = Thread.CurrentThread.ManagedThreadId;
// 2023-11-09 20:04:50,248 NewRelic INFO: [pid: 1, tid: 1] Application name from NEW_RELIC_APP_NAME Environment Variable.
string full = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} NewRelic {level.PadLeft(5)}: [pid: {pid} tid: {tid}] {message}\r\n";
LogWrite(full);
}

private static void LogException(Exception e)
{
if (e == null) return;
LogWrite(e?.Message);
LogWrite(e?.StackTrace);
LogWrite("\r\n");
}

public void Debug(Exception exception, string message, params object[] args)
{
Debug(message, args);
LogException(exception);
}

public void Debug(string message, params object[] args)
{
FormatLog("DEBUG", message, args);
}

public void Error(Exception exception, string message, params object[] args)
{
Error(message, args);
LogException(exception);
}

public void Error(string message, params object[] args)
{
FormatLog("ERROR", message, args, args);
}

public void Finest(Exception exception, string message, params object[] args)
{
Finest(message, args);
LogException(exception);
}

public void Finest(string message, params object[] args)
{
FormatLog("FINEST", message, args);
}

public void Info(Exception exception, string message, params object[] args)
{
Info(message, args);
LogException(exception);
}

public void Info(string message, params object[] args)
{
FormatLog("INFO", message, args);
}

public void Warn(Exception exception, string message, params object[] args)
{
Warn(message, args);
LogException(exception);
}

public void Warn(string message, params object[] args)
{
FormatLog("WARN", message, args);
}
}
}

0 comments on commit 24e41d4

Please sign in to comment.