diff --git a/Directory.Packages.props b/Directory.Packages.props index c05959b34..59f21f62a 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -28,6 +28,7 @@ + @@ -43,4 +44,4 @@ - + \ No newline at end of file diff --git a/Sdk/Speckle.Connectors.Common/SpeckleLogProvider.cs b/Sdk/Speckle.Connectors.Common/SpeckleLogProvider.cs index 1c0848b62..c78a1290a 100644 --- a/Sdk/Speckle.Connectors.Common/SpeckleLogProvider.cs +++ b/Sdk/Speckle.Connectors.Common/SpeckleLogProvider.cs @@ -3,9 +3,9 @@ namespace Speckle.Connectors.Common; -public sealed class SpeckleLogProvider(Logger speckleLogger) : ILoggerProvider +public sealed class SpeckleLogProvider(LoggerProvider speckleLogger) : ILoggerProvider { public void Dispose() { } - public ILogger CreateLogger(string categoryName) => new SpeckleLogger(speckleLogger); + public ILogger CreateLogger(string categoryName) => new SpeckleLogger(speckleLogger.CreateLogger(categoryName)); } diff --git a/Sdk/Speckle.Connectors.Common/SpeckleLogger.cs b/Sdk/Speckle.Connectors.Common/SpeckleLogger.cs index 13c714c3c..140cf4b27 100644 --- a/Sdk/Speckle.Connectors.Common/SpeckleLogger.cs +++ b/Sdk/Speckle.Connectors.Common/SpeckleLogger.cs @@ -16,22 +16,22 @@ public void Log( switch (logLevel) { case LogLevel.Critical: - logger.Write(SpeckleLogLevel.Fatal, exception, formatter(state, exception)); + logger.Write(SpeckleLogLevel.Fatal, eventId.Id, state, exception, formatter); break; case LogLevel.Trace: - logger.Write(SpeckleLogLevel.Verbose, exception, formatter(state, exception)); + logger.Write(SpeckleLogLevel.Verbose, eventId.Id, state, exception, formatter); break; case LogLevel.Debug: - logger.Write(SpeckleLogLevel.Debug, exception, formatter(state, exception)); + logger.Write(SpeckleLogLevel.Debug, eventId.Id, state, exception, formatter); break; case LogLevel.Information: - logger.Write(SpeckleLogLevel.Information, exception, formatter(state, exception)); + logger.Write(SpeckleLogLevel.Information, eventId.Id, state, exception, formatter); break; case LogLevel.Warning: - logger.Write(SpeckleLogLevel.Warning, exception, formatter(state, exception)); + logger.Write(SpeckleLogLevel.Warning, eventId.Id, state, exception, formatter); break; case LogLevel.Error: - logger.Write(SpeckleLogLevel.Error, exception, formatter(state, exception)); + logger.Write(SpeckleLogLevel.Error, eventId.Id, state, exception, formatter); break; case LogLevel.None: default: diff --git a/Sdk/Speckle.Connectors.Logging/Internal/LogBuilder.cs b/Sdk/Speckle.Connectors.Logging/Internal/LogBuilder.cs index b53ab41b5..6c310289d 100644 --- a/Sdk/Speckle.Connectors.Logging/Internal/LogBuilder.cs +++ b/Sdk/Speckle.Connectors.Logging/Internal/LogBuilder.cs @@ -4,13 +4,14 @@ using OpenTelemetry.Resources; using Serilog; using Serilog.Exceptions; +using Serilog.Extensions.Logging; using Serilog.Sinks.OpenTelemetry; namespace Speckle.Connectors.Logging.Internal; internal static class LogBuilder { - public static Logger Initialize( + public static LoggerProvider Initialize( string applicationAndVersion, string connectorVersion, SpeckleLogging? speckleLogging, @@ -60,7 +61,9 @@ ResourceBuilder resourceBuilder "Initialized logger inside {hostApplication}/{productVersion}/{version} for user {id}. Path info {userApplicationDataPath} {installApplicationDataPath}." ); - return new Logger(logger); +#pragma warning disable CA2000 + return new LoggerProvider(new SerilogLoggerProvider(logger)); +#pragma warning restore CA2000 } private static FileVersionInfo GetFileVersionInfo() diff --git a/Sdk/Speckle.Connectors.Logging/LoggerProvider.cs b/Sdk/Speckle.Connectors.Logging/LoggerProvider.cs new file mode 100644 index 000000000..5f16f8821 --- /dev/null +++ b/Sdk/Speckle.Connectors.Logging/LoggerProvider.cs @@ -0,0 +1,10 @@ +using Serilog.Extensions.Logging; + +namespace Speckle.Connectors.Logging; + +public sealed class LoggerProvider(SerilogLoggerProvider provider) : IDisposable +{ + public Logger CreateLogger(string categoryName) => new(provider.CreateLogger(categoryName)); + + public void Dispose() => provider.Dispose(); +} diff --git a/Sdk/Speckle.Connectors.Logging/Observability.cs b/Sdk/Speckle.Connectors.Logging/Observability.cs index 2a1db2c92..420e6798c 100644 --- a/Sdk/Speckle.Connectors.Logging/Observability.cs +++ b/Sdk/Speckle.Connectors.Logging/Observability.cs @@ -4,7 +4,7 @@ namespace Speckle.Connectors.Logging; public static class Observability { - public static (Logger, IDisposable?) Initialize( + public static (LoggerProvider, IDisposable?) Initialize( string applicationAndVersion, string slug, string connectorVersion, diff --git a/Sdk/Speckle.Connectors.Logging/Speckle.Connectors.Logging.csproj b/Sdk/Speckle.Connectors.Logging/Speckle.Connectors.Logging.csproj index 379d32e88..2b5a3b2bb 100644 --- a/Sdk/Speckle.Connectors.Logging/Speckle.Connectors.Logging.csproj +++ b/Sdk/Speckle.Connectors.Logging/Speckle.Connectors.Logging.csproj @@ -18,6 +18,7 @@ + diff --git a/Sdk/Speckle.Connectors.Logging/SpeckleLogger.cs b/Sdk/Speckle.Connectors.Logging/SpeckleLogger.cs index 5360221e8..6015138a0 100644 --- a/Sdk/Speckle.Connectors.Logging/SpeckleLogger.cs +++ b/Sdk/Speckle.Connectors.Logging/SpeckleLogger.cs @@ -1,58 +1,26 @@ -using Serilog.Events; +using Microsoft.Extensions.Logging; namespace Speckle.Connectors.Logging; -public sealed class Logger +public sealed class Logger(ILogger logger) { - private readonly Serilog.ILogger _logger; - - public Logger(Serilog.ILogger logger) - { - _logger = logger; - } - - private static LogEventLevel GetLevel(SpeckleLogLevel speckleLogLevel) => + private static LogLevel GetLevel(SpeckleLogLevel speckleLogLevel) => speckleLogLevel switch { - SpeckleLogLevel.Debug => LogEventLevel.Debug, - SpeckleLogLevel.Verbose => LogEventLevel.Verbose, - SpeckleLogLevel.Information => LogEventLevel.Information, - SpeckleLogLevel.Warning => LogEventLevel.Warning, - SpeckleLogLevel.Error => LogEventLevel.Error, - SpeckleLogLevel.Fatal => LogEventLevel.Fatal, + SpeckleLogLevel.Debug => LogLevel.Debug, + SpeckleLogLevel.Verbose => LogLevel.Trace, + SpeckleLogLevel.Information => LogLevel.Information, + SpeckleLogLevel.Warning => LogLevel.Warning, + SpeckleLogLevel.Error => LogLevel.Error, + SpeckleLogLevel.Fatal => LogLevel.Critical, _ => throw new ArgumentOutOfRangeException(nameof(speckleLogLevel), speckleLogLevel, null) }; - public void Write(SpeckleLogLevel speckleLogLevel, string message, params object?[] arguments) => - _logger.Write(GetLevel(speckleLogLevel), message, arguments); - - public void Write( + public void Write( SpeckleLogLevel speckleLogLevel, + int eventId, + TState state, Exception? exception, - string message, - params object?[] arguments - ) => _logger.Write(GetLevel(speckleLogLevel), exception, message, arguments); - - public void Debug(string message, params object?[] arguments) => _logger.Debug(message, arguments); - - public void Debug(Exception? exception, string message, params object?[] arguments) => - _logger.Debug(exception, message, arguments); - - public void Warning(string message, params object?[] arguments) => _logger.Warning(message, arguments); - - public void Warning(Exception? exception, string message, params object?[] arguments) => - _logger.Warning(exception, message, arguments); - - public void Information(string message, params object?[] arguments) => _logger.Information(message, arguments); - - public void Information(Exception? exception, string message, params object?[] arguments) => - _logger.Information(exception, message, arguments); - - public void LogError(string message, params object?[] arguments) => _logger.Error(message, arguments); - - public void LogError(Exception? exception, string message, params object?[] arguments) => - _logger.Error(exception, message, arguments); - - public void Fatal(Exception? exception, string message, params object?[] arguments) => - _logger.Fatal(exception, message, arguments); + Func formatter + ) => logger.Log(GetLevel(speckleLogLevel), new EventId(eventId), state, exception, formatter); } diff --git a/Sdk/Speckle.Connectors.Logging/packages.lock.json b/Sdk/Speckle.Connectors.Logging/packages.lock.json index 3d58a2917..e54d19b96 100644 --- a/Sdk/Speckle.Connectors.Logging/packages.lock.json +++ b/Sdk/Speckle.Connectors.Logging/packages.lock.json @@ -99,6 +99,16 @@ "System.Reflection.TypeExtensions": "4.7.0" } }, + "Serilog.Extensions.Logging": { + "type": "Direct", + "requested": "[8.0.0, )", + "resolved": "8.0.0", + "contentHash": "YEAMWu1UnWgf1c1KP85l1SgXGfiVo0Rz6x08pCiPOIBt2Qe18tcZLvdBUuV5o1QHvrs8FAry9wTIhgBRtjIlEg==", + "dependencies": { + "Microsoft.Extensions.Logging": "8.0.0", + "Serilog": "3.1.1" + } + }, "Serilog.Sinks.Console": { "type": "Direct", "requested": "[6.0.0, )",