Skip to content

Commit 274072b

Browse files
authored
fix: Update Serilog EventLog sink configuration to enable event source creation (if the app is running with admin privileges). (#1963)
1 parent c0e06e9 commit 274072b

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

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

+20-13
Original file line numberDiff line numberDiff line change
@@ -115,19 +115,26 @@ private static LoggerConfiguration ConfigureEventLogSink(this LoggerConfiguratio
115115
#if NETFRAMEWORK
116116
const string eventLogName = "Application";
117117
const string eventLogSourceName = "New Relic .NET Agent";
118-
119-
loggerConfiguration
120-
.WriteTo.Logger(configuration =>
121-
{
122-
configuration
123-
.ExcludeAuditLog()
124-
.WriteTo.EventLog(
125-
source: eventLogSourceName,
126-
logName: eventLogName,
127-
restrictedToMinimumLevel: LogEventLevel.Warning,
128-
outputTemplate: "{Level}: {Message}{NewLine}{Exception}"
129-
);
130-
});
118+
try
119+
{
120+
loggerConfiguration
121+
.WriteTo.Logger(configuration =>
122+
{
123+
configuration
124+
.ExcludeAuditLog()
125+
.WriteTo.EventLog(
126+
source: eventLogSourceName,
127+
logName: eventLogName,
128+
restrictedToMinimumLevel: LogEventLevel.Warning,
129+
outputTemplate: "{Level}: {Message}{NewLine}{Exception}",
130+
manageEventSource: true // Serilog will create the event source if it doesn't exist *and* if the app is running with admin privileges
131+
);
132+
});
133+
}
134+
catch
135+
{
136+
// 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
137+
}
131138
#endif
132139
return loggerConfiguration;
133140
}

0 commit comments

Comments
 (0)