Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging callback exposed #2576

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LiteDB/Engine/EngineState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion LiteDB/Engine/LiteEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ internal bool Open()
}
catch (Exception ex)
{
LOG(ex.Message, "ERROR");
LOG(ex, "ERROR");

this.Close(ex);
throw;
Expand Down
2 changes: 1 addition & 1 deletion LiteDB/Engine/Pages/CollectionPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry this does not work, PageType argument is not replaced.


if (this.PageType != PageType.Collection) throw LiteException.InvalidPageType(PageType.Collection, this);

Expand Down
2 changes: 1 addition & 1 deletion LiteDB/Engine/Pages/IndexPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
22 changes: 0 additions & 22 deletions LiteDB/Utils/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,28 +108,6 @@ internal class Constants
public const int RANDOMIZER_SEED = 0;
#endif

/// <summary>
/// Log a message using Debug.WriteLine
/// </summary>
[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);
}

/// <summary>
/// Log a message using Debug.WriteLine only if conditional = true
/// </summary>
[DebuggerHidden]
[Conditional("DEBUG")]
public static void LOG(bool conditional, string message, string category)
{
if (conditional) LOG(message, category);
}

/// <summary>
/// Ensure condition is true, otherwise throw exception (check contract)
/// </summary>
Expand Down
44 changes: 44 additions & 0 deletions LiteDB/Utils/Logging.cs
Original file line number Diff line number Diff line change
@@ -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<LogEventArgs> LogCallback;

/// <summary>
/// Log a message using LogCallback event
/// </summary>
[DebuggerHidden]
internal static void LOG(string message, string category)
{
LogCallback?.Invoke(new LogEventArgs() { Message = message, Category = category });
}

/// <summary>
/// Log a message using LogCallback event only if conditional = true
/// </summary>
[DebuggerHidden]
internal static void LOG(bool conditional, string message, string category)
{
if (conditional) LOG(message, category);
}

/// <summary>
/// Log an exception using LogCallback event only if conditional = true
/// </summary>
[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; }
}