Fixed Warnings

This commit is contained in:
Matthias Heil
2026-04-05 08:00:09 +02:00
parent dfb879bfb3
commit 511a5f9f51
21 changed files with 104 additions and 115 deletions
+16 -22
View File
@@ -4,42 +4,36 @@ namespace Common.Core;
public class AppSettings<TOption>
{
#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<TOption>? _appSetting;
// ReSharper disable once StaticMemberInGenericType
protected static IConfigurationSection? _configSection;
#endregion
#region Properties
public TOption? Value { get; set; }
protected static AppSettings<TOption>? 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<TOption> 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<TOption>
{
// no key, so must be a class/strut object
Value = Activator.CreateInstance<TOption>();
_configSection!.Bind(Value);
ConfigSection!.Bind(Value);
return;
}
@@ -77,10 +71,10 @@ public class AppSettings<TOption>
optionType == typeof(decimal) ||
optionType == typeof(float) ||
optionType == typeof(double))
&& _configSection != null)
&& ConfigSection != null)
{
// we must be retrieving a value
Value = _configSection.GetValue<TOption>(key);
Value = ConfigSection.GetValue<TOption>(key);
return;
}
@@ -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
}
}
@@ -1,6 +0,0 @@
namespace LogViewer.Core;
public interface ILogDataStoreImpl
{
public ILogDataStore DataStore { get; }
}
@@ -2,7 +2,7 @@
namespace LogViewer.Core.ViewModels;
public class LogViewerControlViewModel : ViewModel, ILogDataStoreImpl
public class LogViewerControlViewModel : ViewModel, ILogDataStoreCore
{
#region Constructor
@@ -12,9 +12,9 @@ public class DataStoreLoggerProvider: ILoggerProvider
public DataStoreLoggerProvider(IOptionsMonitor<DataStoreLoggerConfiguration> 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<string, DataStoreLogger> _loggers = new();
protected ConcurrentDictionary<string, DataStoreLogger> 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
@@ -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<ILogDataStore> dataStoreProvider,
Func<DataStoreLoggerConfiguration>? getCurrentConfig = null,
IFormatProvider? formatProvider = null) : ILogEventSink
{
protected readonly Func<ILogDataStore> _dataStoreProvider;
private readonly IFormatProvider? _formatProvider;
private readonly Func<DataStoreLoggerConfiguration>? _getCurrentConfig;
public DataStoreLoggerSink(Func<ILogDataStore> dataStoreProvider,
Func<DataStoreLoggerConfiguration>? getCurrentConfig = null,
IFormatProvider? formatProvider = null)
{
_formatProvider = formatProvider;
_dataStoreProvider = dataStoreProvider;
_getCurrentConfig = getCurrentConfig;
}
protected Func<ILogDataStore> DataStoreProvider { get; } = dataStoreProvider;
private IFormatProvider? FormatProvider { get; } = formatProvider;
private Func<DataStoreLoggerConfiguration>? 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('"');