Refactor logging (#2145)

Co-authored-by: Robert Pająk <pellared@hotmail.com>
Co-authored-by: Piotr Kiełkowicz <pkiekowicz@splunk.com>
This commit is contained in:
Dawid Szmigielski 2023-02-27 13:59:02 +01:00 committed by GitHub
parent 775f7b78d6
commit a50fabac12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 226 additions and 296 deletions

View File

@ -1,46 +0,0 @@
// <copyright file="BootstrapperEventSource.cs" company="OpenTelemetry Authors">
// 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.
// </copyright>
using System.Diagnostics.Tracing;
namespace OpenTelemetry.AutoInstrumentation.AspNetCoreBootstrapper;
/// <summary>
/// EventSource implementation for OpenTelemetry BootstrapperHostingStartup implementation.
/// </summary>
[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
/// <summary>Logs as Trace level message.</summary>
/// <param name="message">Message to log.</param>
[Event(1, Message = "{0}", Level = EventLevel.Informational)]
public void Trace(string message)
{
this.WriteEvent(1, message);
}
/// <summary>Logs as Error level message.</summary>
/// <param name="message">Error to log.</param>
[Event(2, Message = "{0}", Level = EventLevel.Error)]
public void Error(string message)
{
this.WriteEvent(2, message);
}
}

View File

@ -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;
/// </summary>
internal class BootstrapperHostingStartup : IHostingStartup
{
private static readonly IOtelLogger Logger = OtelLogging.GetLogger("AspNetCoreBootstrapper");
private readonly LogSettings _logSettings;
/// <summary>
@ -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;
}
}

View File

@ -1,4 +1,4 @@
// <copyright file="Logger.cs" company="OpenTelemetry Authors">
// <copyright file="AutoInstrumentationEventSource.Name.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
@ -14,23 +14,11 @@
// limitations under the License.
// </copyright>
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);
}
}

View File

@ -48,6 +48,9 @@
<Compile Include="..\OpenTelemetry.AutoInstrumentation\Logging\WriteCountingStream.cs">
<Link>Logging\WriteCountingStream.cs</Link>
</Compile>
<Compile Include="..\OpenTelemetry.AutoInstrumentation\AutoInstrumentationEventSource.cs">
<Link>AutoInstrumentationEventSource.cs</Link>
</Compile>
</ItemGroup>
</Project>

View File

@ -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;
/// <summary>
/// Dotnet StartupHook
/// </summary>
internal class StartupHook
{
private static readonly IOtelLogger Logger = OtelLogging.GetLogger("StartupHook");
/// <summary>
/// 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)

View File

@ -1,46 +0,0 @@
// <copyright file="StartupHookEventSource.cs" company="OpenTelemetry Authors">
// 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.
// </copyright>
using System.Diagnostics.Tracing;
namespace OpenTelemetry.AutoInstrumentation.StartupHook;
/// <summary>
/// EventSource implementation for OpenTelemetry StartupHook implementation.
/// </summary>
[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
/// <summary>Logs as Trace level message.</summary>
/// <param name="message">Message to log.</param>
[Event(1, Message = "{0}", Level = EventLevel.Informational)]
public void Trace(string message)
{
this.WriteEvent(1, message);
}
/// <summary>Logs as Error level message.</summary>
/// <param name="message">Error to log.</param>
[Event(2, Message = "{0}", Level = EventLevel.Error)]
public void Error(string message)
{
this.WriteEvent(2, message);
}
}

View File

@ -1,4 +1,4 @@
// <copyright file="Logger.cs" company="OpenTelemetry Authors">
// <copyright file="AutoInstrumentationEventSource.Name.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
@ -14,23 +14,11 @@
// limitations under the License.
// </copyright>
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);
}
}

View File

