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
@@ -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<MainViewModel>()
.AddSingleton<MainWindow>(service => new MainWindow
.AddSingleton(service => new MainWindow
{
DataContext = service.GetRequiredService<MainViewModel>()
});
_host = builder.Build();
_cancellationTokenSource = new();
Host = builder.Build();
CancellationTokenSource = new();
try
{
LogStartingMode();
// set and show
desktop.MainWindow = _host.Services.GetRequiredService<MainWindow>();
desktop.MainWindow = Host.Services.GetRequiredService<MainWindow>();
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<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);
// log a test pattern for each log level
@@ -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
@@ -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">
@@ -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<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
Factory = LoggerFactory.Create(builder => builder
@@ -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
@@ -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">
<control:LogViewerControl x:Name="LogViewerControl" />
@@ -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<MainWindow> logger = new Logger<MainWindow>(LoggingHelper.Factory);
@@ -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<ILogDataStore>()!,formatProvider: CultureInfo.InvariantCulture)
.WriteTo.DataStoreLoggerSink( dataStoreProvider: () => Host!.Services.TryGetService<ILogDataStore>()!,formatProvider: CultureInfo.InvariantCulture)
.CreateLogger();
cfg.ClearProviders().AddSerilog(Log.Logger);
@@ -90,19 +88,19 @@ public partial class App : Application
DataContext = service.GetRequiredService<MainViewModel>()
});
_host = builder.Build();
_cancellationTokenSource = new();
Host = builder.Build();
CancellationTokenSource = new();
try
{
LogStartingMode();
// set and show
desktop.MainWindow = _host.Services.GetRequiredService<MainWindow>();
desktop.MainWindow = Host.Services.GetRequiredService<MainWindow>();
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<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);
// 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();
@@ -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
@@ -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">
<control:LogViewerControl DataContext="{Binding LogViewer}" />
@@ -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">
<control:LogViewerControl x:Name="LogViewerControl" />
@@ -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<RandomLoggingService>(LoggingHelper.Factory));
Service = new(new Logger<RandomLoggingService>(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<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");
// 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();
}
@@ -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