Removed CommonCore
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -37,7 +37,6 @@
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Background Services\RandomLogging.Service\RandomLogging.Service.csproj" />
|
||||
<ProjectReference Include="..\..\Controls\LogViewer.Avalonia\LogViewer.Avalonia.csproj" />
|
||||
<ProjectReference Include="..\..\Core\Common.Core\Common.Core.csproj" />
|
||||
<ProjectReference Include="..\..\Core\MsLogger.Core\MsLogger.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace AvaloniaLoggingNoDI.Helpers;
|
||||
|
||||
public class AppSettings<TOption>
|
||||
{
|
||||
#region Constructors
|
||||
public AppSettings(IConfigurationSection configSection, string? key = null)
|
||||
{
|
||||
ConfigSection = configSection;
|
||||
|
||||
// ReSharper disable once VirtualMemberCallInConstructor
|
||||
GetValue(key);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
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
|
||||
#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<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";
|
||||
|
||||
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<TOption> settings = new AppSettings<TOption>(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<TOption>();
|
||||
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<TOption>(key);
|
||||
return;
|
||||
}
|
||||
|
||||
// Could not find a supported type
|
||||
throw new InvalidCastException($"Type {typeof(TOption).Name} is invalid");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using Common.Core;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using AvaloniaLoggingNoDI.Extensions;
|
||||
using System.Diagnostics;
|
||||
|
||||
@@ -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<TModel>(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<ILogDataStore>()!,formatProvider: CultureInfo.InvariantCulture)
|
||||
.WriteTo.DataStoreLoggerSink( dataStoreProvider: () => Host?.Services.TryGetService<ILogDataStore>()!,formatProvider: CultureInfo.InvariantCulture)
|
||||
.CreateLogger();
|
||||
|
||||
cfg.ClearProviders().AddSerilog(Log.Logger);
|
||||
|
||||
@@ -46,7 +46,6 @@
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Background Services\RandomLogging.Service\RandomLogging.Service.csproj" />
|
||||
<ProjectReference Include="..\..\Controls\LogViewer.Avalonia\LogViewer.Avalonia.csproj" />
|
||||
<ProjectReference Include="..\..\Core\Common.Core\Common.Core.csproj" />
|
||||
<ProjectReference Include="..\..\Core\Serilog.Sinks.LogView.Core\Serilog.Sinks.LogView.Core.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -45,7 +45,6 @@
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Background Services\RandomLogging.Service\RandomLogging.Service.csproj" />
|
||||
<ProjectReference Include="..\..\Controls\LogViewer.Avalonia\LogViewer.Avalonia.csproj" />
|
||||
<ProjectReference Include="..\..\Core\Common.Core\Common.Core.csproj" />
|
||||
<ProjectReference Include="..\..\Core\Serilog.Sinks.LogView.Core\Serilog.Sinks.LogView.Core.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace AvaloniaSerilogNoDI.Helpers;
|
||||
|
||||
public class AppSettings<TOption>
|
||||
{
|
||||
#region Constructors
|
||||
public AppSettings(IConfigurationSection configSection, string? key = null)
|
||||
{
|
||||
ConfigSection = configSection;
|
||||
|
||||
// ReSharper disable once VirtualMemberCallInConstructor
|
||||
GetValue(key);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
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
|
||||
#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<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";
|
||||
|
||||
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<TOption> settings = new AppSettings<TOption>(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<TOption>();
|
||||
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<TOption>(key);
|
||||
return;
|
||||
}
|
||||
|
||||
// Could not find a supported type
|
||||
throw new InvalidCastException($"Type {typeof(TOption).Name} is invalid");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user