@ -21,26 +21,43 @@ namespace OpenTelemetry.AutoInstrumentation;
/// <summary>
/// EventSource implementation for OpenTelemetry AutoInstrumentation implementation.
/// </summary>
[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
/// <summary>Logs as Trace level message.</summary>
/// <param name="message">Message to log.</param>
[Event(1, Message = "{0}", Level = EventLevel.Informational)]
public void Trace(string message)
private AutoInstrumentationEventSource()
{
this.WriteEvent(1, message);
}
public static AutoInstrumentationEventSource Log { get; } = new();
/// <summary>Logs as Error level message.</summary>
/// <param name="message">Error to log.</param>
[Event(2, Message = "{0}", Level = EventLevel.Error)]
public void Error(string message)
{
this.WriteEvent(2, message);
WriteEvent(2, message);
}
/// <summary>Logs as Warning level message.</summary>
/// <param name="message">Message to log.</param>
[Event(3, Message = "{0}", Level = EventLevel.Warning)]
public void Warning(string message)
{
WriteEvent(3, message);
}
/// <summary>Logs as Information level message.</summary>
/// <param name="message">Message to log.</param>
[Event(4, Message = "{0}", Level = EventLevel.Informational)]
public void Information(string message)
{
WriteEvent(4, message);
}
/// <summary>Logs as Warning level message.</summary>
/// <param name="message">Message to log.</param>
[Event(5, Message = "{0}", Level = EventLevel.Verbose)]
public void Verbose(string message)
{
WriteEvent(5, message);
}
}

View File

@ -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;
}
}

View File

