From 30578eb1700f730175519070929f8580ef2b92d0 Mon Sep 17 00:00:00 2001 From: Lubomir Tetak Date: Thu, 12 Dec 2024 08:40:40 +0100 Subject: [PATCH] Logging callback exposed --- LiteDB/Engine/EngineState.cs | 2 +- LiteDB/Engine/LiteEngine.cs | 2 +- LiteDB/Engine/Pages/CollectionPage.cs | 2 +- LiteDB/Engine/Pages/IndexPage.cs | 2 +- LiteDB/Utils/Constants.cs | 22 -------------- LiteDB/Utils/Logging.cs | 44 +++++++++++++++++++++++++++ 6 files changed, 48 insertions(+), 26 deletions(-) create mode 100644 LiteDB/Utils/Logging.cs diff --git a/LiteDB/Engine/EngineState.cs b/LiteDB/Engine/EngineState.cs index bd3dafac7..d31eaafa1 100644 --- a/LiteDB/Engine/EngineState.cs +++ b/LiteDB/Engine/EngineState.cs @@ -36,7 +36,7 @@ public void Validate() public bool Handle(Exception ex) { - LOG(ex.Message, "ERROR"); + LOG(ex, "ERROR"); if (ex is IOException || (ex is LiteException lex && lex.ErrorCode == LiteException.INVALID_DATAFILE_STATE)) diff --git a/LiteDB/Engine/LiteEngine.cs b/LiteDB/Engine/LiteEngine.cs index 59bb84b4c..00432043d 100644 --- a/LiteDB/Engine/LiteEngine.cs +++ b/LiteDB/Engine/LiteEngine.cs @@ -160,7 +160,7 @@ internal bool Open() } catch (Exception ex) { - LOG(ex.Message, "ERROR"); + LOG(ex, "ERROR"); this.Close(ex); throw; diff --git a/LiteDB/Engine/Pages/CollectionPage.cs b/LiteDB/Engine/Pages/CollectionPage.cs index 4db992238..34040e89a 100644 --- a/LiteDB/Engine/Pages/CollectionPage.cs +++ b/LiteDB/Engine/Pages/CollectionPage.cs @@ -39,7 +39,7 @@ public CollectionPage(PageBuffer buffer, uint pageID) public CollectionPage(PageBuffer buffer) : base(buffer) { - ENSURE(this.PageType == PageType.Collection, "page type must be collection page"); + ENSURE(this.PageType == PageType.Collection, "page type must be collection page, but it is {0}", PageType); if (this.PageType != PageType.Collection) throw LiteException.InvalidPageType(PageType.Collection, this); diff --git a/LiteDB/Engine/Pages/IndexPage.cs b/LiteDB/Engine/Pages/IndexPage.cs index b5cd7b0cb..be4439ad8 100644 --- a/LiteDB/Engine/Pages/IndexPage.cs +++ b/LiteDB/Engine/Pages/IndexPage.cs @@ -16,7 +16,7 @@ internal class IndexPage : BasePage public IndexPage(PageBuffer buffer) : base(buffer) { - ENSURE(this.PageType == PageType.Index, "page type must be index page"); + ENSURE(this.PageType == PageType.Index, "page type must be index page, but it is {0}", PageType); if (this.PageType != PageType.Index) throw LiteException.InvalidPageType(PageType.Index, this); } diff --git a/LiteDB/Utils/Constants.cs b/LiteDB/Utils/Constants.cs index 93458193d..c5c48b2df 100644 --- a/LiteDB/Utils/Constants.cs +++ b/LiteDB/Utils/Constants.cs @@ -108,28 +108,6 @@ internal class Constants public const int RANDOMIZER_SEED = 0; #endif - /// - /// Log a message using Debug.WriteLine - /// - [DebuggerHidden] - [Conditional("DEBUG")] - public static void LOG(string message, string category) - { - //Debug.WriteLine is too slow in multi-threads - //var threadID = Environment.CurrentManagedThreadId; - //Debug.WriteLine(message, threadID + "|" + category); - } - - /// - /// Log a message using Debug.WriteLine only if conditional = true - /// - [DebuggerHidden] - [Conditional("DEBUG")] - public static void LOG(bool conditional, string message, string category) - { - if (conditional) LOG(message, category); - } - /// /// Ensure condition is true, otherwise throw exception (check contract) /// diff --git a/LiteDB/Utils/Logging.cs b/LiteDB/Utils/Logging.cs new file mode 100644 index 000000000..2d88d059f --- /dev/null +++ b/LiteDB/Utils/Logging.cs @@ -0,0 +1,44 @@ +global using static LiteDB.Logging; // make LOG method available globally +using System; +using System.Diagnostics; + +namespace LiteDB; + +public static class Logging +{ + public static event Action LogCallback; + + /// + /// Log a message using LogCallback event + /// + [DebuggerHidden] + internal static void LOG(string message, string category) + { + LogCallback?.Invoke(new LogEventArgs() { Message = message, Category = category }); + } + + /// + /// Log a message using LogCallback event only if conditional = true + /// + [DebuggerHidden] + internal static void LOG(bool conditional, string message, string category) + { + if (conditional) LOG(message, category); + } + + /// + /// Log an exception using LogCallback event only if conditional = true + /// + [DebuggerHidden] + internal static void LOG(Exception exception, string category) + { + LogCallback?.Invoke(new LogEventArgs() { Exception = exception, Category = category }); + } +} + +public class LogEventArgs +{ + public string Category { get; set; } + public string Message { get; set; } + public Exception Exception { get; set; } +} \ No newline at end of file