diff --git a/CSharp/Applications/AvaloniaLoggingDI/ViewModels/MainViewModel.cs b/CSharp/Applications/AvaloniaLoggingDI/ViewModels/MainViewModel.cs index 6b0849a..39b2502 100644 --- a/CSharp/Applications/AvaloniaLoggingDI/ViewModels/MainViewModel.cs +++ b/CSharp/Applications/AvaloniaLoggingDI/ViewModels/MainViewModel.cs @@ -2,20 +2,16 @@ namespace AvaloniaLoggingDI.ViewModels; -public class MainViewModel : ViewModelBase +public class MainViewModel(LogViewerControlViewModel logViewer) : ViewModelBase { - #region Constructor - public MainViewModel(LogViewerControlViewModel logViewer) - { - LogViewer = logViewer; - } + #region Constructor #endregion #region Properties - public LogViewerControlViewModel LogViewer { get; } + public LogViewerControlViewModel LogViewer { get; } = logViewer; #endregion } \ No newline at end of file diff --git a/CSharp/Applications/AvaloniaLoggingNoDI/AvaloniaLoggingNoDI.csproj b/CSharp/Applications/AvaloniaLoggingNoDI/AvaloniaLoggingNoDI.csproj index 2dd7c80..c7ed33c 100644 --- a/CSharp/Applications/AvaloniaLoggingNoDI/AvaloniaLoggingNoDI.csproj +++ b/CSharp/Applications/AvaloniaLoggingNoDI/AvaloniaLoggingNoDI.csproj @@ -37,7 +37,6 @@ - diff --git a/CSharp/Core/Common.Core/Helpers/AppSettings.cs b/CSharp/Applications/AvaloniaLoggingNoDI/Helpers/AppSettings.cs similarity index 97% rename from CSharp/Core/Common.Core/Helpers/AppSettings.cs rename to CSharp/Applications/AvaloniaLoggingNoDI/Helpers/AppSettings.cs index 2794b98..52ad7ce 100644 --- a/CSharp/Core/Common.Core/Helpers/AppSettings.cs +++ b/CSharp/Applications/AvaloniaLoggingNoDI/Helpers/AppSettings.cs @@ -1,6 +1,8 @@ using Microsoft.Extensions.Configuration; +using System; +using System.IO; -namespace Common.Core; +namespace AvaloniaLoggingNoDI.Helpers; public class AppSettings { diff --git a/CSharp/Applications/AvaloniaLoggingNoDI/Helpers/LoggingHelper.cs b/CSharp/Applications/AvaloniaLoggingNoDI/Helpers/LoggingHelper.cs index 9cad4ef..9104868 100644 --- a/CSharp/Applications/AvaloniaLoggingNoDI/Helpers/LoggingHelper.cs +++ b/CSharp/Applications/AvaloniaLoggingNoDI/Helpers/LoggingHelper.cs @@ -1,6 +1,5 @@ using System; using System.Drawing; -using Common.Core; using Microsoft.Extensions.Logging; using AvaloniaLoggingNoDI.Extensions; using System.Diagnostics; diff --git a/CSharp/Applications/AvaloniaSerilogDI/App.axaml.cs b/CSharp/Applications/AvaloniaSerilogDI/App.axaml.cs index 2bee82b..ea5fd60 100644 --- a/CSharp/Applications/AvaloniaSerilogDI/App.axaml.cs +++ b/CSharp/Applications/AvaloniaSerilogDI/App.axaml.cs @@ -4,7 +4,6 @@ using Avalonia.Data.Core.Plugins; using Avalonia.Markup.Xaml; using AvaloniaSerilogDI.ViewModels; using AvaloniaSerilogDI.Views; -using Common.Core.Extensions; using LogViewer.Avalonia; using LogViewer.Core; using Microsoft.Extensions.DependencyInjection; @@ -23,6 +22,22 @@ using System.Threading; using Icon = MsBox.Avalonia.Enums.Icon; namespace AvaloniaSerilogDI; +public static class ServicesExtension +{ + public static TModel? TryGetService(this IServiceProvider serviceProvider) where TModel : class + { + try + { + return (TModel?)serviceProvider.GetService(typeof(TModel)); + } + catch (ObjectDisposedException) + { + // ignore as we do not care... + } + + return default; + } +} public partial class App : Application { #region Constructors @@ -75,7 +90,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); diff --git a/CSharp/Applications/AvaloniaSerilogDI/AvaloniaSerilogDI.csproj b/CSharp/Applications/AvaloniaSerilogDI/AvaloniaSerilogDI.csproj index 754c27c..1c02d58 100644 --- a/CSharp/Applications/AvaloniaSerilogDI/AvaloniaSerilogDI.csproj +++ b/CSharp/Applications/AvaloniaSerilogDI/AvaloniaSerilogDI.csproj @@ -46,7 +46,6 @@ - diff --git a/CSharp/Applications/AvaloniaSerilogDI/ViewModels/MainViewModel.cs b/CSharp/Applications/AvaloniaSerilogDI/ViewModels/MainViewModel.cs index 5f6a9ae..13eba5e 100644 --- a/CSharp/Applications/AvaloniaSerilogDI/ViewModels/MainViewModel.cs +++ b/CSharp/Applications/AvaloniaSerilogDI/ViewModels/MainViewModel.cs @@ -2,18 +2,16 @@ namespace AvaloniaSerilogDI.ViewModels; -public class MainViewModel : ViewModelBase +public class MainViewModel(LogViewerControlViewModel logViewer) : ViewModelBase { - #region Constructor - public MainViewModel(LogViewerControlViewModel logViewer) - => LogViewer = logViewer; + #region Constructor #endregion #region Properties - public LogViewerControlViewModel LogViewer { get; } + public LogViewerControlViewModel LogViewer { get; } = logViewer; #endregion } \ No newline at end of file diff --git a/CSharp/Applications/AvaloniaSerilogNoDI/AvaloniaSerilogNoDI.csproj b/CSharp/Applications/AvaloniaSerilogNoDI/AvaloniaSerilogNoDI.csproj index 131f668..b5d54b3 100644 --- a/CSharp/Applications/AvaloniaSerilogNoDI/AvaloniaSerilogNoDI.csproj +++ b/CSharp/Applications/AvaloniaSerilogNoDI/AvaloniaSerilogNoDI.csproj @@ -45,7 +45,6 @@ - diff --git a/CSharp/Applications/AvaloniaSerilogNoDI/Helpers/AppSettings.cs b/CSharp/Applications/AvaloniaSerilogNoDI/Helpers/AppSettings.cs new file mode 100644 index 0000000..640c8f0 --- /dev/null +++ b/CSharp/Applications/AvaloniaSerilogNoDI/Helpers/AppSettings.cs @@ -0,0 +1,88 @@ +using Microsoft.Extensions.Configuration; +using System; +using System.IO; + +namespace AvaloniaSerilogNoDI.Helpers; + +public class AppSettings +{ + #region Constructors + public AppSettings(IConfigurationSection configSection, string? key = null) + { + ConfigSection = configSection; + + // ReSharper disable once VirtualMemberCallInConstructor + GetValue(key); + } + #endregion + + #region Properties + 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 +#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; + } + + + 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"; + + IConfigurationBuilder builder = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{env}.json", optional: true, reloadOnChange: true) + .AddEnvironmentVariables(); + + IConfigurationRoot configuration = builder.Build(); + + if (string.IsNullOrEmpty(section)) + section = "AppSettings"; // default + + AppSettings settings = new AppSettings(configuration.GetSection(section), key); + + return settings; + } + + protected virtual void GetValue(string? key) + { + if (key is null) + { + // no key, so must be a class/strut object + Value = Activator.CreateInstance(); + ConfigSection!.Bind(Value); + return; + } + + Type optionType = typeof(TOption); + + if ((optionType == typeof(string) || + optionType == typeof(int) || + optionType == typeof(long) || + optionType == typeof(decimal) || + optionType == typeof(float) || + optionType == typeof(double)) + && ConfigSection != null) + { + // we must be retrieving a value + Value = ConfigSection.GetValue(key); + return; + } + + // Could not find a supported type + throw new InvalidCastException($"Type {typeof(TOption).Name} is invalid"); + } + + #endregion +} \ No newline at end of file diff --git a/CSharp/Applications/AvaloniaSerilogNoDI/Helpers/LoggingHelper.cs b/CSharp/Applications/AvaloniaSerilogNoDI/Helpers/LoggingHelper.cs index b4b15b0..a62061d 100644 --- a/CSharp/Applications/AvaloniaSerilogNoDI/Helpers/LoggingHelper.cs +++ b/CSharp/Applications/AvaloniaSerilogNoDI/Helpers/LoggingHelper.cs @@ -1,19 +1,30 @@ using AvaloniaSerilogNoDI.DataStores; -using Common.Core.Extensions; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Serilog; using Serilog.Sinks.LogView.Core; +using System; using System.Drawing; using System.Globalization; +using System.IO; namespace AvaloniaSerilogNoDI.Helpers; + // application-wide DataStoreLogger Factory ... returns a wired up Logger instance public static class LoggingHelper { - #region Constructors + public static IConfigurationBuilder Initialize(this IConfigurationBuilder builder) + { + string env = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production"; + return builder + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{env}.json", optional: true, reloadOnChange: true) + .AddEnvironmentVariables(); + } + #region Constructors static LoggingHelper() { IConfigurationRoot configuration = new ConfigurationBuilder() diff --git a/CSharp/Background Services/RandomLogging.Service/RandomLoggingService.cs b/CSharp/Background Services/RandomLogging.Service/RandomLoggingService.cs index 802efec..9438e23 100644 --- a/CSharp/Background Services/RandomLogging.Service/RandomLoggingService.cs +++ b/CSharp/Background Services/RandomLogging.Service/RandomLoggingService.cs @@ -3,12 +3,10 @@ using Microsoft.Extensions.Logging; namespace RandomLogging.Service; -public class RandomLoggingService : BackgroundService +public class RandomLoggingService(ILogger logger) : BackgroundService { - #region Constructors - public RandomLoggingService(ILogger logger) - => _logger = logger; + #region Constructors #endregion @@ -16,7 +14,7 @@ public class RandomLoggingService : BackgroundService #region Injected - private readonly ILogger _logger; + private readonly ILogger _logger = logger; #endregion diff --git a/CSharp/Core/Common.Core/Common.Core.csproj b/CSharp/Core/Common.Core/Common.Core.csproj deleted file mode 100644 index 5b90f3a..0000000 --- a/CSharp/Core/Common.Core/Common.Core.csproj +++ /dev/null @@ -1,15 +0,0 @@ - - - - enable - - - - - - - - - - - diff --git a/CSharp/Core/Common.Core/Extensions/ConfigurationExtension.cs b/CSharp/Core/Common.Core/Extensions/ConfigurationExtension.cs deleted file mode 100644 index cbfcd80..0000000 --- a/CSharp/Core/Common.Core/Extensions/ConfigurationExtension.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Microsoft.Extensions.Configuration; - -namespace Common.Core.Extensions; - -public static class ConfigurationExtension -{ - public static IConfigurationBuilder Initialize(this IConfigurationBuilder builder) - { - string env = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production"; - - return builder - .SetBasePath(Directory.GetCurrentDirectory()) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{env}.json", optional: true, reloadOnChange: true) - .AddEnvironmentVariables(); - } -} \ No newline at end of file diff --git a/CSharp/Core/Common.Core/Extensions/ServicesExtension.cs b/CSharp/Core/Common.Core/Extensions/ServicesExtension.cs deleted file mode 100644 index 1cc42b4..0000000 --- a/CSharp/Core/Common.Core/Extensions/ServicesExtension.cs +++ /dev/null @@ -1,18 +0,0 @@ -namespace Common.Core.Extensions; - -public static class ServicesExtension -{ - public static TModel? TryGetService(this IServiceProvider serviceProvider) where TModel : class - { - try - { - return (TModel?)serviceProvider.GetService(typeof(TModel)); - } - catch (ObjectDisposedException) - { - // ignore as we do not care... - } - - return default; - } -} \ No newline at end of file diff --git a/CSharp/Core/LogViewer.Core/Extensions/LoggerExtensions.cs b/CSharp/Core/LogViewer.Core/Extensions/LoggerExtensions.cs index a283c91..44fa3f9 100644 --- a/CSharp/Core/LogViewer.Core/Extensions/LoggerExtensions.cs +++ b/CSharp/Core/LogViewer.Core/Extensions/LoggerExtensions.cs @@ -45,21 +45,10 @@ public static class LoggerExtensions #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/LogViewer.Core.csproj b/CSharp/Core/LogViewer.Core/LogViewer.Core.csproj index 1f935d9..b81e708 100644 --- a/CSharp/Core/LogViewer.Core/LogViewer.Core.csproj +++ b/CSharp/Core/LogViewer.Core/LogViewer.Core.csproj @@ -8,8 +8,4 @@ - - - - diff --git a/CSharp/Core/LogViewer.Core/ViewModels/LogViewerControlViewModel.cs b/CSharp/Core/LogViewer.Core/ViewModels/LogViewerControlViewModel.cs index 2b7121f..d3b28c8 100644 --- a/CSharp/Core/LogViewer.Core/ViewModels/LogViewerControlViewModel.cs +++ b/CSharp/Core/LogViewer.Core/ViewModels/LogViewerControlViewModel.cs @@ -1,21 +1,7 @@ -using Mvvm.Core; +namespace LogViewer.Core.ViewModels; -namespace LogViewer.Core.ViewModels; - -public class LogViewerControlViewModel : ViewModel, ILogDataStoreCore +public class LogViewerControlViewModel(ILogDataStore dataStore) : ILogDataStoreCore { - #region Constructor - - public LogViewerControlViewModel(ILogDataStore dataStore) - { - DataStore = dataStore; - } - - #endregion - - #region Properties - - public ILogDataStore DataStore { get; set; } - - #endregion + public ILogDataStore DataStore { get; set; } = dataStore; + } \ No newline at end of file diff --git a/LogViewerControl.sln b/LogViewerControl.sln index 9953115..286bcc3 100644 --- a/LogViewerControl.sln +++ b/LogViewerControl.sln @@ -5,8 +5,6 @@ VisualStudioVersion = 18.4.11626.88 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Core", "Core", "{A3BEB004-4DF7-4281-9A08-8A7BCD4E3CC9}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mvvm.Core", "CSharp\Core\Mvvm.Core\Mvvm.Core.csproj", "{BB614345-449F-46AD-BE8C-5E2B7616EDE2}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LogViewer.Core", "CSharp\Core\LogViewer.Core\LogViewer.Core.csproj", "{34F75D8B-6F15-4DE4-8335-FED83557EB8E}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Apps", "Apps", "{42E99803-0A95-4172-9079-3B8BF8CBDE9F}" @@ -15,8 +13,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Controls", "Controls", "{E5 EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Background Services", "Background Services", "{0CDEA51D-46FE-4767-BA2E-8F14582A926D}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Common.Core", "CSharp\Core\Common.Core\Common.Core.csproj", "{1688A0C1-1AE6-49F6-972E-C419E2A3B58F}" -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{006FDAED-6319-4976-B8BA-8D94E4574139}" ProjectSection(SolutionItems) = preProject LICENSE = LICENSE @@ -51,18 +47,10 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {BB614345-449F-46AD-BE8C-5E2B7616EDE2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {BB614345-449F-46AD-BE8C-5E2B7616EDE2}.Debug|Any CPU.Build.0 = Debug|Any CPU - {BB614345-449F-46AD-BE8C-5E2B7616EDE2}.Release|Any CPU.ActiveCfg = Release|Any CPU - {BB614345-449F-46AD-BE8C-5E2B7616EDE2}.Release|Any CPU.Build.0 = Release|Any CPU {34F75D8B-6F15-4DE4-8335-FED83557EB8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {34F75D8B-6F15-4DE4-8335-FED83557EB8E}.Debug|Any CPU.Build.0 = Debug|Any CPU {34F75D8B-6F15-4DE4-8335-FED83557EB8E}.Release|Any CPU.ActiveCfg = Release|Any CPU {34F75D8B-6F15-4DE4-8335-FED83557EB8E}.Release|Any CPU.Build.0 = Release|Any CPU - {1688A0C1-1AE6-49F6-972E-C419E2A3B58F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {1688A0C1-1AE6-49F6-972E-C419E2A3B58F}.Debug|Any CPU.Build.0 = Debug|Any CPU - {1688A0C1-1AE6-49F6-972E-C419E2A3B58F}.Release|Any CPU.ActiveCfg = Release|Any CPU - {1688A0C1-1AE6-49F6-972E-C419E2A3B58F}.Release|Any CPU.Build.0 = Release|Any CPU {18BA2294-FE64-481F-A86F-F5FD84438B66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {18BA2294-FE64-481F-A86F-F5FD84438B66}.Debug|Any CPU.Build.0 = Debug|Any CPU {18BA2294-FE64-481F-A86F-F5FD84438B66}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -100,9 +88,7 @@ Global HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution - {BB614345-449F-46AD-BE8C-5E2B7616EDE2} = {A3BEB004-4DF7-4281-9A08-8A7BCD4E3CC9} {34F75D8B-6F15-4DE4-8335-FED83557EB8E} = {A3BEB004-4DF7-4281-9A08-8A7BCD4E3CC9} - {1688A0C1-1AE6-49F6-972E-C419E2A3B58F} = {A3BEB004-4DF7-4281-9A08-8A7BCD4E3CC9} {18BA2294-FE64-481F-A86F-F5FD84438B66} = {0CDEA51D-46FE-4767-BA2E-8F14582A926D} {0EDAAABD-495D-43A4-BDFB-A0506CAAC07E} = {23CB559B-2361-4ED6-8A26-D1B1C2005D65} {23CB559B-2361-4ED6-8A26-D1B1C2005D65} = {A3BEB004-4DF7-4281-9A08-8A7BCD4E3CC9}