Ver Fonte

Merge branch 'fixes/transitions-disposal' of github.com:AvaloniaUI/Avalonia into fixes/transitions-disposal

Jumar Macato há 6 anos atrás
pai
commit
e3fb01d107

+ 56 - 3
src/Avalonia.Logging.Serilog/SerilogExtensions.cs

@@ -1,5 +1,8 @@
-using Avalonia.Controls;
+using System;
+using Avalonia.Controls;
 using Serilog;
+using Serilog.Configuration;
+using Serilog.Filters;
 using SerilogLevel = Serilog.Events.LogEventLevel;
 
 namespace Avalonia.Logging.Serilog
@@ -9,21 +12,67 @@ namespace Avalonia.Logging.Serilog
     /// </summary>
     public static class SerilogExtensions
     {
+        private const string DefaultTemplate = "[{Area}] {Message} ({SourceType} #{SourceHash})";
+
+        /// <summary>
+        /// Logs Avalonia events to the <see cref="System.Diagnostics.Debug"/> sink.
+        /// </summary>
+        /// <typeparam name="T">The application class type.</typeparam>
+        /// <param name="builder">The app builder instance.</param>
+        /// <param name="level">The minimum level to log.</param>
+        /// <returns>The app builder instance.</returns>
+        public static T LogToDebug<T>(
+            this T builder,
+            LogEventLevel level = LogEventLevel.Warning)
+                where T : AppBuilderBase<T>, new()
+        {
+            SerilogLogger.Initialize(new LoggerConfiguration()
+                .MinimumLevel.Is((SerilogLevel)level)
+                .Enrich.FromLogContext()
+                .WriteTo.Debug(outputTemplate: DefaultTemplate)
+                .CreateLogger());
+            return builder;
+        }
+
         /// <summary>
         /// Logs Avalonia events to the <see cref="System.Diagnostics.Debug"/> sink.
         /// </summary>
         /// <typeparam name="T">The application class type.</typeparam>
         /// <param name="builder">The app builder instance.</param>
+        /// <param name="area">The area to log. Valid values are listed in <see cref="LogArea"/>.</param>
         /// <param name="level">The minimum level to log.</param>
         /// <returns>The app builder instance.</returns>
         public static T LogToDebug<T>(
+            this T builder,
+            string area,
+            LogEventLevel level = LogEventLevel.Warning)
+                where T : AppBuilderBase<T>, new()
+        {
+            SerilogLogger.Initialize(new LoggerConfiguration()
+                .MinimumLevel.Is((SerilogLevel)level)
+                .Filter.ByIncludingOnly(Matching.WithProperty("Area", area))
+                .Enrich.FromLogContext()
+                .WriteTo.Debug(outputTemplate: DefaultTemplate)
+                .CreateLogger());
+            return builder;
+        }
+
+        /// <summary>
+        /// Logs Avalonia events to the <see cref="System.Diagnostics.Trace"/> sink.
+        /// </summary>
+        /// <typeparam name="T">The application class type.</typeparam>
+        /// <param name="builder">The app builder instance.</param>
+        /// <param name="level">The minimum level to log.</param>
+        /// <returns>The app builder instance.</returns>
+        public static T LogToTrace<T>(
             this T builder,
             LogEventLevel level = LogEventLevel.Warning)
                 where T : AppBuilderBase<T>, new()
         {
             SerilogLogger.Initialize(new LoggerConfiguration()
                 .MinimumLevel.Is((SerilogLevel)level)
-                .WriteTo.Debug(outputTemplate: "{Area}: {Message}")
+                .Enrich.FromLogContext()
+                .WriteTo.Trace(outputTemplate: DefaultTemplate)
                 .CreateLogger());
             return builder;
         }
@@ -33,16 +82,20 @@ namespace Avalonia.Logging.Serilog
         /// </summary>
         /// <typeparam name="T">The application class type.</typeparam>
         /// <param name="builder">The app builder instance.</param>
+        /// <param name="area">The area to log. Valid values are listed in <see cref="LogArea"/>.</param>
         /// <param name="level">The minimum level to log.</param>
         /// <returns>The app builder instance.</returns>
         public static T LogToTrace<T>(
             this T builder,
+            string area,
             LogEventLevel level = LogEventLevel.Warning)
                 where T : AppBuilderBase<T>, new()
         {
             SerilogLogger.Initialize(new LoggerConfiguration()
                 .MinimumLevel.Is((SerilogLevel)level)
-                .WriteTo.Trace(outputTemplate: "{Area}: {Message}")
+                .Filter.ByIncludingOnly(Matching.WithProperty("Area", area))
+                .Enrich.FromLogContext()
+                .WriteTo.Trace(outputTemplate: DefaultTemplate)
                 .CreateLogger());
             return builder;
         }

+ 12 - 12
src/Avalonia.Logging.Serilog/SerilogLogger.cs

@@ -1,8 +1,9 @@
 // Copyright (c) The Avalonia Project. All rights reserved.
 // Licensed under the MIT license. See licence.md file in the project root for full license information.
 
-using System.Collections.Generic;
+using System;
 using Serilog;
+using Serilog.Context;
 using AvaloniaLogEventLevel = Avalonia.Logging.LogEventLevel;
 using SerilogLogEventLevel = Serilog.Events.LogEventLevel;
 
@@ -14,7 +15,6 @@ namespace Avalonia.Logging.Serilog
     public class SerilogLogger : ILogSink
     {
         private readonly ILogger _output;
-        private readonly Dictionary<string, ILogger> _areas = new Dictionary<string, ILogger>();
 
         /// <summary>
         /// Initializes a new instance of the <see cref="SerilogLogger"/> class.
@@ -36,21 +36,21 @@ namespace Avalonia.Logging.Serilog
 
         /// <inheritdoc/>
         public void Log(
-            AvaloniaLogEventLevel level, 
-            string area, 
-            object source, 
-            string messageTemplate, 
+            AvaloniaLogEventLevel level,
+            string area,
+            object source,
+            string messageTemplate,
             params object[] propertyValues)
         {
-            ILogger areaLogger;
+            Contract.Requires<ArgumentNullException>(area != null);
+            Contract.Requires<ArgumentNullException>(messageTemplate != null);
 
-            if (!_areas.TryGetValue(area, out areaLogger))
+            using (LogContext.PushProperty("Area", area))
+            using (LogContext.PushProperty("SourceType", source?.GetType()))
+            using (LogContext.PushProperty("SourceHash", source?.GetHashCode()))
             {
-                areaLogger = _output.ForContext("Area", area);
-                _areas.Add(area, areaLogger);
+                _output.Write((SerilogLogEventLevel)level, messageTemplate, propertyValues);
             }
-
-            areaLogger.Write((SerilogLogEventLevel)level, messageTemplate, propertyValues);
         }
     }
 }