diff --git a/src/OpenTelemetry.AutoInstrumentation.AspNetCoreBootstrapper/BootstrapperEventSource.cs b/src/OpenTelemetry.AutoInstrumentation.AspNetCoreBootstrapper/BootstrapperEventSource.cs deleted file mode 100644 index e6d3d3e09..000000000 --- a/src/OpenTelemetry.AutoInstrumentation.AspNetCoreBootstrapper/BootstrapperEventSource.cs +++ /dev/null @@ -1,46 +0,0 @@ -// -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -using System.Diagnostics.Tracing; - -namespace OpenTelemetry.AutoInstrumentation.AspNetCoreBootstrapper; - -/// -/// EventSource implementation for OpenTelemetry BootstrapperHostingStartup implementation. -/// -[EventSource(Name = "OpenTelemetry-Instrumentation-BootstrapperHostingStartup")] -internal class BootstrapperEventSource : EventSource -{ -#pragma warning disable SA1401 // Fields should be private - public static readonly BootstrapperEventSource Log = new BootstrapperEventSource(); -#pragma warning restore SA1401 // Fields should be private - - /// Logs as Trace level message. - /// Message to log. - [Event(1, Message = "{0}", Level = EventLevel.Informational)] - public void Trace(string message) - { - this.WriteEvent(1, message); - } - - /// Logs as Error level message. - /// Error to log. - [Event(2, Message = "{0}", Level = EventLevel.Error)] - public void Error(string message) - { - this.WriteEvent(2, message); - } -} diff --git a/src/OpenTelemetry.AutoInstrumentation.AspNetCoreBootstrapper/BootstrapperHostingStartup.cs b/src/OpenTelemetry.AutoInstrumentation.AspNetCoreBootstrapper/BootstrapperHostingStartup.cs index a8bbf61a2..8c9c57afe 100644 --- a/src/OpenTelemetry.AutoInstrumentation.AspNetCoreBootstrapper/BootstrapperHostingStartup.cs +++ b/src/OpenTelemetry.AutoInstrumentation.AspNetCoreBootstrapper/BootstrapperHostingStartup.cs @@ -17,6 +17,7 @@ using Microsoft.AspNetCore.Hosting; using OpenTelemetry.AutoInstrumentation.Configurations; using OpenTelemetry.AutoInstrumentation.Logger; +using OpenTelemetry.AutoInstrumentation.Logging; [assembly: HostingStartup(typeof(OpenTelemetry.AutoInstrumentation.AspNetCoreBootstrapper.BootstrapperHostingStartup))] @@ -27,6 +28,8 @@ namespace OpenTelemetry.AutoInstrumentation.AspNetCoreBootstrapper; /// internal class BootstrapperHostingStartup : IHostingStartup { + private static readonly IOtelLogger Logger = OtelLogging.GetLogger("AspNetCoreBootstrapper"); + private readonly LogSettings _logSettings; /// @@ -45,13 +48,13 @@ internal class BootstrapperHostingStartup : IHostingStartup { if (!_logSettings.LogsEnabled) { - Logger.LogInformation("BootstrapperHostingStartup loaded, but OpenTelemetry Logs disabled. Skipping."); + Logger.Information("BootstrapperHostingStartup loaded, but OpenTelemetry Logs disabled. Skipping."); return; } if (!_logSettings.EnabledInstrumentations.Contains(LogInstrumentation.ILogger)) { - Logger.LogInformation($"BootstrapperHostingStartup loaded, but {nameof(LogInstrumentation.ILogger)} instrumentation is disabled. Skipping."); + Logger.Information($"BootstrapperHostingStartup loaded, but {nameof(LogInstrumentation.ILogger)} instrumentation is disabled. Skipping."); return; } @@ -60,11 +63,11 @@ internal class BootstrapperHostingStartup : IHostingStartup builder.ConfigureLogging(logging => logging.AddOpenTelemetryLogs()); var applicationName = GetApplicationName(); - Logger.LogInformation($"BootstrapperHostingStartup loaded for application with name {applicationName}."); + Logger.Information($"BootstrapperHostingStartup loaded for application with name {applicationName}."); } catch (Exception ex) { - Logger.LogError($"Error in BootstrapperHostingStartup: {ex}"); + Logger.Error($"Error in BootstrapperHostingStartup: {ex}"); throw; } } @@ -77,7 +80,7 @@ internal class BootstrapperHostingStartup : IHostingStartup } catch (Exception ex) { - Logger.LogError($"Error getting AppDomain.CurrentDomain.FriendlyName: {ex}"); + Logger.Error($"Error getting AppDomain.CurrentDomain.FriendlyName: {ex}"); return string.Empty; } } diff --git a/src/OpenTelemetry.AutoInstrumentation.StartupHook/Logger.cs b/src/OpenTelemetry.AutoInstrumentation.StartupHook/AutoInstrumentationEventSource.Name.cs similarity index 51% rename from src/OpenTelemetry.AutoInstrumentation.StartupHook/Logger.cs rename to src/OpenTelemetry.AutoInstrumentation.StartupHook/AutoInstrumentationEventSource.Name.cs index df81fe70e..cc0abeee6 100644 --- a/src/OpenTelemetry.AutoInstrumentation.StartupHook/Logger.cs +++ b/src/OpenTelemetry.AutoInstrumentation.StartupHook/AutoInstrumentationEventSource.Name.cs @@ -1,4 +1,4 @@ -// +// // Copyright The OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,23 +14,11 @@ // limitations under the License. // -using OpenTelemetry.AutoInstrumentation.Logging; +using System.Diagnostics.Tracing; -namespace OpenTelemetry.AutoInstrumentation.StartupHook; +namespace OpenTelemetry.AutoInstrumentation; -internal static class Logger +[EventSource(Name = "OpenTelemetry-AutoInstrumentation-StartupHook")] +internal partial class AutoInstrumentationEventSource { - private static readonly IOtelLogger Log = OtelLogging.GetLogger("StartupHook"); - - internal static void LogInformation(string message) - { - StartupHookEventSource.Log.Trace(message); - Log.Information(message); - } - - internal static void LogError(string message) - { - StartupHookEventSource.Log.Error(message); - Log.Error(message); - } } diff --git a/src/OpenTelemetry.AutoInstrumentation.StartupHook/OpenTelemetry.AutoInstrumentation.StartupHook.csproj b/src/OpenTelemetry.AutoInstrumentation.StartupHook/OpenTelemetry.AutoInstrumentation.StartupHook.csproj index 187f4153b..bd4912e6a 100644 --- a/src/OpenTelemetry.AutoInstrumentation.StartupHook/OpenTelemetry.AutoInstrumentation.StartupHook.csproj +++ b/src/OpenTelemetry.AutoInstrumentation.StartupHook/OpenTelemetry.AutoInstrumentation.StartupHook.csproj @@ -48,6 +48,9 @@ Logging\WriteCountingStream.cs + + AutoInstrumentationEventSource.cs + diff --git a/src/OpenTelemetry.AutoInstrumentation.StartupHook/StartupHook.cs b/src/OpenTelemetry.AutoInstrumentation.StartupHook/StartupHook.cs index 9ab4a57fb..b54aeb26b 100644 --- a/src/OpenTelemetry.AutoInstrumentation.StartupHook/StartupHook.cs +++ b/src/OpenTelemetry.AutoInstrumentation.StartupHook/StartupHook.cs @@ -18,13 +18,15 @@ using System.Diagnostics; using System.Reflection; using System.Runtime.Versioning; using OpenTelemetry.AutoInstrumentation; -using OpenTelemetry.AutoInstrumentation.StartupHook; +using OpenTelemetry.AutoInstrumentation.Logging; /// /// Dotnet StartupHook /// internal class StartupHook { + private static readonly IOtelLogger Logger = OtelLogging.GetLogger("StartupHook"); + /// /// Load and initialize OpenTelemetry.AutoInstrumentation assembly to bring OpenTelemetry SDK /// with a pre-defined set of exporters, shims, and instrumentations. @@ -42,21 +44,21 @@ internal class StartupHook if (appTargetFrameworkVersion < minSupportedFramework.Version) { - Logger.LogInformation($"Error in StartupHook initialization: {appTargetFramework} is not supported"); + Logger.Information($"Error in StartupHook initialization: {appTargetFramework} is not supported"); return; } } var applicationName = GetApplicationName(); - Logger.LogInformation($"StartupHook loaded for application with name {applicationName}."); + Logger.Information($"StartupHook loaded for application with name {applicationName}."); if (IsApplicationInExcludeList(applicationName)) { - Logger.LogInformation("Application is in the exclusion list. Skipping initialization."); + Logger.Information("Application is in the exclusion list. Skipping initialization."); return; } - Logger.LogInformation("Attempting initialization."); + Logger.Information("Attempting initialization."); string loaderAssemblyLocation = GetLoaderAssemblyLocation(); @@ -67,7 +69,7 @@ internal class StartupHook if (profilerType != null) { - Logger.LogError( + Logger.Error( $"OpenTelemetry.AutoInstrumentation assembly was already loaded from: {profilerType.Assembly?.Location}. " + "Check the DOTNET_STARTUP_HOOK environment variable."); } @@ -83,17 +85,17 @@ internal class StartupHook var loaderInstance = loaderAssembly.CreateInstance("OpenTelemetry.AutoInstrumentation.Loader.Loader"); if (loaderInstance is null) { - Logger.LogError("StartupHook failed to create an instance of the Loader"); + Logger.Error("StartupHook failed to create an instance of the Loader"); } else { - Logger.LogInformation("StartupHook initialized successfully!"); + Logger.Information("StartupHook initialized successfully!"); } } } catch (Exception ex) { - Logger.LogError($"Error in StartupHook initialization: LoaderFolderLocation: {loaderAssemblyLocation}, Error: {ex}"); + Logger.Error($"Error in StartupHook initialization: LoaderFolderLocation: {loaderAssemblyLocation}, Error: {ex}"); throw; } } @@ -116,7 +118,7 @@ internal class StartupHook } catch (Exception ex) { - Logger.LogError($"Error getting loader directory location: {ex}"); + Logger.Error($"Error getting loader directory location: {ex}"); throw; } } @@ -129,7 +131,7 @@ internal class StartupHook } catch (Exception ex) { - Logger.LogError($"Error getting AppDomain.CurrentDomain.FriendlyName: {ex}"); + Logger.Error($"Error getting AppDomain.CurrentDomain.FriendlyName: {ex}"); return string.Empty; } } @@ -169,7 +171,7 @@ internal class StartupHook } catch (Exception ex) { - Logger.LogError($"Error getting environment variable {variableName}: {ex}"); + Logger.Error($"Error getting environment variable {variableName}: {ex}"); return null; } } @@ -202,7 +204,7 @@ internal class StartupHook catch (Exception ex) { // Exception in evaluation should not throw or crash the process. - Logger.LogInformation($"Couldn't evaluate reference to OpenTelemetry Sdk in an app. Exception: {ex}"); + Logger.Information($"Couldn't evaluate reference to OpenTelemetry Sdk in an app. Exception: {ex}"); } if (oTelPackageVersion != null) diff --git a/src/OpenTelemetry.AutoInstrumentation.StartupHook/StartupHookEventSource.cs b/src/OpenTelemetry.AutoInstrumentation.StartupHook/StartupHookEventSource.cs deleted file mode 100644 index 732366d43..000000000 --- a/src/OpenTelemetry.AutoInstrumentation.StartupHook/StartupHookEventSource.cs +++ /dev/null @@ -1,46 +0,0 @@ -// -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -using System.Diagnostics.Tracing; - -namespace OpenTelemetry.AutoInstrumentation.StartupHook; - -/// -/// EventSource implementation for OpenTelemetry StartupHook implementation. -/// -[EventSource(Name = "OpenTelemetry-Instrumentation-StartupHook")] -internal class StartupHookEventSource : EventSource -{ -#pragma warning disable SA1401 // Fields should be private - public static StartupHookEventSource Log = new StartupHookEventSource(); -#pragma warning restore SA1401 // Fields should be private - - /// Logs as Trace level message. - /// Message to log. - [Event(1, Message = "{0}", Level = EventLevel.Informational)] - public void Trace(string message) - { - this.WriteEvent(1, message); - } - - /// Logs as Error level message. - /// Error to log. - [Event(2, Message = "{0}", Level = EventLevel.Error)] - public void Error(string message) - { - this.WriteEvent(2, message); - } -} diff --git a/src/OpenTelemetry.AutoInstrumentation.AspNetCoreBootstrapper/Logger.cs b/src/OpenTelemetry.AutoInstrumentation/AutoInstrumentationEventSource.Name.cs similarity index 50% rename from src/OpenTelemetry.AutoInstrumentation.AspNetCoreBootstrapper/Logger.cs rename to src/OpenTelemetry.AutoInstrumentation/AutoInstrumentationEventSource.Name.cs index 7282c1da7..d1b85e524 100644 --- a/src/OpenTelemetry.AutoInstrumentation.AspNetCoreBootstrapper/Logger.cs +++ b/src/OpenTelemetry.AutoInstrumentation/AutoInstrumentationEventSource.Name.cs @@ -1,4 +1,4 @@ -// +// // Copyright The OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,23 +14,11 @@ // limitations under the License. // -using OpenTelemetry.AutoInstrumentation.Logging; +using System.Diagnostics.Tracing; -namespace OpenTelemetry.AutoInstrumentation.AspNetCoreBootstrapper; +namespace OpenTelemetry.AutoInstrumentation; -internal static class Logger +[EventSource(Name = "OpenTelemetry-AutoInstrumentation")] +internal partial class AutoInstrumentationEventSource { - private static readonly IOtelLogger Log = OtelLogging.GetLogger("AspNetCoreBootstrapper"); - - internal static void LogInformation(string message) - { - BootstrapperEventSource.Log.Trace(message); - Log.Information(message); - } - - internal static void LogError(string message) - { - BootstrapperEventSource.Log.Error(message); - Log.Error(message); - } } diff --git a/src/OpenTelemetry.AutoInstrumentation/AutoInstrumentationEventSource.cs b/src/OpenTelemetry.AutoInstrumentation/AutoInstrumentationEventSource.cs index 4791434b2..a98a06563 100644 --- a/src/OpenTelemetry.AutoInstrumentation/AutoInstrumentationEventSource.cs +++ b/src/OpenTelemetry.AutoInstrumentation/AutoInstrumentationEventSource.cs @@ -21,26 +21,43 @@ namespace OpenTelemetry.AutoInstrumentation; /// /// EventSource implementation for OpenTelemetry AutoInstrumentation implementation. /// -[EventSource(Name = "OpenTelemetry-AutoInstrumentation")] -internal class AutoInstrumentationEventSource : EventSource +internal partial class AutoInstrumentationEventSource : EventSource { -#pragma warning disable SA1401 // Fields should be private - public static readonly AutoInstrumentationEventSource Log = new AutoInstrumentationEventSource(); -#pragma warning restore SA1401 // Fields should be private - - /// Logs as Trace level message. - /// Message to log. - [Event(1, Message = "{0}", Level = EventLevel.Informational)] - public void Trace(string message) + private AutoInstrumentationEventSource() { - this.WriteEvent(1, message); } + public static AutoInstrumentationEventSource Log { get; } = new(); + /// Logs as Error level message. /// Error to log. [Event(2, Message = "{0}", Level = EventLevel.Error)] public void Error(string message) { - this.WriteEvent(2, message); + WriteEvent(2, message); + } + + /// Logs as Warning level message. + /// Message to log. + [Event(3, Message = "{0}", Level = EventLevel.Warning)] + public void Warning(string message) + { + WriteEvent(3, message); + } + + /// Logs as Information level message. + /// Message to log. + [Event(4, Message = "{0}", Level = EventLevel.Informational)] + public void Information(string message) + { + WriteEvent(4, message); + } + + /// Logs as Warning level message. + /// Message to log. + [Event(5, Message = "{0}", Level = EventLevel.Verbose)] + public void Verbose(string message) + { + WriteEvent(5, message); } } diff --git a/src/OpenTelemetry.AutoInstrumentation/Diagnostics/SdkSelfDiagnosticsEventListener.cs b/src/OpenTelemetry.AutoInstrumentation/Diagnostics/SdkSelfDiagnosticsEventListener.cs index eaa059e17..e23402229 100644 --- a/src/OpenTelemetry.AutoInstrumentation/Diagnostics/SdkSelfDiagnosticsEventListener.cs +++ b/src/OpenTelemetry.AutoInstrumentation/Diagnostics/SdkSelfDiagnosticsEventListener.cs @@ -106,17 +106,17 @@ internal class SdkSelfDiagnosticsEventListener : EventListener { case EventLevel.Critical: case EventLevel.Error: - log.Error("EventSource={0}, Message={1}", eventData.EventSource.Name, message); + log.Error("EventSource={0}, Message={1}", eventData.EventSource.Name, message, false); break; case EventLevel.Warning: - log.Warning("EventSource={0}, Message={1}", eventData.EventSource.Name, message); + log.Warning("EventSource={0}, Message={1}", eventData.EventSource.Name, message, false); break; case EventLevel.LogAlways: case EventLevel.Informational: - log.Information("EventSource={0}, Message={1}", eventData.EventSource.Name, message); + log.Information("EventSource={0}, Message={1}", eventData.EventSource.Name, message, false); break; case EventLevel.Verbose: - log.Debug("EventSource={0}, Message={1}", eventData.EventSource.Name, message); + log.Debug("EventSource={0}, Message={1}", eventData.EventSource.Name, message, false); break; } } diff --git a/src/OpenTelemetry.AutoInstrumentation/Logger/LogBuilderExtensions.cs b/src/OpenTelemetry.AutoInstrumentation/Logger/LogBuilderExtensions.cs index 03349da30..1398376c3 100644 --- a/src/OpenTelemetry.AutoInstrumentation/Logger/LogBuilderExtensions.cs +++ b/src/OpenTelemetry.AutoInstrumentation/Logger/LogBuilderExtensions.cs @@ -80,7 +80,7 @@ internal static class LogBuilderExtensions } }); - AutoInstrumentationEventSource.Log.Trace($"Logs: Loaded AddOpenTelemetry from LoggingBuilder."); + AutoInstrumentationEventSource.Log.Information($"Logs: Loaded AddOpenTelemetry from LoggingBuilder."); } catch (Exception ex) { diff --git a/src/OpenTelemetry.AutoInstrumentation/Logging/CustomLogger.cs b/src/OpenTelemetry.AutoInstrumentation/Logging/CustomLogger.cs index d482e5b64..a73fb2131 100644 --- a/src/OpenTelemetry.AutoInstrumentation/Logging/CustomLogger.cs +++ b/src/OpenTelemetry.AutoInstrumentation/Logging/CustomLogger.cs @@ -33,174 +33,195 @@ internal class CustomLogger : IOtelLogger return true; } - public void Debug(string messageTemplate) - => Write(LogLevel.Debug, exception: null, messageTemplate, NoPropertyValues); + public void Debug(string messageTemplate, bool writeToEventLog) + => Write(LogLevel.Debug, exception: null, messageTemplate, NoPropertyValues, writeToEventLog); - public void Debug(string messageTemplate, T property) - => Write(LogLevel.Debug, exception: null, messageTemplate, property); + public void Debug(string messageTemplate, T property, bool writeToEventLog) + => Write(LogLevel.Debug, exception: null, messageTemplate, property, writeToEventLog); - public void Debug(string messageTemplate, T0 property0, T1 property1) - => Write(LogLevel.Debug, exception: null, messageTemplate, property0, property1); + public void Debug(string messageTemplate, T0 property0, T1 property1, bool writeToEventLog) + => Write(LogLevel.Debug, exception: null, messageTemplate, property0, property1, writeToEventLog); - public void Debug(string messageTemplate, T0 property0, T1 property1, T2 property2) - => Write(LogLevel.Debug, exception: null, messageTemplate, property0, property1, property2); + public void Debug(string messageTemplate, T0 property0, T1 property1, T2 property2, bool writeToEventLog) + => Write(LogLevel.Debug, exception: null, messageTemplate, property0, property1, property2, writeToEventLog); - public void Debug(string messageTemplate, object[] args) - => Write(LogLevel.Debug, exception: null, messageTemplate, args); + public void Debug(string messageTemplate, object[] args, bool writeToEventLog) + => Write(LogLevel.Debug, exception: null, messageTemplate, args, writeToEventLog); - public void Debug(Exception exception, string messageTemplate) - => Write(LogLevel.Debug, exception, messageTemplate, NoPropertyValues); + public void Debug(Exception exception, string messageTemplate, bool writeToEventLog) + => Write(LogLevel.Debug, exception, messageTemplate, NoPropertyValues, writeToEventLog); - public void Debug(Exception exception, string messageTemplate, T property) - => Write(LogLevel.Debug, exception, messageTemplate, property); + public void Debug(Exception exception, string messageTemplate, T property, bool writeToEventLog) + => Write(LogLevel.Debug, exception, messageTemplate, property, writeToEventLog); - public void Debug(Exception exception, string messageTemplate, T0 property0, T1 property1) - => Write(LogLevel.Debug, exception, messageTemplate, property0, property1); + public void Debug(Exception exception, string messageTemplate, T0 property0, T1 property1, bool writeToEventLog) + => Write(LogLevel.Debug, exception, messageTemplate, property0, property1, writeToEventLog); - public void Debug(Exception exception, string messageTemplate, T0 property0, T1 property1, T2 property2) - => Write(LogLevel.Debug, exception, messageTemplate, property0, property1, property2); + public void Debug(Exception exception, string messageTemplate, T0 property0, T1 property1, T2 property2, bool writeToEventLog) + => Write(LogLevel.Debug, exception, messageTemplate, property0, property1, property2, writeToEventLog); - public void Debug(Exception exception, string messageTemplate, object[] args) - => Write(LogLevel.Debug, exception, messageTemplate, args); + public void Debug(Exception exception, string messageTemplate, object[] args, bool writeToEventLog) + => Write(LogLevel.Debug, exception, messageTemplate, args, writeToEventLog); - public void Information(string messageTemplate) - => Write(LogLevel.Information, exception: null, messageTemplate, NoPropertyValues); + public void Information(string messageTemplate, bool writeToEventLog) + => Write(LogLevel.Information, exception: null, messageTemplate, NoPropertyValues, writeToEventLog); - public void Information(string messageTemplate, T property) - => Write(LogLevel.Information, exception: null, messageTemplate, property); + public void Information(string messageTemplate, T property, bool writeToEventLog) + => Write(LogLevel.Information, exception: null, messageTemplate, property, writeToEventLog); - public void Information(string messageTemplate, T0 property0, T1 property1) - => Write(LogLevel.Information, exception: null, messageTemplate, property0, property1); + public void Information(string messageTemplate, T0 property0, T1 property1, bool writeToEventLog) + => Write(LogLevel.Information, exception: null, messageTemplate, property0, property1, writeToEventLog); - public void Information(string messageTemplate, T0 property0, T1 property1, T2 property2) - => Write(LogLevel.Information, exception: null, messageTemplate, property0, property1, property2); + public void Information(string messageTemplate, T0 property0, T1 property1, T2 property2, bool writeToEventLog) + => Write(LogLevel.Information, exception: null, messageTemplate, property0, property1, property2, writeToEventLog); - public void Information(string messageTemplate, object[] args) - => Write(LogLevel.Information, exception: null, messageTemplate, args); + public void Information(string messageTemplate, object[] args, bool writeToEventLog) + => Write(LogLevel.Information, exception: null, messageTemplate, args, writeToEventLog); - public void Information(Exception exception, string messageTemplate) - => Write(LogLevel.Information, exception, messageTemplate, NoPropertyValues); + public void Information(Exception exception, string messageTemplate, bool writeToEventLog) + => Write(LogLevel.Information, exception, messageTemplate, NoPropertyValues, writeToEventLog); - public void Information(Exception exception, string messageTemplate, T property) - => Write(LogLevel.Information, exception, messageTemplate, property); + public void Information(Exception exception, string messageTemplate, T property, bool writeToEventLog) + => Write(LogLevel.Information, exception, messageTemplate, property, writeToEventLog); - public void Information(Exception exception, string messageTemplate, T0 property0, T1 property1) - => Write(LogLevel.Information, exception, messageTemplate, property0, property1); + public void Information(Exception exception, string messageTemplate, T0 property0, T1 property1, bool writeToEventLog) + => Write(LogLevel.Information, exception, messageTemplate, property0, property1, writeToEventLog); - public void Information(Exception exception, string messageTemplate, T0 property0, T1 property1, T2 property2) - => Write(LogLevel.Information, exception, messageTemplate, property0, property1, property2); + public void Information(Exception exception, string messageTemplate, T0 property0, T1 property1, T2 property2, bool writeToEventLog) + => Write(LogLevel.Information, exception, messageTemplate, property0, property1, property2, writeToEventLog); - public void Information(Exception exception, string messageTemplate, object[] args) - => Write(LogLevel.Information, exception, messageTemplate, args); + public void Information(Exception exception, string messageTemplate, object[] args, bool writeToEventLog) + => Write(LogLevel.Information, exception, messageTemplate, args, writeToEventLog); - public void Warning(string messageTemplate) - => Write(LogLevel.Warning, exception: null, messageTemplate, NoPropertyValues); + public void Warning(string messageTemplate, bool writeToEventLog) + => Write(LogLevel.Warning, exception: null, messageTemplate, NoPropertyValues, writeToEventLog); - public void Warning(string messageTemplate, T property) - => Write(LogLevel.Warning, exception: null, messageTemplate, property); + public void Warning(string messageTemplate, T property, bool writeToEventLog) + => Write(LogLevel.Warning, exception: null, messageTemplate, property, writeToEventLog); - public void Warning(string messageTemplate, T0 property0, T1 property1) - => Write(LogLevel.Warning, exception: null, messageTemplate, property0, property1); + public void Warning(string messageTemplate, T0 property0, T1 property1, bool writeToEventLog) + => Write(LogLevel.Warning, exception: null, messageTemplate, property0, property1, writeToEventLog); - public void Warning(string messageTemplate, T0 property0, T1 property1, T2 property2) - => Write(LogLevel.Warning, exception: null, messageTemplate, property0, property1, property2); + public void Warning(string messageTemplate, T0 property0, T1 property1, T2 property2, bool writeToEventLog) + => Write(LogLevel.Warning, exception: null, messageTemplate, property0, property1, property2, writeToEventLog); - public void Warning(string messageTemplate, object[] args) - => Write(LogLevel.Warning, exception: null, messageTemplate, args); + public void Warning(string messageTemplate, object[] args, bool writeToEventLog) + => Write(LogLevel.Warning, exception: null, messageTemplate, args, writeToEventLog); - public void Warning(Exception exception, string messageTemplate) - => Write(LogLevel.Warning, exception, messageTemplate, NoPropertyValues); + public void Warning(Exception exception, string messageTemplate, bool writeToEventLog) + => Write(LogLevel.Warning, exception, messageTemplate, NoPropertyValues, writeToEventLog); - public void Warning(Exception exception, string messageTemplate, T property) - => Write(LogLevel.Warning, exception, messageTemplate, property); + public void Warning(Exception exception, string messageTemplate, T property, bool writeToEventLog) + => Write(LogLevel.Warning, exception, messageTemplate, property, writeToEventLog); - public void Warning(Exception exception, string messageTemplate, T0 property0, T1 property1) - => Write(LogLevel.Warning, exception, messageTemplate, property0, property1); + public void Warning(Exception exception, string messageTemplate, T0 property0, T1 property1, bool writeToEventLog) + => Write(LogLevel.Warning, exception, messageTemplate, property0, property1, writeToEventLog); - public void Warning(Exception exception, string messageTemplate, T0 property0, T1 property1, T2 property2) - => Write(LogLevel.Warning, exception, messageTemplate, property0, property1, property2); + public void Warning(Exception exception, string messageTemplate, T0 property0, T1 property1, T2 property2, bool writeToEventLog) + => Write(LogLevel.Warning, exception, messageTemplate, property0, property1, property2, writeToEventLog); - public void Warning(Exception exception, string messageTemplate, object[] args) - => Write(LogLevel.Warning, exception, messageTemplate, args); + public void Warning(Exception exception, string messageTemplate, object[] args, bool writeToEventLog) + => Write(LogLevel.Warning, exception, messageTemplate, args, writeToEventLog); - public void Error(string messageTemplate) - => Write(LogLevel.Error, exception: null, messageTemplate, NoPropertyValues); + public void Error(string messageTemplate, bool writeToEventLog) + => Write(LogLevel.Error, exception: null, messageTemplate, NoPropertyValues, writeToEventLog); - public void Error(string messageTemplate, T property) - => Write(LogLevel.Error, exception: null, messageTemplate, property); + public void Error(string messageTemplate, T property, bool writeToEventLog) + => Write(LogLevel.Error, exception: null, messageTemplate, property, writeToEventLog); - public void Error(string messageTemplate, T0 property0, T1 property1) - => Write(LogLevel.Error, exception: null, messageTemplate, property0, property1); + public void Error(string messageTemplate, T0 property0, T1 property1, bool writeToEventLog) + => Write(LogLevel.Error, exception: null, messageTemplate, property0, property1, writeToEventLog); - public void Error(string messageTemplate, T0 property0, T1 property1, T2 property2) - => Write(LogLevel.Error, exception: null, messageTemplate, property0, property1, property2); + public void Error(string messageTemplate, T0 property0, T1 property1, T2 property2, bool writeToEventLog) + => Write(LogLevel.Error, exception: null, messageTemplate, property0, property1, property2, writeToEventLog); - public void Error(string messageTemplate, object[] args) - => Write(LogLevel.Error, exception: null, messageTemplate, args); + public void Error(string messageTemplate, object[] args, bool writeToEventLog) + => Write(LogLevel.Error, exception: null, messageTemplate, args, writeToEventLog); - public void Error(Exception exception, string messageTemplate) - => Write(LogLevel.Error, exception, messageTemplate, NoPropertyValues); + public void Error(Exception exception, string messageTemplate, bool writeToEventLog) + => Write(LogLevel.Error, exception, messageTemplate, NoPropertyValues, writeToEventLog); - public void Error(Exception exception, string messageTemplate, T property) - => Write(LogLevel.Error, exception, messageTemplate, property); + public void Error(Exception exception, string messageTemplate, T property, bool writeToEventLog) + => Write(LogLevel.Error, exception, messageTemplate, property, writeToEventLog); - public void Error(Exception exception, string messageTemplate, T0 property0, T1 property1) - => Write(LogLevel.Error, exception, messageTemplate, property0, property1); + public void Error(Exception exception, string messageTemplate, T0 property0, T1 property1, bool writeToEventLog) + => Write(LogLevel.Error, exception, messageTemplate, property0, property1, writeToEventLog); - public void Error(Exception exception, string messageTemplate, T0 property0, T1 property1, T2 property2) - => Write(LogLevel.Error, exception, messageTemplate, property0, property1, property2); + public void Error(Exception exception, string messageTemplate, T0 property0, T1 property1, T2 property2, bool writeToEventLog) + => Write(LogLevel.Error, exception, messageTemplate, property0, property1, property2, writeToEventLog); - public void Error(Exception exception, string messageTemplate, object[] args) - => Write(LogLevel.Error, exception, messageTemplate, args); + public void Error(Exception exception, string messageTemplate, object[] args, bool writeToEventLog) + => Write(LogLevel.Error, exception, messageTemplate, args, writeToEventLog); - private void Write(LogLevel level, Exception? exception, string messageTemplate, T property) + private static void WriteEventSourceLog(LogLevel level, string message) + { + switch (level) + { + case LogLevel.Information: + AutoInstrumentationEventSource.Log.Information(message); + break; + case LogLevel.Debug: + AutoInstrumentationEventSource.Log.Verbose(message); + break; + case LogLevel.Warning: + AutoInstrumentationEventSource.Log.Warning(message); + break; + case LogLevel.Error: + AutoInstrumentationEventSource.Log.Error(message); + break; + } + } + + private void Write(LogLevel level, Exception? exception, string messageTemplate, T property, bool writeToEventLog) { if (IsEnabled(level)) { // Avoid boxing + array allocation if disabled - WriteImpl(level, exception, messageTemplate, new object?[] { property }); + WriteImpl(level, exception, messageTemplate, new object?[] { property }, writeToEventLog); } } - private void Write(LogLevel level, Exception? exception, string messageTemplate, T0 property0, T1 property1) + private void Write(LogLevel level, Exception? exception, string messageTemplate, T0 property0, T1 property1, bool writeToEventLog) { if (IsEnabled(level)) { // Avoid boxing + array allocation if disabled - WriteImpl(level, exception, messageTemplate, new object?[] { property0, property1 }); + WriteImpl(level, exception, messageTemplate, new object?[] { property0, property1 }, writeToEventLog); } } - private void Write(LogLevel level, Exception? exception, string messageTemplate, T0 property0, T1 property1, T2 property2) + private void Write(LogLevel level, Exception? exception, string messageTemplate, T0 property0, T1 property1, T2 property2, bool writeToEventLog) { if (IsEnabled(level)) { // Avoid boxing + array allocation if disabled - WriteImpl(level, exception, messageTemplate, new object?[] { property0, property1, property2 }); + WriteImpl(level, exception, messageTemplate, new object?[] { property0, property1, property2 }, writeToEventLog); } } - private void Write(LogLevel level, Exception? exception, string messageTemplate, object[] args) + private void Write(LogLevel level, Exception? exception, string messageTemplate, object[] args, bool writeToEventLog) { if (IsEnabled(level)) { // logging is not disabled - WriteImpl(level, exception, messageTemplate, args); + WriteImpl(level, exception, messageTemplate, args, writeToEventLog); } } - private void WriteImpl(LogLevel level, Exception? exception, string messageTemplate, object?[] args) + private void WriteImpl(LogLevel level, Exception? exception, string messageTemplate, object?[] args, bool writeToEventLog) { try { - var message = - $"[{DateTime.UtcNow:O}] [{level}] {string.Format(messageTemplate, args)} {Environment.NewLine}"; - _sink.Write(message); - + var rawMessage = string.Format(messageTemplate, args); if (exception != null) { - var exceptionMessage = $"Exception: {exception.Message}{Environment.NewLine}{exception}{Environment.NewLine}"; - _sink.Write(exceptionMessage); + rawMessage += $"{Environment.NewLine}Exception: {exception.Message}{Environment.NewLine}{exception}"; + } + + _sink.Write($"[{DateTime.UtcNow:O}] [{level}] {rawMessage} {Environment.NewLine}"); + if (writeToEventLog) + { + WriteEventSourceLog(level, rawMessage); } } catch diff --git a/src/OpenTelemetry.AutoInstrumentation/Logging/IOtelLogger.cs b/src/OpenTelemetry.AutoInstrumentation/Logging/IOtelLogger.cs index 885a134a2..1972602a4 100644 --- a/src/OpenTelemetry.AutoInstrumentation/Logging/IOtelLogger.cs +++ b/src/OpenTelemetry.AutoInstrumentation/Logging/IOtelLogger.cs @@ -22,83 +22,83 @@ internal interface IOtelLogger { bool IsEnabled(LogLevel level); - void Debug(string messageTemplate); + void Debug(string messageTemplate, bool writeToEventLog = true); - void Debug(string messageTemplate, T property); + void Debug(string messageTemplate, T property, bool writeToEventLog = true); - void Debug(string messageTemplate, T0 property0, T1 property1); + void Debug(string messageTemplate, T0 property0, T1 property1, bool writeToEventLog = true); - void Debug(string messageTemplate, T0 property0, T1 property1, T2 property2); + void Debug(string messageTemplate, T0 property0, T1 property1, T2 property2, bool writeToEventLog = true); - void Debug(string messageTemplate, object[] args); + void Debug(string messageTemplate, object[] args, bool writeToEventLog = true); - void Debug(Exception exception, string messageTemplate); + void Debug(Exception exception, string messageTemplate, bool writeToEventLog = true); - void Debug(Exception exception, string messageTemplate, T property); + void Debug(Exception exception, string messageTemplate, T property, bool writeToEventLog = true); - void Debug(Exception exception, string messageTemplate, T0 property0, T1 property1); + void Debug(Exception exception, string messageTemplate, T0 property0, T1 property1, bool writeToEventLog = true); - void Debug(Exception exception, string messageTemplate, T0 property0, T1 property1, T2 property2); + void Debug(Exception exception, string messageTemplate, T0 property0, T1 property1, T2 property2, bool writeToEventLog = true); - void Debug(Exception exception, string messageTemplate, object[] args); + void Debug(Exception exception, string messageTemplate, object[] args, bool writeToEventLog = true); - void Information(string messageTemplate); + void Information(string messageTemplate, bool writeToEventLog = true); - void Information(string messageTemplate, T property); + void Information(string messageTemplate, T property, bool writeToEventLog = true); - void Information(string messageTemplate, T0 property0, T1 property1); + void Information(string messageTemplate, T0 property0, T1 property1, bool writeToEventLog = true); - void Information(string messageTemplate, T0 property0, T1 property1, T2 property2); + void Information(string messageTemplate, T0 property0, T1 property1, T2 property2, bool writeToEventLog = true); - void Information(string messageTemplate, object[] args); + void Information(string messageTemplate, object[] args, bool writeToEventLog = true); - void Information(Exception exception, string messageTemplate); + void Information(Exception exception, string messageTemplate, bool writeToEventLog = true); - void Information(Exception exception, string messageTemplate, T property); + void Information(Exception exception, string messageTemplate, T property, bool writeToEventLog = true); - void Information(Exception exception, string messageTemplate, T0 property0, T1 property1); + void Information(Exception exception, string messageTemplate, T0 property0, T1 property1, bool writeToEventLog = true); - void Information(Exception exception, string messageTemplate, T0 property0, T1 property1, T2 property2); + void Information(Exception exception, string messageTemplate, T0 property0, T1 property1, T2 property2, bool writeToEventLog = true); - void Information(Exception exception, string messageTemplate, object[] args); + void Information(Exception exception, string messageTemplate, object[] args, bool writeToEventLog = true); - void Warning(string messageTemplate); + void Warning(string messageTemplate, bool writeToEventLog = true); - void Warning(string messageTemplate, T property); + void Warning(string messageTemplate, T property, bool writeToEventLog = true); - void Warning(string messageTemplate, T0 property0, T1 property1); + void Warning(string messageTemplate, T0 property0, T1 property1, bool writeToEventLog = true); - void Warning(string messageTemplate, T0 property0, T1 property1, T2 property2); + void Warning(string messageTemplate, T0 property0, T1 property1, T2 property2, bool writeToEventLog = true); - void Warning(string messageTemplate, object[] args); + void Warning(string messageTemplate, object[] args, bool writeToEventLog = true); - void Warning(Exception exception, string messageTemplate); + void Warning(Exception exception, string messageTemplate, bool writeToEventLog = true); - void Warning(Exception exception, string messageTemplate, T property); + void Warning(Exception exception, string messageTemplate, T property, bool writeToEventLog = true); - void Warning(Exception exception, string messageTemplate, T0 property0, T1 property1); + void Warning(Exception exception, string messageTemplate, T0 property0, T1 property1, bool writeToEventLog = true); - void Warning(Exception exception, string messageTemplate, T0 property0, T1 property1, T2 property2); + void Warning(Exception exception, string messageTemplate, T0 property0, T1 property1, T2 property2, bool writeToEventLog = true); - void Warning(Exception exception, string messageTemplate, object[] args); + void Warning(Exception exception, string messageTemplate, object[] args, bool writeToEventLog = true); - void Error(string messageTemplate); + void Error(string messageTemplate, bool writeToEventLog = true); - void Error(string messageTemplate, T property); + void Error(string messageTemplate, T property, bool writeToEventLog = true); - void Error(string messageTemplate, T0 property0, T1 property1); + void Error(string messageTemplate, T0 property0, T1 property1, bool writeToEventLog = true); - void Error(string messageTemplate, T0 property0, T1 property1, T2 property2); + void Error(string messageTemplate, T0 property0, T1 property1, T2 property2, bool writeToEventLog = true); - void Error(string messageTemplate, object[] args); + void Error(string messageTemplate, object[] args, bool writeToEventLog = true); - void Error(Exception exception, string messageTemplate); + void Error(Exception exception, string messageTemplate, bool writeToEventLog = true); - void Error(Exception exception, string messageTemplate, T property); + void Error(Exception exception, string messageTemplate, T property, bool writeToEventLog = true); - void Error(Exception exception, string messageTemplate, T0 property0, T1 property1); + void Error(Exception exception, string messageTemplate, T0 property0, T1 property1, bool writeToEventLog = true); - void Error(Exception exception, string messageTemplate, T0 property0, T1 property1, T2 property2); + void Error(Exception exception, string messageTemplate, T0 property0, T1 property1, T2 property2, bool writeToEventLog = true); - void Error(Exception exception, string messageTemplate, object[] args); + void Error(Exception exception, string messageTemplate, object[] args, bool writeToEventLog = true); }