Skip to content

Commit

Permalink
Try to pass log info to serilog without rendering the log statement (#…
Browse files Browse the repository at this point in the history
…266)

* Try to pass log info to serilog without rendering the log statement

* formatting

* remove redundant from new
  • Loading branch information
adamhathcock authored Sep 26, 2024
1 parent b086d16 commit 6115620
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 58 deletions.
3 changes: 2 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<PackageVersion Include="OpenTelemetry.Instrumentation.Http" version="1.9.0" />
<PackageVersion Include="Serilog" Version="4.0.1" />
<PackageVersion Include="Serilog.Exceptions" Version="8.4.0" />
<PackageVersion Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageVersion Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageVersion Include="Serilog.Sinks.OpenTelemetry" Version="4.0.0" />
Expand All @@ -43,4 +44,4 @@
<GlobalPackageReference Include="Speckle.InterfaceGenerator" Version="0.9.6" />
<GlobalPackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3"/>
</ItemGroup>
</Project>
</Project>
4 changes: 2 additions & 2 deletions Sdk/Speckle.Connectors.Common/SpeckleLogProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
12 changes: 6 additions & 6 deletions Sdk/Speckle.Connectors.Common/SpeckleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ public void Log<TState>(
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:
Expand Down
7 changes: 5 additions & 2 deletions Sdk/Speckle.Connectors.Logging/Internal/LogBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
10 changes: 10 additions & 0 deletions Sdk/Speckle.Connectors.Logging/LoggerProvider.cs
Original file line number Diff line number Diff line change
@@ -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();
}
2 changes: 1 addition & 1 deletion Sdk/Speckle.Connectors.Logging/Observability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<PackageReference Include="OpenTelemetry.Instrumentation.Http" PrivateAssets="all"/>
<PackageReference Include="Serilog" PrivateAssets="all"/>
<PackageReference Include="Serilog.Exceptions" PrivateAssets="all"/>
<PackageReference Include="Serilog.Extensions.Logging" PrivateAssets="all"/>
<PackageReference Include="Serilog.Sinks.Console" PrivateAssets="all"/>
<PackageReference Include="Serilog.Sinks.File" PrivateAssets="all"/>
<PackageReference Include="Serilog.Sinks.OpenTelemetry" PrivateAssets="all"/>
Expand Down
60 changes: 14 additions & 46 deletions Sdk/Speckle.Connectors.Logging/SpeckleLogger.cs
Original file line number Diff line number Diff line change
@@ -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<TState>(
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<TState, Exception?, string> formatter
) => logger.Log(GetLevel(speckleLogLevel), new EventId(eventId), state, exception, formatter);
}
10 changes: 10 additions & 0 deletions Sdk/Speckle.Connectors.Logging/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -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, )",
Expand Down

0 comments on commit 6115620

Please sign in to comment.