From 274072baba12888c49e402b81d2e0d598f717614 Mon Sep 17 00:00:00 2001 From: Marty Tippin <120425148+tippmar-nr@users.noreply.github.com> Date: Tue, 10 Oct 2023 08:52:50 -0500 Subject: [PATCH] fix: Update Serilog EventLog sink configuration to enable event source creation (if the app is running with admin privileges). (#1963) --- .../Agent/Core/Logging/LoggerBootstrapper.cs | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/Agent/NewRelic/Agent/Core/Logging/LoggerBootstrapper.cs b/src/Agent/NewRelic/Agent/Core/Logging/LoggerBootstrapper.cs index 3438256c78..fb3538e62b 100644 --- a/src/Agent/NewRelic/Agent/Core/Logging/LoggerBootstrapper.cs +++ b/src/Agent/NewRelic/Agent/Core/Logging/LoggerBootstrapper.cs @@ -115,19 +115,26 @@ private static LoggerConfiguration ConfigureEventLogSink(this LoggerConfiguratio #if NETFRAMEWORK const string eventLogName = "Application"; const string eventLogSourceName = "New Relic .NET Agent"; - - loggerConfiguration - .WriteTo.Logger(configuration => - { - configuration - .ExcludeAuditLog() - .WriteTo.EventLog( - source: eventLogSourceName, - logName: eventLogName, - restrictedToMinimumLevel: LogEventLevel.Warning, - outputTemplate: "{Level}: {Message}{NewLine}{Exception}" - ); - }); + try + { + loggerConfiguration + .WriteTo.Logger(configuration => + { + configuration + .ExcludeAuditLog() + .WriteTo.EventLog( + source: eventLogSourceName, + logName: eventLogName, + restrictedToMinimumLevel: LogEventLevel.Warning, + outputTemplate: "{Level}: {Message}{NewLine}{Exception}", + manageEventSource: true // Serilog will create the event source if it doesn't exist *and* if the app is running with admin privileges + ); + }); + } + catch + { + // 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 + } #endif return loggerConfiguration; }