-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
649 additions
and
378 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Lantern.Beacon.Console; | ||
|
||
public class CustomConsoleLogger( | ||
string name, | ||
Func<CustomConsoleLogger.CustomConsoleLoggerConfiguration, bool> filter, | ||
CustomConsoleLogger.CustomConsoleLoggerConfiguration config) | ||
: ILogger | ||
{ | ||
private readonly string _name = name ?? throw new ArgumentNullException(nameof(name)); | ||
private readonly Func<CustomConsoleLoggerConfiguration, bool> _filter = filter ?? throw new ArgumentNullException(nameof(filter)); | ||
private readonly CustomConsoleLoggerConfiguration _config = config ?? throw new ArgumentNullException(nameof(config)); | ||
|
||
public IDisposable BeginScope<TState>(TState state) => null; | ||
|
||
public bool IsEnabled(LogLevel logLevel) => _filter(_config); | ||
|
||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) | ||
{ | ||
if (!IsEnabled(logLevel)) return; | ||
|
||
if (_config.EventId == 0 || _config.EventId == eventId.Id) | ||
{ | ||
var timestamp = DateTime.UtcNow.ToString(_config.TimestampPrefix); | ||
var logLevelString = GetLogLevelString(logLevel); | ||
var logMessage = RemoveCategoryAndId(formatter(state, exception)); | ||
System.Console.WriteLine($"{timestamp} [{logLevelString}] {logMessage}"); | ||
} | ||
} | ||
|
||
private static string RemoveCategoryAndId(string message) | ||
{ | ||
// Logic to remove the category and eventId from the message | ||
// Assuming the category will be of the pattern 'Category[EventId]: message' | ||
var index = message.IndexOf(']'); | ||
|
||
if (index != -1 && message.Length > index + 1) | ||
{ | ||
message = message[(index + 2)..]; | ||
} | ||
|
||
return message; | ||
} | ||
|
||
private string GetLogLevelString(LogLevel logLevel) | ||
{ | ||
// Convert log level to custom string representation | ||
return logLevel switch | ||
{ | ||
LogLevel.Trace => "Trace", | ||
LogLevel.Debug => "Debug", | ||
LogLevel.Information => "Info", | ||
LogLevel.Warning => "Warning", | ||
LogLevel.Error => "Error", | ||
LogLevel.Critical => "Critical", | ||
LogLevel.None => "None", | ||
_ => "Unknown" | ||
}; | ||
} | ||
|
||
public class CustomConsoleLoggerConfiguration | ||
{ | ||
public int EventId { get; set; } | ||
public string TimestampPrefix { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Lantern.Beacon.Console; | ||
|
||
public class CustomConsoleLoggerProvider : ILoggerProvider | ||
{ | ||
private readonly CustomConsoleLogger.CustomConsoleLoggerConfiguration _config; | ||
private readonly Func<CustomConsoleLogger.CustomConsoleLoggerConfiguration, bool> _filter; | ||
|
||
public CustomConsoleLoggerProvider(Func<CustomConsoleLogger.CustomConsoleLoggerConfiguration, bool> filter, CustomConsoleLogger.CustomConsoleLoggerConfiguration config) | ||
{ | ||
_filter = filter; | ||
_config = config; | ||
} | ||
|
||
public ILogger CreateLogger(string categoryName) | ||
{ | ||
return new CustomConsoleLogger(categoryName, _filter, _config); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.