Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 149de07b0e | |||
| 511a5f9f51 | |||
| dfb879bfb3 |
@@ -1 +1,3 @@
|
|||||||
/.vs
|
/.vs
|
||||||
|
/bin
|
||||||
|
/packages
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ public partial class App : Application
|
|||||||
// catch all unhandled errors
|
// catch all unhandled errors
|
||||||
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
|
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
|
||||||
|
|
||||||
HostApplicationBuilder builder = Host.CreateApplicationBuilder();
|
HostApplicationBuilder builder = Microsoft.Extensions.Hosting.Host.CreateApplicationBuilder();
|
||||||
|
|
||||||
builder
|
builder
|
||||||
/*
|
/*
|
||||||
@@ -87,24 +87,24 @@ public partial class App : Application
|
|||||||
|
|
||||||
services
|
services
|
||||||
.AddSingleton<MainViewModel>()
|
.AddSingleton<MainViewModel>()
|
||||||
.AddSingleton<MainWindow>(service => new MainWindow
|
.AddSingleton(service => new MainWindow
|
||||||
{
|
{
|
||||||
DataContext = service.GetRequiredService<MainViewModel>()
|
DataContext = service.GetRequiredService<MainViewModel>()
|
||||||
});
|
});
|
||||||
|
|
||||||
_host = builder.Build();
|
Host = builder.Build();
|
||||||
_cancellationTokenSource = new();
|
CancellationTokenSource = new();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
LogStartingMode();
|
LogStartingMode();
|
||||||
|
|
||||||
// set and show
|
// set and show
|
||||||
desktop.MainWindow = _host.Services.GetRequiredService<MainWindow>();
|
desktop.MainWindow = Host.Services.GetRequiredService<MainWindow>();
|
||||||
desktop.ShutdownRequested += OnShutdownRequested;
|
desktop.ShutdownRequested += OnShutdownRequested;
|
||||||
|
|
||||||
// startup background services
|
// startup background services
|
||||||
_ = _host.StartAsync(_cancellationTokenSource.Token);
|
_ = Host.StartAsync(CancellationTokenSource.Token);
|
||||||
}
|
}
|
||||||
catch (OperationCanceledException)
|
catch (OperationCanceledException)
|
||||||
{
|
{
|
||||||
@@ -131,15 +131,13 @@ public partial class App : Application
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void OnShutdownRequested(object? sender, ShutdownRequestedEventArgs e)
|
private void OnShutdownRequested(object? sender, ShutdownRequestedEventArgs e)
|
||||||
=> _ = _host!.StopAsync(_cancellationTokenSource!.Token);
|
=> _ = Host?.StopAsync(CancellationTokenSource!.Token);
|
||||||
|
|
||||||
#region Fields
|
|
||||||
|
private IHost? Host { get; set; }
|
||||||
private IHost? _host;
|
private CancellationTokenSource? CancellationTokenSource { get; set; }
|
||||||
private CancellationTokenSource? _cancellationTokenSource;
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
|
|
||||||
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
|
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
|
||||||
=> ShowMessageBox("Unhandled Error", ((Exception)e.ExceptionObject).Message);
|
=> ShowMessageBox("Unhandled Error", ((Exception)e.ExceptionObject).Message);
|
||||||
|
|
||||||
@@ -161,7 +159,7 @@ public partial class App : Application
|
|||||||
StringComparison.OrdinalIgnoreCase);
|
StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
// initialize a logger & EventId
|
// initialize a logger & EventId
|
||||||
ILogger<App> logger = _host!.Services.GetRequiredService<ILogger<App>>();
|
ILogger<App> logger = Host!.Services.GetRequiredService<ILogger<App>>();
|
||||||
EventId eventId = new EventId(id: 0, name: Assembly.GetEntryAssembly()!.GetName().Name);
|
EventId eventId = new EventId(id: 0, name: Assembly.GetEntryAssembly()!.GetName().Name);
|
||||||
|
|
||||||
// log a test pattern for each log level
|
// log a test pattern for each log level
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ using System;
|
|||||||
|
|
||||||
namespace AvaloniaLoggingDI;
|
namespace AvaloniaLoggingDI;
|
||||||
|
|
||||||
internal class Program
|
internal sealed class Program
|
||||||
{
|
{
|
||||||
// Initialization code. Don't use any Avalonia, third-party APIs or any
|
// Initialization code. Don't use any Avalonia, third-party APIs or any
|
||||||
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
|
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
|
||||||
|
|||||||
@@ -2,20 +2,16 @@
|
|||||||
|
|
||||||
namespace AvaloniaLoggingDI.ViewModels;
|
namespace AvaloniaLoggingDI.ViewModels;
|
||||||
|
|
||||||
public class MainViewModel : ViewModelBase
|
public class MainViewModel(LogViewerControlViewModel logViewer) : ViewModelBase
|
||||||
{
|
{
|
||||||
#region Constructor
|
|
||||||
|
|
||||||
public MainViewModel(LogViewerControlViewModel logViewer)
|
#region Constructor
|
||||||
{
|
|
||||||
LogViewer = logViewer;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
|
|
||||||
public LogViewerControlViewModel LogViewer { get; }
|
public LogViewerControlViewModel LogViewer { get; } = logViewer;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
@@ -10,7 +10,7 @@
|
|||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
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"
|
Icon="/Assets/avalonia-logo.ico"
|
||||||
WindowStartupLocation="CenterScreen" Height="634" Width="600">
|
WindowStartupLocation="CenterScreen" Height="634" Width="600">
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,6 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\Background Services\RandomLogging.Service\RandomLogging.Service.csproj" />
|
<ProjectReference Include="..\..\Background Services\RandomLogging.Service\RandomLogging.Service.csproj" />
|
||||||
<ProjectReference Include="..\..\Controls\LogViewer.Avalonia\LogViewer.Avalonia.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" />
|
<ProjectReference Include="..\..\Core\MsLogger.Core\MsLogger.Core.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
+19
-23
@@ -1,45 +1,41 @@
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
namespace Common.Core;
|
namespace AvaloniaLoggingNoDI.Helpers;
|
||||||
|
|
||||||
public class AppSettings<TOption>
|
public class AppSettings<TOption>
|
||||||
{
|
{
|
||||||
#region Constructors
|
#region Constructors
|
||||||
|
|
||||||
public AppSettings(IConfigurationSection configSection, string? key = null)
|
public AppSettings(IConfigurationSection configSection, string? key = null)
|
||||||
{
|
{
|
||||||
_configSection = configSection;
|
ConfigSection = configSection;
|
||||||
|
|
||||||
// ReSharper disable once VirtualMemberCallInConstructor
|
// ReSharper disable once VirtualMemberCallInConstructor
|
||||||
GetValue(key);
|
GetValue(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Fields
|
|
||||||
|
|
||||||
protected static AppSettings<TOption>? _appSetting;
|
|
||||||
|
|
||||||
// ReSharper disable once StaticMemberInGenericType
|
|
||||||
protected static IConfigurationSection? _configSection;
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
|
protected static AppSettings<TOption>? AppSetting { get; private set; }
|
||||||
public TOption? Value { get; set; }
|
|
||||||
|
|
||||||
|
// ReSharper disable once StaticMemberInGenericType
|
||||||
|
protected static IConfigurationSection? ConfigSection { get; private set; }
|
||||||
|
|
||||||
|
public TOption? Value { get; set; }
|
||||||
#endregion
|
#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)
|
public static TOption? Current(string section, string? key = null)
|
||||||
{
|
{
|
||||||
_appSetting = GetCurrentSettings(section, key);
|
AppSetting = GetCurrentSettings(section, key);
|
||||||
return _appSetting.Value;
|
return AppSetting.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static AppSettings<TOption> GetCurrentSettings(string section, string? key = null)
|
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";
|
string env = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production";
|
||||||
|
|
||||||
@@ -65,7 +61,7 @@ public class AppSettings<TOption>
|
|||||||
{
|
{
|
||||||
// no key, so must be a class/strut object
|
// no key, so must be a class/strut object
|
||||||
Value = Activator.CreateInstance<TOption>();
|
Value = Activator.CreateInstance<TOption>();
|
||||||
_configSection!.Bind(Value);
|
ConfigSection!.Bind(Value);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,10 +73,10 @@ public class AppSettings<TOption>
|
|||||||
optionType == typeof(decimal) ||
|
optionType == typeof(decimal) ||
|
||||||
optionType == typeof(float) ||
|
optionType == typeof(float) ||
|
||||||
optionType == typeof(double))
|
optionType == typeof(double))
|
||||||
&& _configSection != null)
|
&& ConfigSection != null)
|
||||||
{
|
{
|
||||||
// we must be retrieving a value
|
// we must be retrieving a value
|
||||||
Value = _configSection.GetValue<TOption>(key);
|
Value = ConfigSection.GetValue<TOption>(key);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using Common.Core;
|
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using AvaloniaLoggingNoDI.Extensions;
|
using AvaloniaLoggingNoDI.Extensions;
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
namespace AvaloniaLoggingNoDI.Helpers;
|
namespace AvaloniaLoggingNoDI.Helpers;
|
||||||
|
|
||||||
@@ -15,8 +15,8 @@ public static class LoggingHelper
|
|||||||
{
|
{
|
||||||
// retrieve the log level from 'appsettings'
|
// retrieve the log level from 'appsettings'
|
||||||
string value = AppSettings<string>.Current("Logging:LogLevel", "Default") ?? "Information";
|
string value = AppSettings<string>.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
|
// wire up the loggers
|
||||||
Factory = LoggerFactory.Create(builder => builder
|
Factory = LoggerFactory.Create(builder => builder
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ using System;
|
|||||||
|
|
||||||
namespace AvaloniaLoggingNoDI;
|
namespace AvaloniaLoggingNoDI;
|
||||||
|
|
||||||
internal class Program
|
internal sealed class Program
|
||||||
{
|
{
|
||||||
// Initialization code. Don't use any Avalonia, third-party APIs or any
|
// Initialization code. Don't use any Avalonia, third-party APIs or any
|
||||||
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
|
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
|
||||||
|
|||||||
@@ -10,8 +10,8 @@
|
|||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
|
|
||||||
Title="C# AVALONIA MINIMAL | LogViewer Control Example - Dot Net 7.0"
|
Title="AvaloniaLoggingNoDI-MsLogger"
|
||||||
Icon="/Assets/avalonia-logo.ico"
|
Icon="/Assets/avalonia-logo.ico"
|
||||||
WindowStartupLocation="CenterScreen" Height="634" Width="600">
|
WindowStartupLocation="CenterScreen" Height="634" Width="600">
|
||||||
|
|
||||||
<control:LogViewerControl x:Name="LogViewerControl" />
|
<control:LogViewerControl x:Name="LogViewerControl" />
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ using System.Threading;
|
|||||||
|
|
||||||
namespace AvaloniaLoggingNoDI.Views;
|
namespace AvaloniaLoggingNoDI.Views;
|
||||||
|
|
||||||
public partial class MainWindow : Window, ILogDataStoreImpl
|
public partial class MainWindow : Window, ILogDataStoreCore
|
||||||
{
|
{
|
||||||
public MainWindow()
|
public MainWindow()
|
||||||
{
|
{
|
||||||
@@ -21,7 +21,7 @@ public partial class MainWindow : Window, ILogDataStoreImpl
|
|||||||
|
|
||||||
// Get the Launch mode
|
// Get the Launch mode
|
||||||
bool isDevelopment = string.Equals(Environment.GetEnvironmentVariable("DOTNET_MODIFIABLE_ASSEMBLIES"), "debug",
|
bool isDevelopment = string.Equals(Environment.GetEnvironmentVariable("DOTNET_MODIFIABLE_ASSEMBLIES"), "debug",
|
||||||
StringComparison.InvariantCultureIgnoreCase);
|
StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
// initialize a logger & EventId
|
// initialize a logger & EventId
|
||||||
Logger<MainWindow> logger = new Logger<MainWindow>(LoggingHelper.Factory);
|
Logger<MainWindow> logger = new Logger<MainWindow>(LoggingHelper.Factory);
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ using Avalonia.Data.Core.Plugins;
|
|||||||
using Avalonia.Markup.Xaml;
|
using Avalonia.Markup.Xaml;
|
||||||
using AvaloniaSerilogDI.ViewModels;
|
using AvaloniaSerilogDI.ViewModels;
|
||||||
using AvaloniaSerilogDI.Views;
|
using AvaloniaSerilogDI.Views;
|
||||||
using Common.Core.Extensions;
|
|
||||||
using LogViewer.Avalonia;
|
using LogViewer.Avalonia;
|
||||||
using LogViewer.Core;
|
using LogViewer.Core;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
@@ -23,6 +22,22 @@ using System.Threading;
|
|||||||
using Icon = MsBox.Avalonia.Enums.Icon;
|
using Icon = MsBox.Avalonia.Enums.Icon;
|
||||||
namespace AvaloniaSerilogDI;
|
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
|
public partial class App : Application
|
||||||
{
|
{
|
||||||
#region Constructors
|
#region Constructors
|
||||||
@@ -32,13 +47,11 @@ public partial class App : Application
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Fields
|
|
||||||
|
private IHost? Host {get; set;}
|
||||||
private IHost? _host;
|
private CancellationTokenSource? CancellationTokenSource { get; set;}
|
||||||
private CancellationTokenSource? _cancellationTokenSource;
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
|
|
||||||
public override void OnFrameworkInitializationCompleted()
|
public override void OnFrameworkInitializationCompleted()
|
||||||
@@ -51,7 +64,7 @@ public partial class App : Application
|
|||||||
// catch all unhandled errors
|
// catch all unhandled errors
|
||||||
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
|
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
|
||||||
|
|
||||||
HostApplicationBuilder builder = Host.CreateApplicationBuilder();
|
HostApplicationBuilder builder = Microsoft.Extensions.Hosting.Host.CreateApplicationBuilder();
|
||||||
|
|
||||||
builder
|
builder
|
||||||
// Register the Random Logging Service
|
// Register the Random Logging Service
|
||||||
@@ -77,7 +90,7 @@ public partial class App : Application
|
|||||||
{
|
{
|
||||||
Log.Logger = new LoggerConfiguration()
|
Log.Logger = new LoggerConfiguration()
|
||||||
.ReadFrom.Configuration(builder.Configuration)
|
.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();
|
.CreateLogger();
|
||||||
|
|
||||||
cfg.ClearProviders().AddSerilog(Log.Logger);
|
cfg.ClearProviders().AddSerilog(Log.Logger);
|
||||||
@@ -90,19 +103,19 @@ public partial class App : Application
|
|||||||
DataContext = service.GetRequiredService<MainViewModel>()
|
DataContext = service.GetRequiredService<MainViewModel>()
|
||||||
});
|
});
|
||||||
|
|
||||||
_host = builder.Build();
|
Host = builder.Build();
|
||||||
_cancellationTokenSource = new();
|
CancellationTokenSource = new();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
LogStartingMode();
|
LogStartingMode();
|
||||||
|
|
||||||
// set and show
|
// set and show
|
||||||
desktop.MainWindow = _host.Services.GetRequiredService<MainWindow>();
|
desktop.MainWindow = Host.Services.GetRequiredService<MainWindow>();
|
||||||
desktop.ShutdownRequested += OnShutdownRequested;
|
desktop.ShutdownRequested += OnShutdownRequested;
|
||||||
|
|
||||||
// startup background services
|
// startup background services
|
||||||
_ = _host.StartAsync(_cancellationTokenSource.Token);
|
_ = Host.StartAsync(CancellationTokenSource.Token);
|
||||||
}
|
}
|
||||||
catch (OperationCanceledException)
|
catch (OperationCanceledException)
|
||||||
{
|
{
|
||||||
@@ -162,7 +175,7 @@ public partial class App : Application
|
|||||||
StringComparison.OrdinalIgnoreCase);
|
StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
// initialize a logger & EventId
|
// initialize a logger & EventId
|
||||||
ILogger<App> logger = _host!.Services.GetRequiredService<ILogger<App>>();
|
ILogger<App> logger = Host!.Services.GetRequiredService<ILogger<App>>();
|
||||||
EventId eventId = new(id: 0, name: Assembly.GetEntryAssembly()!.GetName().Name);
|
EventId eventId = new(id: 0, name: Assembly.GetEntryAssembly()!.GetName().Name);
|
||||||
|
|
||||||
// log a test pattern for each log level
|
// log a test pattern for each log level
|
||||||
@@ -175,7 +188,7 @@ public partial class App : Application
|
|||||||
private void CleanUp()
|
private void CleanUp()
|
||||||
{
|
{
|
||||||
// tell the background services that we are shutting down
|
// tell the background services that we are shutting down
|
||||||
_ = _host?.StopAsync(_cancellationTokenSource?.Token ?? CancellationToken.None);
|
_ = Host?.StopAsync(CancellationTokenSource?.Token ?? CancellationToken.None);
|
||||||
|
|
||||||
// flush logs
|
// flush logs
|
||||||
Log.CloseAndFlush();
|
Log.CloseAndFlush();
|
||||||
|
|||||||
@@ -46,7 +46,6 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\Background Services\RandomLogging.Service\RandomLogging.Service.csproj" />
|
<ProjectReference Include="..\..\Background Services\RandomLogging.Service\RandomLogging.Service.csproj" />
|
||||||
<ProjectReference Include="..\..\Controls\LogViewer.Avalonia\LogViewer.Avalonia.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" />
|
<ProjectReference Include="..\..\Core\Serilog.Sinks.LogView.Core\Serilog.Sinks.LogView.Core.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ using System;
|
|||||||
|
|
||||||
namespace AvaloniaSerilogDI;
|
namespace AvaloniaSerilogDI;
|
||||||
|
|
||||||
internal class Program
|
internal sealed class Program
|
||||||
{
|
{
|
||||||
// Initialization code. Don't use any Avalonia, third-party APIs or any
|
// Initialization code. Don't use any Avalonia, third-party APIs or any
|
||||||
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
|
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
|
||||||
|
|||||||
@@ -2,18 +2,16 @@
|
|||||||
|
|
||||||
namespace AvaloniaSerilogDI.ViewModels;
|
namespace AvaloniaSerilogDI.ViewModels;
|
||||||
|
|
||||||
public class MainViewModel : ViewModelBase
|
public class MainViewModel(LogViewerControlViewModel logViewer) : ViewModelBase
|
||||||
{
|
{
|
||||||
#region Constructor
|
|
||||||
|
|
||||||
public MainViewModel(LogViewerControlViewModel logViewer)
|
#region Constructor
|
||||||
=> LogViewer = logViewer;
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
|
|
||||||
public LogViewerControlViewModel LogViewer { get; }
|
public LogViewerControlViewModel LogViewer { get; } = logViewer;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
@@ -10,8 +10,8 @@
|
|||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||||
|
|
||||||
Title="C# AVALONIA SeriLog | LogViewer Control Example - Dot Net 7.0"
|
Title="AvaloniaLoggingDI-Serilog"
|
||||||
Icon="/Assets/avalonia-logo.ico"
|
Icon="/Assets/avalonia-logo.ico"
|
||||||
WindowStartupLocation="CenterScreen" Height="634" Width="600">
|
WindowStartupLocation="CenterScreen" Height="634" Width="600">
|
||||||
|
|
||||||
<control:LogViewerControl DataContext="{Binding LogViewer}" />
|
<control:LogViewerControl DataContext="{Binding LogViewer}" />
|
||||||
|
|||||||
@@ -45,7 +45,6 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\Background Services\RandomLogging.Service\RandomLogging.Service.csproj" />
|
<ProjectReference Include="..\..\Background Services\RandomLogging.Service\RandomLogging.Service.csproj" />
|
||||||
<ProjectReference Include="..\..\Controls\LogViewer.Avalonia\LogViewer.Avalonia.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" />
|
<ProjectReference Include="..\..\Core\Serilog.Sinks.LogView.Core\Serilog.Sinks.LogView.Core.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</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 AvaloniaSerilogNoDI.DataStores;
|
||||||
using Common.Core.Extensions;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using Serilog.Sinks.LogView.Core;
|
using Serilog.Sinks.LogView.Core;
|
||||||
|
using System;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
namespace AvaloniaSerilogNoDI.Helpers;
|
namespace AvaloniaSerilogNoDI.Helpers;
|
||||||
|
|
||||||
|
|
||||||
// application-wide DataStoreLogger Factory ... returns a wired up Logger instance
|
// application-wide DataStoreLogger Factory ... returns a wired up Logger instance
|
||||||
public static class LoggingHelper
|
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()
|
static LoggingHelper()
|
||||||
{
|
{
|
||||||
IConfigurationRoot configuration = new ConfigurationBuilder()
|
IConfigurationRoot configuration = new ConfigurationBuilder()
|
||||||
|
|||||||
@@ -10,8 +10,8 @@
|
|||||||
|
|
||||||
xmlns:control="clr-namespace:LogViewer.Avalonia;assembly=LogViewer.Avalonia"
|
xmlns:control="clr-namespace:LogViewer.Avalonia;assembly=LogViewer.Avalonia"
|
||||||
|
|
||||||
Title="C# AVALONIA | SeriLog LogViewer Control Example - Dot Net 7.0"
|
Title="AvaloniaLoggingNoDI-SerilogLogger"
|
||||||
Icon="/Assets/avalonia-logo.ico"
|
Icon="/Assets/avalonia-logo.ico"
|
||||||
WindowStartupLocation="CenterScreen" Height="634" Width="600">
|
WindowStartupLocation="CenterScreen" Height="634" Width="600">
|
||||||
|
|
||||||
<control:LogViewerControl x:Name="LogViewerControl" />
|
<control:LogViewerControl x:Name="LogViewerControl" />
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ using System.Threading;
|
|||||||
|
|
||||||
namespace AvaloniaSerilogNoDI;
|
namespace AvaloniaSerilogNoDI;
|
||||||
|
|
||||||
public partial class MainWindow : Window, ILogDataStoreImpl
|
public partial class MainWindow : Window, ILogDataStoreCore
|
||||||
{
|
{
|
||||||
#region Constructors
|
#region Constructors
|
||||||
|
|
||||||
@@ -20,11 +20,11 @@ public partial class MainWindow : Window, ILogDataStoreImpl
|
|||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
|
||||||
// Initialize _service and pass in the Logger
|
// Initialize _service and pass in the Logger
|
||||||
_service = new(new Logger<RandomLoggingService>(LoggingHelper.Factory));
|
Service = new(new Logger<RandomLoggingService>(LoggingHelper.Factory));
|
||||||
|
|
||||||
// Get the Launch mode
|
// Get the Launch mode
|
||||||
bool isDevelopment = string.Equals(Environment.GetEnvironmentVariable("DOTNET_MODIFIABLE_ASSEMBLIES"), "debug",
|
bool isDevelopment = string.Equals(Environment.GetEnvironmentVariable("DOTNET_MODIFIABLE_ASSEMBLIES"), "debug",
|
||||||
StringComparison.InvariantCultureIgnoreCase);
|
StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
// initialize a logger & EventId
|
// initialize a logger & EventId
|
||||||
Logger<MainWindow> logger = new Logger<MainWindow>(LoggingHelper.Factory);
|
Logger<MainWindow> logger = new Logger<MainWindow>(LoggingHelper.Factory);
|
||||||
@@ -37,7 +37,7 @@ public partial class MainWindow : Window, ILogDataStoreImpl
|
|||||||
logger.Emit(eventId, LogLevel.Information, $"Running in {(isDevelopment ? "Debug" : "Release")} mode");
|
logger.Emit(eventId, LogLevel.Information, $"Running in {(isDevelopment ? "Debug" : "Release")} mode");
|
||||||
|
|
||||||
// Start generating log entries
|
// 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...
|
// manually wire up the logging to the view ... the control will show backlog entries...
|
||||||
DataStore = MainControlsDataStore.DataStore;
|
DataStore = MainControlsDataStore.DataStore;
|
||||||
@@ -52,12 +52,9 @@ public partial class MainWindow : Window, ILogDataStoreImpl
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Fields
|
|
||||||
|
private RandomLoggingService? Service { get; set;}
|
||||||
private readonly RandomLoggingService? _service;
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
|
|
||||||
public ILogDataStore DataStore { get; init; }
|
public ILogDataStore DataStore { get; init; }
|
||||||
@@ -71,7 +68,7 @@ public partial class MainWindow : Window, ILogDataStoreImpl
|
|||||||
{
|
{
|
||||||
Window.Closing -= OnClosing;
|
Window.Closing -= OnClosing;
|
||||||
|
|
||||||
_ = _service?.StopAsync();
|
_ = Service?.StopAsync();
|
||||||
LoggingHelper.CloseAndFlush();
|
LoggingHelper.CloseAndFlush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ using System;
|
|||||||
|
|
||||||
namespace AvaloniaSerilogNoDI;
|
namespace AvaloniaSerilogNoDI;
|
||||||
|
|
||||||
internal class Program
|
internal sealed class Program
|
||||||
{
|
{
|
||||||
// Initialization code. Don't use any Avalonia, third-party APIs or any
|
// Initialization code. Don't use any Avalonia, third-party APIs or any
|
||||||
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
|
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
|
||||||
|
|||||||
@@ -3,12 +3,10 @@ using Microsoft.Extensions.Logging;
|
|||||||
|
|
||||||
namespace RandomLogging.Service;
|
namespace RandomLogging.Service;
|
||||||
|
|
||||||
public class RandomLoggingService : BackgroundService
|
public class RandomLoggingService(ILogger<RandomLoggingService> logger) : BackgroundService
|
||||||
{
|
{
|
||||||
#region Constructors
|
|
||||||
|
|
||||||
public RandomLoggingService(ILogger<RandomLoggingService> logger)
|
#region Constructors
|
||||||
=> _logger = logger;
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -16,7 +14,7 @@ public class RandomLoggingService : BackgroundService
|
|||||||
|
|
||||||
#region Injected
|
#region Injected
|
||||||
|
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger = logger;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -164,7 +162,7 @@ public class RandomLoggingService : BackgroundService
|
|||||||
}
|
}
|
||||||
|
|
||||||
_logger.Emit(GenerateEventId(), level, GetMessage(),
|
_logger.Emit(GenerateEventId(), level, GetMessage(),
|
||||||
new Exception(_errorMessages[_random.Next(0, _errorMessages.Count)]));
|
new InvalidDataException(_errorMessages[_random.Next(0, _errorMessages.Count)]));
|
||||||
}
|
}
|
||||||
|
|
||||||
private EventId GenerateEventId()
|
private EventId GenerateEventId()
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ public partial class LogViewerControl : UserControl
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private ILogDataStoreImpl? vm;
|
private ILogDataStoreCore? vm;
|
||||||
private LogModel? item;
|
private LogModel? item;
|
||||||
|
|
||||||
private void OnDataContextChanged(object? sender, EventArgs e)
|
private void OnDataContextChanged(object? sender, EventArgs e)
|
||||||
@@ -21,7 +21,7 @@ public partial class LogViewerControl : UserControl
|
|||||||
if (DataContext is null)
|
if (DataContext is null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
vm = (ILogDataStoreImpl)DataContext;
|
vm = (ILogDataStoreCore)DataContext;
|
||||||
vm.DataStore.Entries.CollectionChanged += OnCollectionChanged;
|
vm.DataStore.Entries.CollectionChanged += OnCollectionChanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="10.0.5" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="10.0.5" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.5" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.5" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="10.0.5" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
||||||
@@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
namespace Common.Core.Extensions;
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -41,8 +41,9 @@ public static class LoggerExtensions
|
|||||||
|
|
||||||
public static void TestPattern(this ILogger logger, EventId eventId)
|
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");
|
logger.Emit(eventId, LogLevel.Trace, "Trace Test Pattern");
|
||||||
logger.Emit(eventId, LogLevel.Debug, "Debug Test Pattern");
|
logger.Emit(eventId, LogLevel.Debug, "Debug Test Pattern");
|
||||||
logger.Emit(eventId, LogLevel.Information, "Information Test Pattern");
|
logger.Emit(eventId, LogLevel.Information, "Information Test Pattern");
|
||||||
|
|||||||
@@ -8,8 +8,4 @@
|
|||||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.5" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.5" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\Mvvm.Core\Mvvm.Core.csproj" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
namespace LogViewer.Core;
|
|
||||||
|
|
||||||
public interface ILogDataStoreImpl
|
|
||||||
{
|
|
||||||
public ILogDataStore DataStore { get; }
|
|
||||||
}
|
|
||||||
@@ -1,21 +1,7 @@
|
|||||||
using Mvvm.Core;
|
namespace LogViewer.Core.ViewModels;
|
||||||
|
|
||||||
namespace LogViewer.Core.ViewModels;
|
public class LogViewerControlViewModel(ILogDataStore dataStore) : ILogDataStoreCore
|
||||||
|
|
||||||
public class LogViewerControlViewModel : ViewModel, ILogDataStoreImpl
|
|
||||||
{
|
{
|
||||||
#region Constructor
|
public ILogDataStore DataStore { get; set; } = dataStore;
|
||||||
|
|
||||||
public LogViewerControlViewModel(ILogDataStore dataStore)
|
|
||||||
{
|
|
||||||
DataStore = dataStore;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Properties
|
|
||||||
|
|
||||||
public ILogDataStore DataStore { get; set; }
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
||||||
@@ -12,9 +12,9 @@ public class DataStoreLoggerProvider: ILoggerProvider
|
|||||||
|
|
||||||
public DataStoreLoggerProvider(IOptionsMonitor<DataStoreLoggerConfiguration> config, ILogDataStore dataStore)
|
public DataStoreLoggerProvider(IOptionsMonitor<DataStoreLoggerConfiguration> config, ILogDataStore dataStore)
|
||||||
{
|
{
|
||||||
_dataStore = dataStore;
|
DataStore = dataStore;
|
||||||
_currentConfig = config.CurrentValue;
|
_currentConfig = config.CurrentValue;
|
||||||
_onChangeToken = config.OnChange(updatedConfig => _currentConfig = updatedConfig);
|
OnChangeToken = config.OnChange(updatedConfig => _currentConfig = updatedConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@@ -23,25 +23,26 @@ public class DataStoreLoggerProvider: ILoggerProvider
|
|||||||
|
|
||||||
private DataStoreLoggerConfiguration _currentConfig;
|
private DataStoreLoggerConfiguration _currentConfig;
|
||||||
|
|
||||||
private readonly IDisposable? _onChangeToken;
|
private IDisposable? OnChangeToken { get; }
|
||||||
protected readonly ILogDataStore _dataStore;
|
protected ILogDataStore DataStore { get; }
|
||||||
|
|
||||||
protected readonly ConcurrentDictionary<string, DataStoreLogger> _loggers = new();
|
protected ConcurrentDictionary<string, DataStoreLogger> Loggers { get; } = new();
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
|
|
||||||
public ILogger CreateLogger(string categoryName)
|
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()
|
protected DataStoreLoggerConfiguration GetCurrentConfig()
|
||||||
=> _currentConfig;
|
=> _currentConfig;
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
_loggers.Clear();
|
GC.SuppressFinalize(this);
|
||||||
_onChangeToken?.Dispose();
|
Loggers.Clear();
|
||||||
|
OnChangeToken?.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@@ -2,24 +2,18 @@
|
|||||||
using LogViewer.Core;
|
using LogViewer.Core;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Serilog.Core;
|
using Serilog.Core;
|
||||||
|
using System.Globalization;
|
||||||
|
|
||||||
namespace Serilog.Sinks.LogView.Core;
|
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;
|
protected Func<ILogDataStore> DataStoreProvider { get; } = dataStoreProvider;
|
||||||
|
private IFormatProvider? FormatProvider { get; } = formatProvider;
|
||||||
private readonly IFormatProvider? _formatProvider;
|
private Func<DataStoreLoggerConfiguration>? GetCurrentConfig { get; } = getCurrentConfig;
|
||||||
private readonly Func<DataStoreLoggerConfiguration>? _getCurrentConfig;
|
|
||||||
|
|
||||||
public DataStoreLoggerSink(Func<ILogDataStore> dataStoreProvider,
|
|
||||||
Func<DataStoreLoggerConfiguration>? getCurrentConfig = null,
|
|
||||||
IFormatProvider? formatProvider = null)
|
|
||||||
{
|
|
||||||
_formatProvider = formatProvider;
|
|
||||||
_dataStoreProvider = dataStoreProvider;
|
|
||||||
_getCurrentConfig = getCurrentConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Emit(LogEvent logEvent)
|
public void Emit(LogEvent logEvent)
|
||||||
{
|
{
|
||||||
@@ -33,13 +27,13 @@ public class DataStoreLoggerSink : ILogEventSink
|
|||||||
_ => LogLevel.Information
|
_ => LogLevel.Information
|
||||||
};
|
};
|
||||||
|
|
||||||
DataStoreLoggerConfiguration config = _getCurrentConfig?.Invoke() ?? new DataStoreLoggerConfiguration();
|
DataStoreLoggerConfiguration config = GetCurrentConfig?.Invoke() ?? new DataStoreLoggerConfiguration();
|
||||||
|
|
||||||
EventId eventId = EventIdFactory(logEvent);
|
EventId eventId = EventIdFactory(logEvent);
|
||||||
if (eventId.Id == 0 && config.EventId != 0)
|
if (eventId.Id == 0 && config.EventId != 0)
|
||||||
eventId = config.EventId;
|
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);
|
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)
|
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
|
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
|
||||||
if (dataStore == null)
|
if (dataStore == null)
|
||||||
@@ -79,11 +73,11 @@ public class DataStoreLoggerSink : ILogEventSink
|
|||||||
// ref: https://stackoverflow.com/a/56722516
|
// ref: https://stackoverflow.com/a/56722516
|
||||||
StructureValue? value = src as StructureValue;
|
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)
|
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)
|
if (nameProperty is not null)
|
||||||
eventName = nameProperty.Value.ToString().Trim('"');
|
eventName = nameProperty.Value.ToString().Trim('"');
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
<Project>
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
|
<AppendTargetFrameworkToOutputPath>true</AppendTargetFrameworkToOutputPath>
|
||||||
|
<LangVersion>latest</LangVersion>
|
||||||
|
<AnalysisLevel>latest-recommended</AnalysisLevel>
|
||||||
|
<BinDir>$(MSBuildThisFileDirectory)bin</BinDir>
|
||||||
|
<RestorePackagesPath>$(MSBuildThisFileDirectory)packages</RestorePackagesPath>
|
||||||
|
<OutputDirectory>$(MSBuildProjectName)</OutputDirectory>
|
||||||
|
<OutputDirectory Condition=" '$(AssemblyName)' != '' ">$(AssemblyName)</OutputDirectory>
|
||||||
|
<OutputPath>$(BinDir)\$(Configuration)\$(MSBuildProjectName)\</OutputPath>
|
||||||
|
<BaseIntermediateOutputPath>$(BinDir)\obj\$(MSBuildProjectName)\</BaseIntermediateOutputPath>
|
||||||
|
<EnableUnsafeBinaryFormatterSerialization>false</EnableUnsafeBinaryFormatterSerialization>
|
||||||
|
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Remove="*.DotSettings" />
|
||||||
|
<None Remove="packages.lock.json" />
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
||||||
@@ -5,8 +5,6 @@ VisualStudioVersion = 18.4.11626.88
|
|||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Core", "Core", "{A3BEB004-4DF7-4281-9A08-8A7BCD4E3CC9}"
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Core", "Core", "{A3BEB004-4DF7-4281-9A08-8A7BCD4E3CC9}"
|
||||||
EndProject
|
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}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LogViewer.Core", "CSharp\Core\LogViewer.Core\LogViewer.Core.csproj", "{34F75D8B-6F15-4DE4-8335-FED83557EB8E}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Apps", "Apps", "{42E99803-0A95-4172-9079-3B8BF8CBDE9F}"
|
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
|
EndProject
|
||||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Background Services", "Background Services", "{0CDEA51D-46FE-4767-BA2E-8F14582A926D}"
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Background Services", "Background Services", "{0CDEA51D-46FE-4767-BA2E-8F14582A926D}"
|
||||||
EndProject
|
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}"
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{006FDAED-6319-4976-B8BA-8D94E4574139}"
|
||||||
ProjectSection(SolutionItems) = preProject
|
ProjectSection(SolutionItems) = preProject
|
||||||
LICENSE = LICENSE
|
LICENSE = LICENSE
|
||||||
@@ -51,18 +47,10 @@ Global
|
|||||||
Release|Any CPU = Release|Any CPU
|
Release|Any CPU = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
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.ActiveCfg = Debug|Any CPU
|
||||||
{34F75D8B-6F15-4DE4-8335-FED83557EB8E}.Debug|Any CPU.Build.0 = 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.ActiveCfg = Release|Any CPU
|
||||||
{34F75D8B-6F15-4DE4-8335-FED83557EB8E}.Release|Any CPU.Build.0 = 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.ActiveCfg = Debug|Any CPU
|
||||||
{18BA2294-FE64-481F-A86F-F5FD84438B66}.Debug|Any CPU.Build.0 = 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
|
{18BA2294-FE64-481F-A86F-F5FD84438B66}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
@@ -100,9 +88,7 @@ Global
|
|||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(NestedProjects) = preSolution
|
GlobalSection(NestedProjects) = preSolution
|
||||||
{BB614345-449F-46AD-BE8C-5E2B7616EDE2} = {A3BEB004-4DF7-4281-9A08-8A7BCD4E3CC9}
|
|
||||||
{34F75D8B-6F15-4DE4-8335-FED83557EB8E} = {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}
|
{18BA2294-FE64-481F-A86F-F5FD84438B66} = {0CDEA51D-46FE-4767-BA2E-8F14582A926D}
|
||||||
{0EDAAABD-495D-43A4-BDFB-A0506CAAC07E} = {23CB559B-2361-4ED6-8A26-D1B1C2005D65}
|
{0EDAAABD-495D-43A4-BDFB-A0506CAAC07E} = {23CB559B-2361-4ED6-8A26-D1B1C2005D65}
|
||||||
{23CB559B-2361-4ED6-8A26-D1B1C2005D65} = {A3BEB004-4DF7-4281-9A08-8A7BCD4E3CC9}
|
{23CB559B-2361-4ED6-8A26-D1B1C2005D65} = {A3BEB004-4DF7-4281-9A08-8A7BCD4E3CC9}
|
||||||
Reference in New Issue
Block a user