|
11 | 11 | using Logger = NewRelic.Agent.Core.Logging.Logger;
|
12 | 12 | using NewRelic.Agent.Core.Logging;
|
13 | 13 | using Serilog.Templates;
|
14 |
| -#if NETFRAMEWORK |
15 | 14 | using Serilog.Events;
|
| 15 | +#if NETSTANDARD2_0 |
| 16 | +using System.Runtime.InteropServices; |
16 | 17 | #endif
|
17 | 18 |
|
18 | 19 | namespace NewRelic.Agent.Core
|
@@ -107,35 +108,49 @@ private static LoggerConfiguration ConfigureInMemoryLogSink(this LoggerConfigura
|
107 | 108 | }
|
108 | 109 |
|
109 | 110 | /// <summary>
|
110 |
| - /// Add the Event Log sink if running on .NET Framework |
| 111 | + /// Configure an Event Log sink if running on Windows. Logs messages at Warning level and above. Intended for |
| 112 | + /// use during bootstrapping and as a fallback if the file logging sink can't be created. |
| 113 | + /// |
| 114 | + /// The Agent will create the event log source if it doesn't exist *and* if the app is running with |
| 115 | + /// administrator privileges. Otherwise, it will silently do nothing. |
| 116 | + /// |
| 117 | + /// It is possible to manually create the event log source in an elevated Powershell window: |
| 118 | + /// New-EventLog -LogName "Application" -Source "New Relic .NET Agent" |
| 119 | + /// |
111 | 120 | /// </summary>
|
112 | 121 | /// <param name="loggerConfiguration"></param>
|
113 | 122 | private static LoggerConfiguration ConfigureEventLogSink(this LoggerConfiguration loggerConfiguration)
|
114 | 123 | {
|
115 |
| -#if NETFRAMEWORK |
116 |
| - const string eventLogName = "Application"; |
117 |
| - const string eventLogSourceName = "New Relic .NET Agent"; |
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 |
| 124 | +#if NETSTANDARD2_0 |
| 125 | + var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); |
| 126 | +#else |
| 127 | + var isWindows = true; |
| 128 | +#endif |
| 129 | + if (isWindows) |
135 | 130 | {
|
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 |
| 131 | + const string eventLogName = "Application"; |
| 132 | + const string eventLogSourceName = "New Relic .NET Agent"; |
| 133 | + try |
| 134 | + { |
| 135 | + loggerConfiguration |
| 136 | + .WriteTo.Logger(configuration => |
| 137 | + { |
| 138 | + configuration |
| 139 | + .ExcludeAuditLog() |
| 140 | + .WriteTo.EventLog( |
| 141 | + source: eventLogSourceName, |
| 142 | + logName: eventLogName, |
| 143 | + restrictedToMinimumLevel: LogEventLevel.Warning, |
| 144 | + outputTemplate: "{Level}: {Message}{NewLine}{Exception}", |
| 145 | + manageEventSource: true |
| 146 | + ); |
| 147 | + }); |
| 148 | + } |
| 149 | + catch |
| 150 | + { |
| 151 | + // 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 |
| 152 | + } |
137 | 153 | }
|
138 |
| -#endif |
139 | 154 | return loggerConfiguration;
|
140 | 155 | }
|
141 | 156 |
|
@@ -197,11 +212,10 @@ private static LoggerConfiguration ConfigureFileSink(this LoggerConfiguration lo
|
197 | 212 | catch (Exception ex)
|
198 | 213 | {
|
199 | 214 | Log.Logger.Warning(ex, "Unexpected exception when configuring file sink.");
|
200 |
| -#if NETFRAMEWORK |
| 215 | + |
201 | 216 | // Fallback to the event log sink if we cannot setup a file logger.
|
202 | 217 | Log.Logger.Warning("Falling back to EventLog sink.");
|
203 | 218 | loggerConfiguration.ConfigureEventLogSink();
|
204 |
| -#endif |
205 | 219 | }
|
206 | 220 |
|
207 | 221 | return loggerConfiguration;
|
|
0 commit comments