@ -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)
{

View File

@ -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<T>(string messageTemplate, T property)
=> Write(LogLevel.Debug, exception: null, messageTemplate, property);
public void Debug<T>(string messageTemplate, T property, bool writeToEventLog)
=> Write(LogLevel.Debug, exception: null, messageTemplate, property, writeToEventLog);
public void Debug<T0, T1>(string messageTemplate, T0 property0, T1 property1)
=> Write(LogLevel.Debug, exception: null, messageTemplate, property0, property1);
public void Debug<T0, T1>(string messageTemplate, T0 property0, T1 property1, bool writeToEventLog)
=> Write(LogLevel.Debug, exception: null, messageTemplate, property0, property1, writeToEventLog);
public void Debug<T0, T1, T2>(string messageTemplate, T0 property0, T1 property1, T2 property2)
=> Write(LogLevel.Debug, exception: null, messageTemplate, property0, property1, property2);
public void Debug<T0, T1, T2>(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<T>(Exception exception, string messageTemplate, T property)
=> Write(LogLevel.Debug, exception, messageTemplate, property);
public void Debug<T>(Exception exception, string messageTemplate, T property, bool writeToEventLog)
=> Write(LogLevel.Debug, exception, messageTemplate, property, writeToEventLog);
public void Debug<T0, T1>(Exception exception, string messageTemplate, T0 property0, T1 property1)
=> Write(LogLevel.Debug, exception, messageTemplate, property0, property1);
public void Debug<T0, T1>(Exception exception, string messageTemplate, T0 property0, T1 property1, bool writeToEventLog)
=> Write(LogLevel.Debug, exception, messageTemplate, property0, property1, writeToEventLog);
public void Debug<T0, T1, T2>(Exception exception, string messageTemplate, T0 property0, T1 property1, T2 property2)
=> Write(LogLevel.Debug, exception, messageTemplate, property0, property1, property2);
public void Debug<T0, T1, T2>(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<T>(string messageTemplate, T property)
=> Write(LogLevel.Information, exception: null, messageTemplate, property);
public void Information<T>(string messageTemplate, T property, bool writeToEventLog)
=> Write(LogLevel.Information, exception: null, messageTemplate, property, writeToEventLog);
public void Information<T0, T1>(string messageTemplate, T0 property0, T1 property1)
=> Write(LogLevel.Information, exception: null, messageTemplate, property0, property1);
public void Information<T0, T1>(string messageTemplate, T0 property0, T1 property1, bool writeToEventLog)
=> Write(LogLevel.Information, exception: null, messageTemplate, property0, property1, writeToEventLog);
public void Information<T0, T1, T2>(string messageTemplate, T0 property0, T1 property1, T2 property2)
=> Write(LogLevel.Information, exception: null, messageTemplate, property0, property1, property2);
public void Information<T0, T1, T2>(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<T>(Exception exception, string messageTemplate, T property)
=> Write(LogLevel.Information, exception, messageTemplate, property);
public void Information<T>(Exception exception, string messageTemplate, T property, bool writeToEventLog)
=> Write(LogLevel.Information, exception, messageTemplate, property, writeToEventLog);
public void Information<T0, T1>(Exception exception, string messageTemplate, T0 property0, T1 property1)
=> Write(LogLevel.Information, exception, messageTemplate, property0, property1);
public void Information<T0, T1>(Exception exception, string messageTemplate, T0 property0, T1 property1, bool writeToEventLog)
=> Write(LogLevel.Information, exception, messageTemplate, property0, property1, writeToEventLog);
public void Information<T0, T1, T2>(Exception exception, string messageTemplate, T0 property0, T1 property1, T2 property2)
=> Write(LogLevel.Information, exception, messageTemplate, property0, property1, property2);
public void Information<T0, T1, T2>(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<T>(string messageTemplate, T property)
=> Write(LogLevel.Warning, exception: null, messageTemplate, property);
public void Warning<T>(string messageTemplate, T property, bool writeToEventLog)
=> Write(LogLevel.Warning, exception: null, messageTemplate, property, writeToEventLog);
public void Warning<T0, T1>(string messageTemplate, T0 property0, T1 property1)
=> Write(LogLevel.Warning, exception: null, messageTemplate, property0, property1);
public void Warning<T0, T1>(string messageTemplate, T0 property0, T1 property1, bool writeToEventLog)
=> Write(LogLevel.Warning, exception: null, messageTemplate, property0, property1, writeToEventLog);
public void Warning<T0, T1, T2>(string messageTemplate, T0 property0, T1 property1, T2 property2)
=> Write(LogLevel.Warning, exception: null, messageTemplate, property0, property1, property2);
public void Warning<T0, T1, T2>(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<T>(Exception exception, string messageTemplate, T property)
=> Write(LogLevel.Warning, exception, messageTemplate, property);
public void Warning<T>(Exception exception, string messageTemplate, T property, bool writeToEventLog)
=> Write(LogLevel.Warning, exception, messageTemplate, property, writeToEventLog);
public void Warning<T0, T1>(Exception exception, string messageTemplate, T0 property0, T1 property1)
=> Write(LogLevel.Warning, exception, messageTemplate, property0, property1);
public void Warning<T0, T1>(Exception exception, string messageTemplate, T0 property0, T1 property1, bool writeToEventLog)
=> Write(LogLevel.Warning, exception, messageTemplate, property0, property1, writeToEventLog);
public void Warning<T0, T1, T2>(Exception exception, string messageTemplate, T0 property0, T1 property1, T2 property2)
=> Write(LogLevel.Warning, exception, messageTemplate, property0, property1, property2);
public void Warning<T0, T1, T2>(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<T>(string messageTemplate, T property)
=> Write(LogLevel.Error, exception: null, messageTemplate, property);
public void Error<T>(string messageTemplate, T property, bool writeToEventLog)
=> Write(LogLevel.Error, exception: null, messageTemplate, property, writeToEventLog);
public void Error<T0, T1>(string messageTemplate, T0 property0, T1 property1)
=> Write(LogLevel.Error, exception: null, messageTemplate, property0, property1);
public void Error<T0, T1>(string messageTemplate, T0 property0, T1 property1, bool writeToEventLog)
=> Write(LogLevel.Error, exception: null, messageTemplate, property0, property1, writeToEventLog);
public void Error<T0, T1, T2>(string messageTemplate, T0 property0, T1 property1, T2 property2)
=> Write(LogLevel.Error, exception: null, messageTemplate, property0, property1, property2);
public void Error<T0, T1, T2>(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<T>(Exception exception, string messageTemplate, T property)
=> Write(LogLevel.Error, exception, messageTemplate, property);
public void Error<T>(Exception exception, string messageTemplate, T property, bool writeToEventLog)
=> Write(LogLevel.Error, exception, messageTemplate, property, writeToEventLog);
public void Error<T0, T1>(Exception exception, string messageTemplate, T0 property0, T1 property1)
=> Write(LogLevel.Error, exception, messageTemplate, property0, property1);
public void Error<T0, T1>(Exception exception, string messageTemplate, T0 property0, T1 property1, bool writeToEventLog)
=> Write(LogLevel.Error, exception, messageTemplate, property0, property1, writeToEventLog);
public void Error<T0, T1, T2>(Exception exception, string messageTemplate, T0 property0, T1 property1, T2 property2)
=> Write(LogLevel.Error, exception, messageTemplate, property0, property1, property2);
public void Error<T0, T1, T2>(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<T>(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<T>(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<T0, T1>(LogLevel level, Exception? exception, string messageTemplate, T0 property0, T1 property1)
private void Write<T0, T1>(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<T0, T1, T2>(LogLevel level, Exception? exception, string messageTemplate, T0 property0, T1 property1, T2 property2)
private void Write<T0, T1, T2>(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

View File

@ -22,83 +22,83 @@ internal interface IOtelLogger
{
bool IsEnabled(LogLevel level);
void Debug(string messageTemplate);
void Debug(string messageTemplate, bool writeToEventLog = true);
void Debug<T>(string messageTemplate, T property);
void Debug<T>(string messageTemplate, T property, bool writeToEventLog = true);
void Debug<T0, T1>(string messageTemplate, T0 property0, T1 property1);
void Debug<T0, T1>(string messageTemplate, T0 property0, T1 property1, bool writeToEventLog = true);
void Debug<T0, T1, T2>(string messageTemplate, T0 property0, T1 property1, T2 property2);
void Debug<T0, T1, T2>(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<T>(Exception exception, string messageTemplate, T property);
void Debug<T>(Exception exception, string messageTemplate, T property, bool writeToEventLog = true);
void Debug<T0, T1>(Exception exception, string messageTemplate, T0 property0, T1 property1);
void Debug<T0, T1>(Exception exception, string messageTemplate, T0 property0, T1 property1, bool writeToEventLog = true);
void Debug<T0, T1, T2>(Exception exception, string messageTemplate, T0 property0, T1 property1, T2 property2);
void Debug<T0, T1, T2>(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<T>(string messageTemplate, T property);
void Information<T>(string messageTemplate, T property, bool writeToEventLog = true);
void Information<T0, T1>(string messageTemplate, T0 property0, T1 property1);
void Information<T0, T1>(string messageTemplate, T0 property0, T1 property1, bool writeToEventLog = true);
void Information<T0, T1, T2>(string messageTemplate, T0 property0, T1 property1, T2 property2);
void Information<T0, T1, T2>(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<T>(Exception exception, string messageTemplate, T property);
void Information<T>(Exception exception, string messageTemplate, T property, bool writeToEventLog = true);
void Information<T0, T1>(Exception exception, string messageTemplate, T0 property0, T1 property1);
void Information<T0, T1>(Exception exception, string messageTemplate, T0 property0, T1 property1, bool writeToEventLog = true);
void Information<T0, T1, T2>(Exception exception, string messageTemplate, T0 property0, T1 property1, T2 property2);
void Information<T0, T1, T2>(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<T>(string messageTemplate, T property);
void Warning<T>(string messageTemplate, T property, bool writeToEventLog = true);
void Warning<T0, T1>(string messageTemplate, T0 property0, T1 property1);
void Warning<T0, T1>(string messageTemplate, T0 property0, T1 property1, bool writeToEventLog = true);
void Warning<T0, T1, T2>(string messageTemplate, T0 property0, T1 property1, T2 property2);
void Warning<T0, T1, T2>(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<T>(Exception exception, string messageTemplate, T property);
void Warning<T>(Exception exception, string messageTemplate, T property, bool writeToEventLog = true);
void Warning<T0, T1>(Exception exception, string messageTemplate, T0 property0, T1 property1);
void Warning<T0, T1>(Exception exception, string messageTemplate, T0 property0, T1 property1, bool writeToEventLog = true);
void Warning<T0, T1, T2>(Exception exception, string messageTemplate, T0 property0, T1 property1, T2 property2);
void Warning<T0, T1, T2>(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<T>(string messageTemplate, T property);
void Error<T>(string messageTemplate, T property, bool writeToEventLog = true);
void Error<T0, T1>(string messageTemplate, T0 property0, T1 property1);
void Error<T0, T1>(string messageTemplate, T0 property0, T1 property1, bool writeToEventLog = true);
void Error<T0, T1, T2>(string messageTemplate, T0 property0, T1 property1, T2 property2);
void Error<T0, T1, T2>(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<T>(Exception exception, string messageTemplate, T property);
void Error<T>(Exception exception, string messageTemplate, T property, bool writeToEventLog = true);
void Error<T0, T1>(Exception exception, string messageTemplate, T0 property0, T1 property1);
void Error<T0, T1>(Exception exception, string messageTemplate, T0 property0, T1 property1, bool writeToEventLog = true);
void Error<T0, T1, T2>(Exception exception, string messageTemplate, T0 property0, T1 property1, T2 property2);
void Error<T0, T1, T2>(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);
}