From 03621a86646597991866edb565b1978c8d544699 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sun, 12 Nov 2017 15:02:07 +0100 Subject: [PATCH 1/4] Added AppBuilder methods for logging. You can now set up serilog logging to `Debug` or `Trace` by calling e.g.: ``` AppBuilder.Configure - - - - diff --git a/build/Serilog.props b/build/Serilog.props index e9bd39f6af..a814cf998d 100644 --- a/build/Serilog.props +++ b/build/Serilog.props @@ -1,5 +1,7 @@  - + + + diff --git a/samples/BindingTest/App.xaml.cs b/samples/BindingTest/App.xaml.cs index 42e5716541..ccad1d0ba9 100644 --- a/samples/BindingTest/App.xaml.cs +++ b/samples/BindingTest/App.xaml.cs @@ -16,22 +16,11 @@ namespace BindingTest private static void Main() { - InitializeLogging(); - AppBuilder.Configure() .UsePlatformDetect() .UseReactiveUI() + .LogToDebug() .Start(); } - - private static void InitializeLogging() - { -#if DEBUG - SerilogLogger.Initialize(new LoggerConfiguration() - .MinimumLevel.Warning() - .WriteTo.Trace(outputTemplate: "{Area}: {Message}") - .CreateLogger()); -#endif - } } } diff --git a/samples/ControlCatalog.Desktop/Program.cs b/samples/ControlCatalog.Desktop/Program.cs index 20bf4ed342..b151cabf43 100644 --- a/samples/ControlCatalog.Desktop/Program.cs +++ b/samples/ControlCatalog.Desktop/Program.cs @@ -12,8 +12,6 @@ namespace ControlCatalog { static void Main(string[] args) { - InitializeLogging(); - // TODO: Make this work with GTK/Skia/Cairo depending on command-line args // again. BuildAvaloniaApp().Start(); @@ -23,18 +21,7 @@ namespace ControlCatalog /// This method is needed for IDE previewer infrastructure /// public static AppBuilder BuildAvaloniaApp() - => AppBuilder.Configure().UsePlatformDetect(); - - // This will be made into a runtime configuration extension soon! - private static void InitializeLogging() - { -#if DEBUG - SerilogLogger.Initialize(new LoggerConfiguration() - .MinimumLevel.Warning() - .WriteTo.Trace(outputTemplate: "{Area}: {Message}") - .CreateLogger()); -#endif - } + => AppBuilder.Configure().LogToDebug().UsePlatformDetect(); private static void ConfigureAssetAssembly(AppBuilder builder) { diff --git a/samples/RenderTest/Program.cs b/samples/RenderTest/Program.cs index 514771097f..a1a2b8c410 100644 --- a/samples/RenderTest/Program.cs +++ b/samples/RenderTest/Program.cs @@ -12,25 +12,13 @@ namespace RenderTest { static void Main(string[] args) { - InitializeLogging(); - // TODO: Make this work with GTK/Skia/Cairo depending on command-line args // again. AppBuilder.Configure() .UsePlatformDetect() .UseReactiveUI() + .LogToDebug() .Start(); } - - // This will be made into a runtime configuration extension soon! - private static void InitializeLogging() - { -#if DEBUG - SerilogLogger.Initialize(new LoggerConfiguration() - .MinimumLevel.Warning() - .WriteTo.Trace(outputTemplate: "{Area}: {Message}") - .CreateLogger()); -#endif - } } } diff --git a/samples/VirtualizationTest/Program.cs b/samples/VirtualizationTest/Program.cs index e9156d803f..097f0cfdc7 100644 --- a/samples/VirtualizationTest/Program.cs +++ b/samples/VirtualizationTest/Program.cs @@ -13,22 +13,11 @@ namespace VirtualizationTest { static void Main(string[] args) { - InitializeLogging(); - AppBuilder.Configure() .UsePlatformDetect() .UseReactiveUI() + .LogToDebug() .Start(); } - - private static void InitializeLogging() - { -#if DEBUG - SerilogLogger.Initialize(new LoggerConfiguration() - .MinimumLevel.Warning() - .WriteTo.Trace(outputTemplate: "{Area}: {Message}") - .CreateLogger()); -#endif - } } } diff --git a/src/Avalonia.Logging.Serilog/Avalonia.Logging.Serilog.csproj b/src/Avalonia.Logging.Serilog/Avalonia.Logging.Serilog.csproj index b9fa2f208b..5aa2cd67ba 100644 --- a/src/Avalonia.Logging.Serilog/Avalonia.Logging.Serilog.csproj +++ b/src/Avalonia.Logging.Serilog/Avalonia.Logging.Serilog.csproj @@ -28,6 +28,7 @@ + \ No newline at end of file diff --git a/src/Avalonia.Logging.Serilog/SerilogExtensions.cs b/src/Avalonia.Logging.Serilog/SerilogExtensions.cs new file mode 100644 index 0000000000..6abc7e4684 --- /dev/null +++ b/src/Avalonia.Logging.Serilog/SerilogExtensions.cs @@ -0,0 +1,51 @@ +using System; +using Avalonia.Controls; +using Serilog; +using SerilogLevel = Serilog.Events.LogEventLevel; + +namespace Avalonia.Logging.Serilog +{ + /// + /// Extension methods for Serilog logging. + /// + public static class SerilogExtensions + { + /// + /// Logs Avalonia events to the sink. + /// + /// The application class type. + /// The app builder instance. + /// The minimum level to log. + /// The app builder instance. + public static T LogToDebug( + this T builder, + LogEventLevel level = LogEventLevel.Warning) + where T : AppBuilderBase, new() + { + SerilogLogger.Initialize(new LoggerConfiguration() + .MinimumLevel.Is((SerilogLevel)level) + .WriteTo.Debug(outputTemplate: "{Area}: {Message}") + .CreateLogger()); + return builder; + } + + /// + /// Logs Avalonia events to the sink. + /// + /// The application class type. + /// The app builder instance. + /// The minimum level to log. + /// The app builder instance. + public static T LogToTrace( + this T builder, + LogEventLevel level = LogEventLevel.Warning) + where T : AppBuilderBase, new() + { + SerilogLogger.Initialize(new LoggerConfiguration() + .MinimumLevel.Is((SerilogLevel)level) + .WriteTo.Trace(outputTemplate: "{Area}: {Message}") + .CreateLogger()); + return builder; + } + } +} From c02514f162f034faffcb2c857f2c4003895a0b5b Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sun, 12 Nov 2017 15:41:50 +0100 Subject: [PATCH 2/4] Removed imports of deleted .props file. --- samples/BindingTest/BindingTest.csproj | 1 - samples/ControlCatalog.Desktop/ControlCatalog.Desktop.csproj | 1 - samples/RenderTest/RenderTest.csproj | 1 - samples/VirtualizationTest/VirtualizationTest.csproj | 1 - .../interop/Direct3DInteropSample/Direct3DInteropSample.csproj | 1 - 5 files changed, 5 deletions(-) diff --git a/samples/BindingTest/BindingTest.csproj b/samples/BindingTest/BindingTest.csproj index 4f82303c3a..9f3fed5522 100644 --- a/samples/BindingTest/BindingTest.csproj +++ b/samples/BindingTest/BindingTest.csproj @@ -151,7 +151,6 @@ - \ No newline at end of file diff --git a/samples/ControlCatalog.Desktop/ControlCatalog.Desktop.csproj b/samples/ControlCatalog.Desktop/ControlCatalog.Desktop.csproj index 3f4679086e..cfa2f89b0e 100644 --- a/samples/ControlCatalog.Desktop/ControlCatalog.Desktop.csproj +++ b/samples/ControlCatalog.Desktop/ControlCatalog.Desktop.csproj @@ -135,5 +135,4 @@ - \ No newline at end of file diff --git a/samples/RenderTest/RenderTest.csproj b/samples/RenderTest/RenderTest.csproj index a71f17ab7a..4a471a831a 100644 --- a/samples/RenderTest/RenderTest.csproj +++ b/samples/RenderTest/RenderTest.csproj @@ -180,7 +180,6 @@ - \ No newline at end of file diff --git a/samples/VirtualizationTest/VirtualizationTest.csproj b/samples/VirtualizationTest/VirtualizationTest.csproj index 6d3803b4cf..147355aed1 100644 --- a/samples/VirtualizationTest/VirtualizationTest.csproj +++ b/samples/VirtualizationTest/VirtualizationTest.csproj @@ -147,7 +147,6 @@ - \ No newline at end of file diff --git a/samples/interop/Direct3DInteropSample/Direct3DInteropSample.csproj b/samples/interop/Direct3DInteropSample/Direct3DInteropSample.csproj index e0fc79faea..4271d05f91 100644 --- a/samples/interop/Direct3DInteropSample/Direct3DInteropSample.csproj +++ b/samples/interop/Direct3DInteropSample/Direct3DInteropSample.csproj @@ -28,6 +28,5 @@ - \ No newline at end of file From 5119828b8d184b7522f11419f4319fcc92c7a0e8 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sun, 12 Nov 2017 15:52:45 +0100 Subject: [PATCH 3/4] Add serilog sink dependencies to build script. --- packages.cake | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages.cake b/packages.cake index 9e78ee75d5..35738b6863 100644 --- a/packages.cake +++ b/packages.cake @@ -243,6 +243,8 @@ public class Packages Dependencies = new DependencyBuilder(this) { new NuSpecDependency() { Id = "Serilog", Version = SerilogVersion }, + new NuSpecDependency() { Id = "Serilog.Sinks.Debug", Version = "1.0.0" }, + new NuSpecDependency() { Id = "Serilog.Sinks.Trace", Version = "2.1.0" }, new NuSpecDependency() { Id = "Sprache", Version = SpracheVersion }, new NuSpecDependency() { Id = "System.Reactive", Version = SystemReactiveVersion }, new NuSpecDependency() { Id = "Avalonia.Remote.Protocol", Version = parameters.Version }, @@ -251,6 +253,8 @@ public class Packages new NuSpecDependency() { Id = "Microsoft.Extensions.DependencyModel", TargetFramework = "netcoreapp2.0", Version = "1.1.0" }, new NuSpecDependency() { Id = "NETStandard.Library", TargetFramework = "netcoreapp2.0", Version = "1.6.0" }, new NuSpecDependency() { Id = "Serilog", TargetFramework = "netcoreapp2.0", Version = SerilogVersion }, + new NuSpecDependency() { Id = "Serilog.Sinks.Debug", TargetFramework = "netcoreapp2.0", VersionVersion = "1.0.0" }, + new NuSpecDependency() { Id = "Serilog.Sinks.Trace", TargetFramework = "netcoreapp2.0", VersionVersion = "2.1.0" }, new NuSpecDependency() { Id = "Sprache", TargetFramework = "netcoreapp2.0", Version = SpracheVersion }, new NuSpecDependency() { Id = "System.Reactive", TargetFramework = "netcoreapp2.0", Version = SystemReactiveVersion }, new NuSpecDependency() { Id = "Avalonia.Remote.Protocol", TargetFramework = "netcoreapp2.0", Version = parameters.Version }, From f6830df1026aa7ad5100ae47041bd13f5048608a Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sun, 12 Nov 2017 15:59:59 +0100 Subject: [PATCH 4/4] Get serilog versions automatically. --- packages.cake | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages.cake b/packages.cake index 35738b6863..bc290fce22 100644 --- a/packages.cake +++ b/packages.cake @@ -107,6 +107,8 @@ public class Packages context.Information("Setting NuGet package dependencies versions:"); var SerilogVersion = packageVersions["Serilog"].FirstOrDefault().Item1; + var SerilogSinksDebugVersion = packageVersions["Serilog.Sinks.Debug"].FirstOrDefault().Item1; + var SerilogSinksTraceVersion = packageVersions["Serilog.Sinks.Trace"].FirstOrDefault().Item1; var SpracheVersion = packageVersions["Sprache"].FirstOrDefault().Item1; var SystemReactiveVersion = packageVersions["System.Reactive"].FirstOrDefault().Item1; var ReactiveUIVersion = packageVersions["reactiveui"].FirstOrDefault().Item1; @@ -243,8 +245,8 @@ public class Packages Dependencies = new DependencyBuilder(this) { new NuSpecDependency() { Id = "Serilog", Version = SerilogVersion }, - new NuSpecDependency() { Id = "Serilog.Sinks.Debug", Version = "1.0.0" }, - new NuSpecDependency() { Id = "Serilog.Sinks.Trace", Version = "2.1.0" }, + new NuSpecDependency() { Id = "Serilog.Sinks.Debug", Version = SerilogSinksDebugVersion }, + new NuSpecDependency() { Id = "Serilog.Sinks.Trace", Version = SerilogSinksTraceVersion }, new NuSpecDependency() { Id = "Sprache", Version = SpracheVersion }, new NuSpecDependency() { Id = "System.Reactive", Version = SystemReactiveVersion }, new NuSpecDependency() { Id = "Avalonia.Remote.Protocol", Version = parameters.Version }, @@ -253,8 +255,8 @@ public class Packages new NuSpecDependency() { Id = "Microsoft.Extensions.DependencyModel", TargetFramework = "netcoreapp2.0", Version = "1.1.0" }, new NuSpecDependency() { Id = "NETStandard.Library", TargetFramework = "netcoreapp2.0", Version = "1.6.0" }, new NuSpecDependency() { Id = "Serilog", TargetFramework = "netcoreapp2.0", Version = SerilogVersion }, - new NuSpecDependency() { Id = "Serilog.Sinks.Debug", TargetFramework = "netcoreapp2.0", VersionVersion = "1.0.0" }, - new NuSpecDependency() { Id = "Serilog.Sinks.Trace", TargetFramework = "netcoreapp2.0", VersionVersion = "2.1.0" }, + new NuSpecDependency() { Id = "Serilog.Sinks.Debug", TargetFramework = "netcoreapp2.0", Version = SerilogSinksDebugVersion }, + new NuSpecDependency() { Id = "Serilog.Sinks.Trace", TargetFramework = "netcoreapp2.0", Version = SerilogSinksTraceVersion }, new NuSpecDependency() { Id = "Sprache", TargetFramework = "netcoreapp2.0", Version = SpracheVersion }, new NuSpecDependency() { Id = "System.Reactive", TargetFramework = "netcoreapp2.0", Version = SystemReactiveVersion }, new NuSpecDependency() { Id = "Avalonia.Remote.Protocol", TargetFramework = "netcoreapp2.0", Version = parameters.Version },