From 511a5f9f51956f33d3a58d1c758e6f0b6df579be Mon Sep 17 00:00:00 2001 From: Matthias Heil Date: Sun, 5 Apr 2026 08:00:09 +0200 Subject: [PATCH] Fixed Warnings --- .../AvaloniaLoggingDI/App.axaml.cs | 26 ++++++------- .../Applications/AvaloniaLoggingDI/Program.cs | 2 +- .../AvaloniaLoggingDI/Views/MainWindow.axaml | 2 +- .../Helpers/LoggingHelper.cs | 5 ++- .../AvaloniaLoggingNoDI/Program.cs | 2 +- .../Views/MainWindow.axaml | 4 +- .../Views/MainWindow.axaml.cs | 4 +- .../AvaloniaSerilogDI/App.axaml.cs | 26 ++++++------- .../Applications/AvaloniaSerilogDI/Program.cs | 2 +- .../AvaloniaSerilogDI/Views/MainWindow.axaml | 4 +- .../AvaloniaSerilogNoDI/MainWindow.axaml | 4 +- .../AvaloniaSerilogNoDI/MainWindow.axaml.cs | 19 ++++------ .../AvaloniaSerilogNoDI/Program.cs | 2 +- .../RandomLoggingService.cs | 2 +- .../LogViewerControl.axaml.cs | 4 +- .../Core/Common.Core/Helpers/AppSettings.cs | 38 ++++++++----------- .../Extensions/LoggerExtensions.cs | 14 ++++++- .../Logging/ILogDataStoreImpl.cs | 6 --- .../ViewModels/LogViewerControlViewModel.cs | 2 +- .../MsLogger.Core/DataStoreLoggerProvider.cs | 17 +++++---- .../DataStoreLoggerSink.cs | 34 +++++++---------- 21 files changed, 104 insertions(+), 115 deletions(-) delete mode 100644 CSharp/Core/LogViewer.Core/Logging/ILogDataStoreImpl.cs diff --git a/CSharp/Applications/AvaloniaLoggingDI/App.axaml.cs b/CSharp/Applications/AvaloniaLoggingDI/App.axaml.cs index 0c66151..3009d81 100644 --- a/CSharp/Applications/AvaloniaLoggingDI/App.axaml.cs +++ b/CSharp/Applications/AvaloniaLoggingDI/App.axaml.cs @@ -37,7 +37,7 @@ public partial class App : Application // catch all unhandled errors AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; - HostApplicationBuilder builder = Host.CreateApplicationBuilder(); + HostApplicationBuilder builder = Microsoft.Extensions.Hosting.Host.CreateApplicationBuilder(); builder /* @@ -87,24 +87,24 @@ public partial class App : Application services .AddSingleton() - .AddSingleton(service => new MainWindow + .AddSingleton(service => new MainWindow { DataContext = service.GetRequiredService() }); - _host = builder.Build(); - _cancellationTokenSource = new(); + Host = builder.Build(); + CancellationTokenSource = new(); try { LogStartingMode(); // set and show - desktop.MainWindow = _host.Services.GetRequiredService(); + desktop.MainWindow = Host.Services.GetRequiredService(); desktop.ShutdownRequested += OnShutdownRequested; // startup background services - _ = _host.StartAsync(_cancellationTokenSource.Token); + _ = Host.StartAsync(CancellationTokenSource.Token); } catch (OperationCanceledException) { @@ -131,15 +131,13 @@ public partial class App : Application } } private void OnShutdownRequested(object? sender, ShutdownRequestedEventArgs e) - => _ = _host!.StopAsync(_cancellationTokenSource!.Token); + => _ = Host?.StopAsync(CancellationTokenSource!.Token); - #region Fields - - private IHost? _host; - private CancellationTokenSource? _cancellationTokenSource; - - #endregion + + private IHost? Host { get; set; } + private CancellationTokenSource? CancellationTokenSource { get; set; } + private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e) => ShowMessageBox("Unhandled Error", ((Exception)e.ExceptionObject).Message); @@ -161,7 +159,7 @@ public partial class App : Application StringComparison.OrdinalIgnoreCase); // initialize a logger & EventId - ILogger logger = _host!.Services.GetRequiredService>(); + ILogger logger = Host!.Services.GetRequiredService>(); EventId eventId = new EventId(id: 0, name: Assembly.GetEntryAssembly()!.GetName().Name); // log a test pattern for each log level diff --git a/CSharp/Applications/AvaloniaLoggingDI/Program.cs b/CSharp/Applications/AvaloniaLoggingDI/Program.cs index a45333a..e266825 100644 --- a/CSharp/Applications/AvaloniaLoggingDI/Program.cs +++ b/CSharp/Applications/AvaloniaLoggingDI/Program.cs @@ -3,7 +3,7 @@ using System; namespace AvaloniaLoggingDI; -internal class Program +internal sealed class Program { // Initialization code. Don't use any Avalonia, third-party APIs or any // SynchronizationContext-reliant code before AppMain is called: things aren't initialized diff --git a/CSharp/Applications/AvaloniaLoggingDI/Views/MainWindow.axaml b/CSharp/Applications/AvaloniaLoggingDI/Views/MainWindow.axaml index 4e73e0b..7ec7dbf 100644 --- a/CSharp/Applications/AvaloniaLoggingDI/Views/MainWindow.axaml +++ b/CSharp/Applications/AvaloniaLoggingDI/Views/MainWindow.axaml @@ -10,7 +10,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" - Title="C# AVALONIA | LogViewer Control Example - Dot Net 7.0" + Title="AvaloniaLoggingDI-MsLogger" Icon="/Assets/avalonia-logo.ico" WindowStartupLocation="CenterScreen" Height="634" Width="600"> diff --git a/CSharp/Applications/AvaloniaLoggingNoDI/Helpers/LoggingHelper.cs b/CSharp/Applications/AvaloniaLoggingNoDI/Helpers/LoggingHelper.cs index 5a7cd26..9cad4ef 100644 --- a/CSharp/Applications/AvaloniaLoggingNoDI/Helpers/LoggingHelper.cs +++ b/CSharp/Applications/AvaloniaLoggingNoDI/Helpers/LoggingHelper.cs @@ -3,6 +3,7 @@ using System.Drawing; using Common.Core; using Microsoft.Extensions.Logging; using AvaloniaLoggingNoDI.Extensions; +using System.Diagnostics; namespace AvaloniaLoggingNoDI.Helpers; @@ -15,8 +16,8 @@ public static class LoggingHelper { // retrieve the log level from 'appsettings' string value = AppSettings.Current("Logging:LogLevel", "Default") ?? "Information"; - Enum.TryParse(value, out LogLevel logLevel); - + var success = Enum.TryParse(value, out LogLevel logLevel); + Debug.Assert(success, $"Failed to parse log level from appsettings. Value: '{value}'"); // wire up the loggers Factory = LoggerFactory.Create(builder => builder diff --git a/CSharp/Applications/AvaloniaLoggingNoDI/Program.cs b/CSharp/Applications/AvaloniaLoggingNoDI/Program.cs index 9e11908..319faa8 100644 --- a/CSharp/Applications/AvaloniaLoggingNoDI/Program.cs +++ b/CSharp/Applications/AvaloniaLoggingNoDI/Program.cs @@ -3,7 +3,7 @@ using System; namespace AvaloniaLoggingNoDI; -internal class Program +internal sealed class Program { // Initialization code. Don't use any Avalonia, third-party APIs or any // SynchronizationContext-reliant code before AppMain is called: things aren't initialized diff --git a/CSharp/Applications/AvaloniaLoggingNoDI/Views/MainWindow.axaml b/CSharp/Applications/AvaloniaLoggingNoDI/Views/MainWindow.axaml index 8a38025..b57f4fa 100644 --- a/CSharp/Applications/AvaloniaLoggingNoDI/Views/MainWindow.axaml +++ b/CSharp/Applications/AvaloniaLoggingNoDI/Views/MainWindow.axaml @@ -10,8 +10,8 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" - Title="C# AVALONIA MINIMAL | LogViewer Control Example - Dot Net 7.0" - Icon="/Assets/avalonia-logo.ico" + Title="AvaloniaLoggingNoDI-MsLogger" + Icon="/Assets/avalonia-logo.ico" WindowStartupLocation="CenterScreen" Height="634" Width="600"> diff --git a/CSharp/Applications/AvaloniaLoggingNoDI/Views/MainWindow.axaml.cs b/CSharp/Applications/AvaloniaLoggingNoDI/Views/MainWindow.axaml.cs index 60337bb..d263681 100644 --- a/CSharp/Applications/AvaloniaLoggingNoDI/Views/MainWindow.axaml.cs +++ b/CSharp/Applications/AvaloniaLoggingNoDI/Views/MainWindow.axaml.cs @@ -10,7 +10,7 @@ using System.Threading; namespace AvaloniaLoggingNoDI.Views; -public partial class MainWindow : Window, ILogDataStoreImpl +public partial class MainWindow : Window, ILogDataStoreCore { public MainWindow() { @@ -21,7 +21,7 @@ public partial class MainWindow : Window, ILogDataStoreImpl // Get the Launch mode bool isDevelopment = string.Equals(Environment.GetEnvironmentVariable("DOTNET_MODIFIABLE_ASSEMBLIES"), "debug", - StringComparison.InvariantCultureIgnoreCase); + StringComparison.OrdinalIgnoreCase); // initialize a logger & EventId Logger logger = new Logger(LoggingHelper.Factory); diff --git a/CSharp/Applications/AvaloniaSerilogDI/App.axaml.cs b/CSharp/Applications/AvaloniaSerilogDI/App.axaml.cs index 013c212..2bee82b 100644 --- a/CSharp/Applications/AvaloniaSerilogDI/App.axaml.cs +++ b/CSharp/Applications/AvaloniaSerilogDI/App.axaml.cs @@ -32,13 +32,11 @@ public partial class App : Application #endregion - #region Fields - - private IHost? _host; - private CancellationTokenSource? _cancellationTokenSource; - - #endregion + + private IHost? Host {get; set;} + private CancellationTokenSource? CancellationTokenSource { get; set;} + #region Methods public override void OnFrameworkInitializationCompleted() @@ -51,7 +49,7 @@ public partial class App : Application // catch all unhandled errors AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; - HostApplicationBuilder builder = Host.CreateApplicationBuilder(); + HostApplicationBuilder builder = Microsoft.Extensions.Hosting.Host.CreateApplicationBuilder(); builder // Register the Random Logging Service @@ -77,7 +75,7 @@ public partial class App : Application { Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(builder.Configuration) - .WriteTo.DataStoreLoggerSink( dataStoreProvider: () => _host!.Services.TryGetService()!,formatProvider: CultureInfo.InvariantCulture) + .WriteTo.DataStoreLoggerSink( dataStoreProvider: () => Host!.Services.TryGetService()!,formatProvider: CultureInfo.InvariantCulture) .CreateLogger(); cfg.ClearProviders().AddSerilog(Log.Logger); @@ -90,19 +88,19 @@ public partial class App : Application DataContext = service.GetRequiredService() }); - _host = builder.Build(); - _cancellationTokenSource = new(); + Host = builder.Build(); + CancellationTokenSource = new(); try { LogStartingMode(); // set and show - desktop.MainWindow = _host.Services.GetRequiredService(); + desktop.MainWindow = Host.Services.GetRequiredService(); desktop.ShutdownRequested += OnShutdownRequested; // startup background services - _ = _host.StartAsync(_cancellationTokenSource.Token); + _ = Host.StartAsync(CancellationTokenSource.Token); } catch (OperationCanceledException) { @@ -162,7 +160,7 @@ public partial class App : Application StringComparison.OrdinalIgnoreCase); // initialize a logger & EventId - ILogger logger = _host!.Services.GetRequiredService>(); + ILogger logger = Host!.Services.GetRequiredService>(); EventId eventId = new(id: 0, name: Assembly.GetEntryAssembly()!.GetName().Name); // log a test pattern for each log level @@ -175,7 +173,7 @@ public partial class App : Application private void CleanUp() { // tell the background services that we are shutting down - _ = _host?.StopAsync(_cancellationTokenSource?.Token ?? CancellationToken.None); + _ = Host?.StopAsync(CancellationTokenSource?.Token ?? CancellationToken.None); // flush logs Log.CloseAndFlush(); diff --git a/CSharp/Applications/AvaloniaSerilogDI/Program.cs b/CSharp/Applications/AvaloniaSerilogDI/Program.cs index 96b242a..da40120 100644 --- a/CSharp/Applications/AvaloniaSerilogDI/Program.cs +++ b/CSharp/Applications/AvaloniaSerilogDI/Program.cs @@ -3,7 +3,7 @@ using System; namespace AvaloniaSerilogDI; -internal class Program +internal sealed class Program { // Initialization code. Don't use any Avalonia, third-party APIs or any // SynchronizationContext-reliant code before AppMain is called: things aren't initialized diff --git a/CSharp/Applications/AvaloniaSerilogDI/Views/MainWindow.axaml b/CSharp/Applications/AvaloniaSerilogDI/Views/MainWindow.axaml index 2261709..df4a2b7 100644 --- a/CSharp/Applications/AvaloniaSerilogDI/Views/MainWindow.axaml +++ b/CSharp/Applications/AvaloniaSerilogDI/Views/MainWindow.axaml @@ -10,8 +10,8 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" - Title="C# AVALONIA SeriLog | LogViewer Control Example - Dot Net 7.0" - Icon="/Assets/avalonia-logo.ico" + Title="AvaloniaLoggingDI-Serilog" + Icon="/Assets/avalonia-logo.ico" WindowStartupLocation="CenterScreen" Height="634" Width="600"> diff --git a/CSharp/Applications/AvaloniaSerilogNoDI/MainWindow.axaml b/CSharp/Applications/AvaloniaSerilogNoDI/MainWindow.axaml index de41d25..74b0631 100644 --- a/CSharp/Applications/AvaloniaSerilogNoDI/MainWindow.axaml +++ b/CSharp/Applications/AvaloniaSerilogNoDI/MainWindow.axaml @@ -10,8 +10,8 @@ xmlns:control="clr-namespace:LogViewer.Avalonia;assembly=LogViewer.Avalonia" - Title="C# AVALONIA | SeriLog LogViewer Control Example - Dot Net 7.0" - Icon="/Assets/avalonia-logo.ico" + Title="AvaloniaLoggingNoDI-SerilogLogger" + Icon="/Assets/avalonia-logo.ico" WindowStartupLocation="CenterScreen" Height="634" Width="600"> diff --git a/CSharp/Applications/AvaloniaSerilogNoDI/MainWindow.axaml.cs b/CSharp/Applications/AvaloniaSerilogNoDI/MainWindow.axaml.cs index 656dcfc..3af7dd6 100644 --- a/CSharp/Applications/AvaloniaSerilogNoDI/MainWindow.axaml.cs +++ b/CSharp/Applications/AvaloniaSerilogNoDI/MainWindow.axaml.cs @@ -11,7 +11,7 @@ using System.Threading; namespace AvaloniaSerilogNoDI; -public partial class MainWindow : Window, ILogDataStoreImpl +public partial class MainWindow : Window, ILogDataStoreCore { #region Constructors @@ -20,11 +20,11 @@ public partial class MainWindow : Window, ILogDataStoreImpl InitializeComponent(); // Initialize _service and pass in the Logger - _service = new(new Logger(LoggingHelper.Factory)); + Service = new(new Logger(LoggingHelper.Factory)); // Get the Launch mode bool isDevelopment = string.Equals(Environment.GetEnvironmentVariable("DOTNET_MODIFIABLE_ASSEMBLIES"), "debug", - StringComparison.InvariantCultureIgnoreCase); + StringComparison.OrdinalIgnoreCase); // initialize a logger & EventId Logger logger = new Logger(LoggingHelper.Factory); @@ -37,7 +37,7 @@ public partial class MainWindow : Window, ILogDataStoreImpl logger.Emit(eventId, LogLevel.Information, $"Running in {(isDevelopment ? "Debug" : "Release")} mode"); // Start generating log entries - _ = _service.StartAsync(CancellationToken.None); + _ = Service.StartAsync(CancellationToken.None); // manually wire up the logging to the view ... the control will show backlog entries... DataStore = MainControlsDataStore.DataStore; @@ -52,12 +52,9 @@ public partial class MainWindow : Window, ILogDataStoreImpl #endregion - #region Fields - - private readonly RandomLoggingService? _service; - - #endregion - + + private RandomLoggingService? Service { get; set;} + #region Properties public ILogDataStore DataStore { get; init; } @@ -71,7 +68,7 @@ public partial class MainWindow : Window, ILogDataStoreImpl { Window.Closing -= OnClosing; - _ = _service?.StopAsync(); + _ = Service?.StopAsync(); LoggingHelper.CloseAndFlush(); } diff --git a/CSharp/Applications/AvaloniaSerilogNoDI/Program.cs b/CSharp/Applications/AvaloniaSerilogNoDI/Program.cs index c1b0108..3fedabd 100644 --- a/CSharp/Applications/AvaloniaSerilogNoDI/Program.cs +++ b/CSharp/Applications/AvaloniaSerilogNoDI/Program.cs @@ -3,7 +3,7 @@ using System; namespace AvaloniaSerilogNoDI; -internal class Program +internal sealed class Program { // Initialization code. Don't use any Avalonia, third-party APIs or any // SynchronizationContext-reliant code before AppMain is called: things aren't initialized diff --git a/CSharp/Background Services/RandomLogging.Service/RandomLoggingService.cs b/CSharp/Background Services/RandomLogging.Service/RandomLoggingService.cs index a12787c..802efec 100644 --- a/CSharp/Background Services/RandomLogging.Service/RandomLoggingService.cs +++ b/CSharp/Background Services/RandomLogging.Service/RandomLoggingService.cs @@ -164,7 +164,7 @@ public class RandomLoggingService : BackgroundService } _logger.Emit(GenerateEventId(), level, GetMessage(), - new Exception(_errorMessages[_random.Next(0, _errorMessages.Count)])); + new InvalidDataException(_errorMessages[_random.Next(0, _errorMessages.Count)])); } private EventId GenerateEventId() diff --git a/CSharp/Controls/LogViewer.Avalonia/LogViewerControl.axaml.cs b/CSharp/Controls/LogViewer.Avalonia/LogViewerControl.axaml.cs index b48d679..e46ec90 100644 --- a/CSharp/Controls/LogViewer.Avalonia/LogViewerControl.axaml.cs +++ b/CSharp/Controls/LogViewer.Avalonia/LogViewerControl.axaml.cs @@ -13,7 +13,7 @@ public partial class LogViewerControl : UserControl } - private ILogDataStoreImpl? vm; + private ILogDataStoreCore? vm; private LogModel? item; private void OnDataContextChanged(object? sender, EventArgs e) @@ -21,7 +21,7 @@ public partial class LogViewerControl : UserControl if (DataContext is null) return; - vm = (ILogDataStoreImpl)DataContext; + vm = (ILogDataStoreCore)DataContext; vm.DataStore.Entries.CollectionChanged += OnCollectionChanged; } diff --git a/CSharp/Core/Common.Core/Helpers/AppSettings.cs b/CSharp/Core/Common.Core/Helpers/AppSettings.cs index 23a4c04..2794b98 100644 --- a/CSharp/Core/Common.Core/Helpers/AppSettings.cs +++ b/CSharp/Core/Common.Core/Helpers/AppSettings.cs @@ -4,42 +4,36 @@ namespace Common.Core; public class AppSettings { - #region Constructors - + #region Constructors public AppSettings(IConfigurationSection configSection, string? key = null) { - _configSection = configSection; + ConfigSection = configSection; // ReSharper disable once VirtualMemberCallInConstructor GetValue(key); } - - #endregion - - #region Fields - - protected static AppSettings? _appSetting; - - // ReSharper disable once StaticMemberInGenericType - protected static IConfigurationSection? _configSection; - #endregion #region Properties - - public TOption? Value { get; set; } + protected static AppSettings? AppSetting { get; private set; } + // ReSharper disable once StaticMemberInGenericType + protected static IConfigurationSection? ConfigSection { get; private set; } + + public TOption? Value { get; set; } #endregion - #region Methods - + #region Methods +#pragma warning disable CA1000 // Do not declare static members on generic types public static TOption? Current(string section, string? key = null) { - _appSetting = GetCurrentSettings(section, key); - return _appSetting.Value; + AppSetting = GetCurrentSettings(section, key); + return AppSetting.Value; } + public static AppSettings GetCurrentSettings(string section, string? key = null) +#pragma warning restore CA1000 // Do not declare static members on generic types { string env = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production"; @@ -65,7 +59,7 @@ public class AppSettings { // no key, so must be a class/strut object Value = Activator.CreateInstance(); - _configSection!.Bind(Value); + ConfigSection!.Bind(Value); return; } @@ -77,10 +71,10 @@ public class AppSettings optionType == typeof(decimal) || optionType == typeof(float) || optionType == typeof(double)) - && _configSection != null) + && ConfigSection != null) { // we must be retrieving a value - Value = _configSection.GetValue(key); + Value = ConfigSection.GetValue(key); return; } diff --git a/CSharp/Core/LogViewer.Core/Extensions/LoggerExtensions.cs b/CSharp/Core/LogViewer.Core/Extensions/LoggerExtensions.cs index 92198a2..a283c91 100644 --- a/CSharp/Core/LogViewer.Core/Extensions/LoggerExtensions.cs +++ b/CSharp/Core/LogViewer.Core/Extensions/LoggerExtensions.cs @@ -41,13 +41,25 @@ public static class LoggerExtensions public static void TestPattern(this ILogger logger, EventId eventId) { - Exception exception = new Exception("Test Error Message"); + Exception exception = new InvalidDataException("Test Error Message"); +#pragma warning disable CA1848 // Use the LoggerMessage delegates logger.Emit(eventId, LogLevel.Trace, "Trace Test Pattern"); +#pragma warning restore CA1848 // Use the LoggerMessage delegates +#pragma warning disable CA1848 // Use the LoggerMessage delegates logger.Emit(eventId, LogLevel.Debug, "Debug Test Pattern"); +#pragma warning restore CA1848 // Use the LoggerMessage delegates +#pragma warning disable CA1848 // Use the LoggerMessage delegates logger.Emit(eventId, LogLevel.Information, "Information Test Pattern"); +#pragma warning restore CA1848 // Use the LoggerMessage delegates +#pragma warning disable CA1848 // Use the LoggerMessage delegates logger.Emit(eventId, LogLevel.Warning, "Warning Test Pattern"); +#pragma warning restore CA1848 // Use the LoggerMessage delegates +#pragma warning disable CA1848 // Use the LoggerMessage delegates logger.Emit(eventId, LogLevel.Error, "Error Test Pattern", exception); +#pragma warning restore CA1848 // Use the LoggerMessage delegates +#pragma warning disable CA1848 // Use the LoggerMessage delegates logger.Emit(eventId, LogLevel.Critical, "Critical Test Pattern", exception); +#pragma warning restore CA1848 // Use the LoggerMessage delegates } } \ No newline at end of file diff --git a/CSharp/Core/LogViewer.Core/Logging/ILogDataStoreImpl.cs b/CSharp/Core/LogViewer.Core/Logging/ILogDataStoreImpl.cs deleted file mode 100644 index ce7a0e1..0000000 --- a/CSharp/Core/LogViewer.Core/Logging/ILogDataStoreImpl.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace LogViewer.Core; - -public interface ILogDataStoreImpl -{ - public ILogDataStore DataStore { get; } -} \ No newline at end of file diff --git a/CSharp/Core/LogViewer.Core/ViewModels/LogViewerControlViewModel.cs b/CSharp/Core/LogViewer.Core/ViewModels/LogViewerControlViewModel.cs index 86e5d9b..2b7121f 100644 --- a/CSharp/Core/LogViewer.Core/ViewModels/LogViewerControlViewModel.cs +++ b/CSharp/Core/LogViewer.Core/ViewModels/LogViewerControlViewModel.cs @@ -2,7 +2,7 @@ namespace LogViewer.Core.ViewModels; -public class LogViewerControlViewModel : ViewModel, ILogDataStoreImpl +public class LogViewerControlViewModel : ViewModel, ILogDataStoreCore { #region Constructor diff --git a/CSharp/Core/MsLogger.Core/DataStoreLoggerProvider.cs b/CSharp/Core/MsLogger.Core/DataStoreLoggerProvider.cs index c1d1a94..cddb603 100644 --- a/CSharp/Core/MsLogger.Core/DataStoreLoggerProvider.cs +++ b/CSharp/Core/MsLogger.Core/DataStoreLoggerProvider.cs @@ -12,9 +12,9 @@ public class DataStoreLoggerProvider: ILoggerProvider public DataStoreLoggerProvider(IOptionsMonitor config, ILogDataStore dataStore) { - _dataStore = dataStore; + DataStore = dataStore; _currentConfig = config.CurrentValue; - _onChangeToken = config.OnChange(updatedConfig => _currentConfig = updatedConfig); + OnChangeToken = config.OnChange(updatedConfig => _currentConfig = updatedConfig); } #endregion @@ -23,25 +23,26 @@ public class DataStoreLoggerProvider: ILoggerProvider private DataStoreLoggerConfiguration _currentConfig; - private readonly IDisposable? _onChangeToken; - protected readonly ILogDataStore _dataStore; + private IDisposable? OnChangeToken { get; } + protected ILogDataStore DataStore { get; } - protected readonly ConcurrentDictionary _loggers = new(); + protected ConcurrentDictionary Loggers { get; } = new(); #endregion #region Methods public ILogger CreateLogger(string categoryName) - => _loggers.GetOrAdd(categoryName, name => new DataStoreLogger(name, GetCurrentConfig, _dataStore)); + => Loggers.GetOrAdd(categoryName, name => new DataStoreLogger(name, GetCurrentConfig, DataStore)); protected DataStoreLoggerConfiguration GetCurrentConfig() => _currentConfig; public void Dispose() { - _loggers.Clear(); - _onChangeToken?.Dispose(); + GC.SuppressFinalize(this); + Loggers.Clear(); + OnChangeToken?.Dispose(); } #endregion diff --git a/CSharp/Core/Serilog.Sinks.LogView.Core/DataStoreLoggerSink.cs b/CSharp/Core/Serilog.Sinks.LogView.Core/DataStoreLoggerSink.cs index b38e3c5..32415da 100644 --- a/CSharp/Core/Serilog.Sinks.LogView.Core/DataStoreLoggerSink.cs +++ b/CSharp/Core/Serilog.Sinks.LogView.Core/DataStoreLoggerSink.cs @@ -2,24 +2,18 @@ using LogViewer.Core; using Microsoft.Extensions.Logging; using Serilog.Core; +using System.Globalization; namespace Serilog.Sinks.LogView.Core; -public class DataStoreLoggerSink : ILogEventSink +public class DataStoreLoggerSink( + Func dataStoreProvider, + Func? getCurrentConfig = null, + IFormatProvider? formatProvider = null) : ILogEventSink { - protected readonly Func _dataStoreProvider; - - private readonly IFormatProvider? _formatProvider; - private readonly Func? _getCurrentConfig; - - public DataStoreLoggerSink(Func dataStoreProvider, - Func? getCurrentConfig = null, - IFormatProvider? formatProvider = null) - { - _formatProvider = formatProvider; - _dataStoreProvider = dataStoreProvider; - _getCurrentConfig = getCurrentConfig; - } + protected Func DataStoreProvider { get; } = dataStoreProvider; + private IFormatProvider? FormatProvider { get; } = formatProvider; + private Func? GetCurrentConfig { get; } = getCurrentConfig; public void Emit(LogEvent logEvent) { @@ -33,13 +27,13 @@ public class DataStoreLoggerSink : ILogEventSink _ => LogLevel.Information }; - DataStoreLoggerConfiguration config = _getCurrentConfig?.Invoke() ?? new DataStoreLoggerConfiguration(); + 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 message = logEvent.RenderMessage(FormatProvider); string exception = logEvent.Exception?.Message ?? (logEvent.Level >= LogEventLevel.Error ? message : string.Empty); @@ -50,7 +44,7 @@ public class DataStoreLoggerSink : ILogEventSink protected virtual void AddLogEntry(LogLevel logLevel, EventId eventId, string message, string exception, LogEntryColor color) { - ILogDataStore? dataStore = _dataStoreProvider.Invoke(); + ILogDataStore? dataStore = DataStoreProvider.Invoke(); // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract if (dataStore == null) @@ -79,11 +73,11 @@ public class DataStoreLoggerSink : ILogEventSink // ref: https://stackoverflow.com/a/56722516 StructureValue? value = src as StructureValue; - LogEventProperty? idProperty = value!.Properties.FirstOrDefault(x => x.Name.Equals("Id")); + LogEventProperty? idProperty = value!.Properties.FirstOrDefault(x => x.Name.Equals("Id", StringComparison.Ordinal)); if (idProperty is not null) - id = int.Parse(idProperty.Value.ToString()); + id = int.Parse(idProperty.Value.ToString(),CultureInfo.InvariantCulture); - LogEventProperty? nameProperty = value.Properties.FirstOrDefault(x => x.Name.Equals("Name")); + LogEventProperty? nameProperty = value.Properties.FirstOrDefault(x => x.Name.Equals("Name", StringComparison.Ordinal)); if (nameProperty is not null) eventName = nameProperty.Value.ToString().Trim('"');