Skip to content

Commit

Permalink
Rework logging to be more MS logger
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhathcock committed Jul 16, 2024
1 parent 22497c8 commit 3ad8cdd
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 26 deletions.
10 changes: 7 additions & 3 deletions DUI3/Speckle.Connectors.DUI/Bridge/BrowserBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,15 @@ private struct RunMethodArgs
/// </summary>
/// <param name="jsonSerializerSettings">The settings to use for JSON serialization and deserialization.</param>
/// <param name="loggerFactory">The factory to create a logger for <see cref="BrowserBridge"/>.</param>
public BrowserBridge(JsonSerializerSettings jsonSerializerSettings, ILoggerFactory loggerFactory)
public BrowserBridge(
JsonSerializerSettings jsonSerializerSettings,
ILogger<BrowserBridge> logger,
ILogger<TopLevelExceptionHandler> topLogger
)
{
_serializerOptions = jsonSerializerSettings;
_logger = loggerFactory.CreateLogger<BrowserBridge>();
_topLevelExceptionHandler = new TopLevelExceptionHandler(loggerFactory, this); //TODO: Probably we could inject this with a Lazy somewhere
_logger = logger;
_topLevelExceptionHandler = new TopLevelExceptionHandler(topLogger, this); //TODO: Probably we could inject this with a Lazy somewhere
// Capture the main thread's SynchronizationContext
_mainThreadContext = SynchronizationContext.Current;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ public sealed class TopLevelExceptionHandler : ITopLevelExceptionHandler

private const string UNHANDLED_LOGGER_TEMPLATE = "An unhandled Exception occured";

public TopLevelExceptionHandler(ILoggerFactory loggerFactory, IBridge bridge)
public TopLevelExceptionHandler(ILogger<TopLevelExceptionHandler> logger, IBridge bridge)
{
_logger = loggerFactory.CreateLogger<TopLevelExceptionHandler>();
_logger = logger;
_bridge = bridge;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,8 @@ public SpeckleContainer Build()
{
var container = ContainerBuilder.Build();

// POC: we could create the factory on construction of the container and then inject that and store it
var logger = container.Resolve<ILoggerFactory>().CreateLogger<SpeckleContainerBuilder>();

//POC: we could create the factory on construction of the container and then inject that and store it?
var logger = container.Resolve<ILogger<SpeckleContainerBuilder>>();
// POC: we could probably expand on this
List<string> assemblies = AppDomain.CurrentDomain.GetAssemblies().Select(x => x.FullName).ToList();
logger.LogInformation("Loaded assemblies: {@Assemblies}", assemblies);
Expand Down
7 changes: 5 additions & 2 deletions Sdk/Speckle.Connectors.Utils/Operations/RootObjectSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@ public sealed class RootObjectSender : IRootObjectSender
private readonly ServerTransport.Factory _transportFactory;
private readonly ISendConversionCache _sendConversionCache;
private readonly AccountService _accountService;
private readonly ISendHelper _sendHelper;

public RootObjectSender(
ServerTransport.Factory transportFactory,
ISendConversionCache sendConversionCache,
AccountService accountService
AccountService accountService,
ISendHelper sendHelper
)
{
_transportFactory = transportFactory;
_sendConversionCache = sendConversionCache;
_accountService = accountService;
_sendHelper = sendHelper;
}

public async Task<(string rootObjId, Dictionary<string, ObjectReference> convertedReferences)> Send(
Expand All @@ -45,7 +48,7 @@ AccountService accountService
Account account = _accountService.GetAccountWithServerUrlFallback(sendInfo.AccountId, sendInfo.ServerUrl);

ITransport transport = _transportFactory(account, sendInfo.ProjectId, 60, null);
var sendResult = await SendHelper.Send(commitObject, transport, true, null, ct).ConfigureAwait(false);
var sendResult = await _sendHelper.Send(commitObject, transport, true, null, ct).ConfigureAwait(false);

_sendConversionCache.StoreSendResult(sendInfo.ProjectId, sendResult.convertedReferences);

Expand Down
42 changes: 26 additions & 16 deletions Sdk/Speckle.Connectors.Utils/Operations/Send.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
using System.Collections.Concurrent;
using System.Collections.Concurrent;
using System.Diagnostics;
using Microsoft.Extensions.Logging;
using Serilog.Context;
using Speckle.Core.Logging;
using Speckle.Core.Models;
using Speckle.Core.Serialisation;
using Speckle.Core.Transports;
using Speckle.InterfaceGenerator;
using Speckle.Newtonsoft.Json.Linq;

namespace Speckle.Connectors.Utils.Operations;

/// <summary>
/// NOTE: Contains copy pasted code from the OG Send operations in Core (the non-obsolete ones).
/// </summary>
public static class SendHelper
[GenerateAutoInterface]
public class SendHelper(ILogger<SendHelper> logger) : ISendHelper
{
/// <summary>
/// IMPORTANT: Copy pasted function from Operations.Send in Core, but this time returning the converted references from the serializer.
Expand All @@ -30,7 +33,7 @@ public static class SendHelper
/// using ServerTransport destination = new(account, streamId);
/// string objectId = await Send(mySpeckleObject, destination, true);
/// </code></example>
public static async Task<(string rootObjId, Dictionary<string, ObjectReference> convertedReferences)> Send(
public async Task<(string rootObjId, Dictionary<string, ObjectReference> convertedReferences)> Send(
Base value,
ITransport transport,
bool useDefaultCache,
Expand Down Expand Up @@ -66,7 +69,7 @@ public static class SendHelper
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="SpeckleException"></exception>
private static async Task<(string rootObjId, Dictionary<string, ObjectReference> convertedReferences)> Send(
private async Task<(string rootObjId, Dictionary<string, ObjectReference> convertedReferences)> Send(
Base value,
IReadOnlyCollection<ITransport> transports,
Action<ConcurrentDictionary<string, int>>? onProgressAction = null,
Expand All @@ -90,7 +93,7 @@ public static class SendHelper
using (LogContext.PushProperty("correlationId", Guid.NewGuid().ToString()))
{
var sendTimer = Stopwatch.StartNew();
SpeckleLog.Logger.Information("Starting send operation");
logger.LogInformation("Starting send operation");

var internalProgressAction = GetInternalProgressAction(onProgressAction);

Expand All @@ -111,11 +114,7 @@ public static class SendHelper
}
catch (Exception ex) when (!ex.IsFatal())
{
SpeckleLog.Logger.Information(
ex,
"Send operation failed after {elapsed} seconds",
sendTimer.Elapsed.TotalSeconds
);
logger.LogInformation(ex, "Send operation failed after {elapsed} seconds", sendTimer.Elapsed.TotalSeconds);
if (ex is OperationCanceledException or SpeckleException)
{
throw;
Expand All @@ -132,17 +131,28 @@ public static class SendHelper
}

sendTimer.Stop();
SpeckleLog
.Logger.ForContext("transportElapsedBreakdown", transports.ToDictionary(t => t.TransportName, t => t.Elapsed))
.ForContext("note", "the elapsed summary doesn't need to add up to the total elapsed... Threading magic...")
.ForContext("serializerElapsed", serializerV2.Elapsed)
.Information(
using (
LogContext.PushProperty(
"transportElapsedBreakdown",
transports.ToDictionary(t => t.TransportName, t => t.Elapsed)
)
)
using (
LogContext.PushProperty(
"note",
"the elapsed summary doesn't need to add up to the total elapsed... Threading magic..."
)
)
using (LogContext.PushProperty("serializerElapsed", serializerV2.Elapsed.ToString()))
{
logger.LogInformation(
"Finished sending {objectCount} objects after {elapsed}, result {objectId}",
transports.Max(t => t.SavedObjectCount),
sendTimer.Elapsed.TotalSeconds,
serializerReturnValue.rootObjId
);
return serializerReturnValue;
return serializerReturnValue;
}
}
}

Expand Down

0 comments on commit 3ad8cdd

Please sign in to comment.