From 2d8ffb9d4de42f3185f18d1d71b246d2c072ca0b Mon Sep 17 00:00:00 2001 From: Piotr Karczmarz Date: Tue, 28 Jan 2025 12:53:16 +0100 Subject: [PATCH] Extended logging for SentryLog: - support for message and caller method name parameters --- src/Cody.Core/Logging/ISentryLog.cs | 5 +++-- src/Cody.Core/Logging/Logger.cs | 4 ++-- src/Cody.Core/Logging/SentryLog.cs | 16 ++++++++++++---- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/Cody.Core/Logging/ISentryLog.cs b/src/Cody.Core/Logging/ISentryLog.cs index c58773a..26d4be2 100644 --- a/src/Cody.Core/Logging/ISentryLog.cs +++ b/src/Cody.Core/Logging/ISentryLog.cs @@ -1,11 +1,12 @@ using System; +using System.Runtime.CompilerServices; namespace Cody.Core.Logging { public interface ISentryLog { - void Error(Exception exception); + void Error(string message, Exception ex, [CallerMemberName] string callerName = ""); - void Error(string message); + void Error(string message, [CallerMemberName] string callerName = ""); } } diff --git a/src/Cody.Core/Logging/Logger.cs b/src/Cody.Core/Logging/Logger.cs index 9b12b23..b798fa3 100644 --- a/src/Cody.Core/Logging/Logger.cs +++ b/src/Cody.Core/Logging/Logger.cs @@ -65,7 +65,7 @@ public void Error(string message, [CallerMemberName] string callerName = "") // TODO: _fileLogger.Error(customMessage); DebugWrite(customMessage); _outputWindowPane?.Error(message, callerName); - _sentryLog?.Error(customMessage); + _sentryLog?.Error(customMessage, callerName); _testLogger?.WriteLog(message, "ERROR", callerName); } @@ -87,7 +87,7 @@ public void Error(string message, Exception ex, [CallerMemberName] string caller // TODO: _fileLogger.Error(originalException, customMessage); DebugWrite(customMessage); _outputWindowPane?.Error(outputMessage, callerName); - _sentryLog?.Error(originalException); + _sentryLog?.Error(outputMessage, originalException, callerName); _testLogger?.WriteLog(message, "ERROR", callerName); } diff --git a/src/Cody.Core/Logging/SentryLog.cs b/src/Cody.Core/Logging/SentryLog.cs index b413120..833ecbd 100644 --- a/src/Cody.Core/Logging/SentryLog.cs +++ b/src/Cody.Core/Logging/SentryLog.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.Linq; using System.Reflection; +using System.Runtime.CompilerServices; namespace Cody.Core.Logging { @@ -11,14 +12,21 @@ public class SentryLog : ISentryLog { public const string CodyAssemblyPrefix = "Cody."; - public void Error(Exception exception) + public void Error(string message, Exception ex, [CallerMemberName] string callerName = "") { - SentrySdk.CaptureException(exception); + SentrySdk.CaptureException(ex, scope => + { + scope.SetExtra(nameof(callerName), callerName); + scope.SetExtra(nameof(message), message); + }); } - public void Error(string message) + public void Error(string message, [CallerMemberName] string callerName = "") { - SentrySdk.CaptureMessage(message); + SentrySdk.CaptureMessage(message, scope => + { + scope.SetExtra(nameof(callerName), callerName); + }); } public static void Initialize()