removed other projects

This commit is contained in:
Matthias Heil
2026-04-05 10:13:12 +02:00
parent 149de07b0e
commit 90bca23d53
79 changed files with 31 additions and 1718 deletions
@@ -0,0 +1,89 @@
using Serilog.Events;
using LogViewer.Core;
using Microsoft.Extensions.Logging;
using Serilog.Core;
using System.Globalization;
using Serilog.Sinks.LogView.Core.Logging;
namespace Serilog.Sinks.LogView.Core;
public class DataStoreLoggerSink(
Func<ILogDataStore> dataStoreProvider,
Func<DataStoreLoggerConfiguration>? getCurrentConfig = null,
IFormatProvider? formatProvider = null) : ILogEventSink
{
protected Func<ILogDataStore> DataStoreProvider { get; } = dataStoreProvider;
private IFormatProvider? FormatProvider { get; } = formatProvider;
private Func<DataStoreLoggerConfiguration>? GetCurrentConfig { get; } = getCurrentConfig;
public void Emit(LogEvent logEvent)
{
LogLevel logLevel = logEvent.Level switch
{
LogEventLevel.Verbose => LogLevel.Trace,
LogEventLevel.Debug => LogLevel.Debug,
LogEventLevel.Warning => LogLevel.Warning,
LogEventLevel.Error => LogLevel.Error,
LogEventLevel.Fatal => LogLevel.Critical,
_ => LogLevel.Information
};
DataStoreLoggerConfiguration config = GetCurrentConfig?.Invoke() ?? new DataStoreLoggerConfiguration();
EventId eventId = EventIdFactory(logEvent);
if (eventId.Id == 0 && config.EventId != 0)
eventId = config.EventId;
string message = logEvent.RenderMessage(FormatProvider);
string exception = logEvent.Exception?.Message ?? (logEvent.Level >= LogEventLevel.Error ? message : string.Empty);
LogEntryColor color = config.Colors[logLevel];
AddLogEntry(logLevel, eventId, message, exception, color);
}
protected virtual void AddLogEntry(LogLevel logLevel, EventId eventId, string message, string exception, LogEntryColor color)
{
ILogDataStore? dataStore = DataStoreProvider.Invoke();
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
if (dataStore == null)
return; // app is shutting down
dataStore.AddEntry(new()
{
Timestamp = DateTime.UtcNow,
LogLevel = logLevel,
EventId = eventId,
State = message,
Exception = exception,
Color = color
});
}
private static EventId EventIdFactory(LogEvent logEvent)
{
EventId eventId;
if (!logEvent.Properties.TryGetValue("EventId", out LogEventPropertyValue? src))
return new();
int? id = null;
string? eventName = null;
// ref: https://stackoverflow.com/a/56722516
StructureValue? value = src as StructureValue;
LogEventProperty? idProperty = value!.Properties.FirstOrDefault(x => x.Name.Equals("Id", StringComparison.Ordinal));
if (idProperty is not null)
id = int.Parse(idProperty.Value.ToString(),CultureInfo.InvariantCulture);
LogEventProperty? nameProperty = value.Properties.FirstOrDefault(x => x.Name.Equals("Name", StringComparison.Ordinal));
if (nameProperty is not null)
eventName = nameProperty.Value.ToString().Trim('"');
eventId = new EventId(id ?? 0, eventName ?? string.Empty);
return eventId;
}
}
@@ -0,0 +1,25 @@
using Serilog.Configuration;
using LogViewer.Core;
using Serilog.Sinks.LogView.Core.Logging;
namespace Serilog.Sinks.LogView.Core.Extensions;
public static class DataStoreLoggerSinkExtensions
{
public static LoggerConfiguration DataStoreLoggerSink
(
this LoggerSinkConfiguration loggerConfiguration,
Func<ILogDataStore> dataStoreProvider,
Action<DataStoreLoggerConfiguration>? configuration = null,
IFormatProvider formatProvider = null!
)
=> loggerConfiguration.Sink(new DataStoreLoggerSink(dataStoreProvider, GetConfig(configuration), formatProvider));
private static Func<DataStoreLoggerConfiguration> GetConfig(Action<DataStoreLoggerConfiguration>? configuration)
{
// convert from Action to Func delegate to pass data
DataStoreLoggerConfiguration data = new();
configuration?.Invoke(data);
return () => data;
}
}
@@ -0,0 +1,54 @@
namespace Microsoft.Extensions.Logging;
public static class LoggerExtensions
{
public static void Emit(this ILogger logger, EventId eventId,
LogLevel logLevel, string message, Exception? exception = null, params object?[] args)
{
if (logger is null)
return;
//if (!logger.IsEnabled(logLevel))
// return;
switch (logLevel)
{
case LogLevel.Trace:
logger.LogTrace(eventId, message, args);
break;
case LogLevel.Debug:
logger.LogDebug(eventId, message, args);
break;
case LogLevel.Information:
logger.LogInformation(eventId, message, args);
break;
case LogLevel.Warning:
logger.LogWarning(eventId, exception, message, args);
break;
case LogLevel.Error:
logger.LogError(eventId, exception, message, args);
break;
case LogLevel.Critical:
logger.LogCritical(eventId, exception, message, args);
break;
}
}
public static void TestPattern(this ILogger logger, EventId eventId)
{
Exception exception = new InvalidDataException("Test Error Message");
#pragma warning disable CA1848 // Use the LoggerMessage delegates
logger.Emit(eventId, LogLevel.Trace, "Trace Test Pattern");
logger.Emit(eventId, LogLevel.Debug, "Debug Test Pattern");
logger.Emit(eventId, LogLevel.Information, "Information Test Pattern");
logger.Emit(eventId, LogLevel.Warning, "Warning Test Pattern");
logger.Emit(eventId, LogLevel.Error, "Error Test Pattern", exception);
logger.Emit(eventId, LogLevel.Critical, "Critical Test Pattern", exception);
}
}
@@ -0,0 +1,25 @@
using System.Drawing;
using Microsoft.Extensions.Logging;
using Serilog.Sinks.LogView.Core.Logging;
namespace LogViewer.Core;
public class DataStoreLoggerConfiguration
{
#region Properties
public EventId EventId { get; set; }
public Dictionary<LogLevel, LogEntryColor> Colors { get; } = new()
{
[LogLevel.Trace] = new() { Foreground = Color.DarkGray },
[LogLevel.Debug] = new() { Foreground = Color.Gray },
[LogLevel.Information] = new(),
[LogLevel.Warning] = new() { Foreground = Color.Orange},
[LogLevel.Error] = new() { Foreground = Color.White, Background = Color.OrangeRed },
[LogLevel.Critical] = new() { Foreground=Color.White, Background = Color.Red },
[LogLevel.None] = new(),
};
#endregion
}
@@ -0,0 +1,9 @@
using System.Collections.ObjectModel;
namespace Serilog.Sinks.LogView.Core.Logging;
public interface ILogDataStore
{
ObservableCollection<LogModel> Entries { get; }
void AddEntry(LogModel logModel);
}
@@ -0,0 +1,6 @@
namespace Serilog.Sinks.LogView.Core.Logging;
public interface ILogDataStoreCore
{
public ILogDataStore DataStore { get; }
}
@@ -0,0 +1,32 @@
using System.Collections.ObjectModel;
namespace Serilog.Sinks.LogView.Core.Logging;
public class LogDataStore : ILogDataStore
{
#region Fields
private static readonly SemaphoreSlim _semaphore = new(initialCount: 1);
#endregion
#region Properties
public ObservableCollection<LogModel> Entries { get; } = new();
#endregion
#region Methods
public virtual void AddEntry(LogModel logModel)
{
// ensure only one operation at time from multiple threads
_semaphore.Wait();
Entries.Add(logModel);
_semaphore.Release();
}
#endregion
}
@@ -0,0 +1,10 @@
using System.Drawing;
namespace Serilog.Sinks.LogView.Core.Logging;
public class LogEntryColor
{
public Color Foreground { get; set; } = Color.Black;
public Color Background { get; set; } = Color.Transparent;
}
@@ -0,0 +1,22 @@
using Microsoft.Extensions.Logging;
namespace Serilog.Sinks.LogView.Core.Logging;
public class LogModel
{
#region Properties
public DateTime Timestamp { get; set; }
public LogLevel LogLevel { get; set; }
public EventId EventId { get; set; }
public object? State { get; set; }
public string? Exception { get; set; }
public LogEntryColor? Color { get; set; }
#endregion
}
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Serilog" Version="4.3.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.5" />
</ItemGroup>
</Project>