Browse Source
1. Fixed some tests to expect `BindingNotification`s to be returned on a broken binding chain. 2. Changed logging of `BindingNotification`s - log at `Warning` level (instead of `Error`) except when the binding chain is broken at the root, in which case log at `Information` level. Do this to prevent flooding the output window when initializing.pull/894/head
10 changed files with 97 additions and 56 deletions
@ -0,0 +1,53 @@ |
|||
using System; |
|||
using Avalonia.Data; |
|||
|
|||
namespace Avalonia.Logging |
|||
{ |
|||
internal static class LoggerExtensions |
|||
{ |
|||
public static void LogIfError( |
|||
this BindingNotification notification, |
|||
object source, |
|||
AvaloniaProperty property) |
|||
{ |
|||
if (notification.ErrorType == BindingErrorType.Error) |
|||
{ |
|||
if (notification.Error is AggregateException aggregate) |
|||
{ |
|||
foreach (var inner in aggregate.InnerExceptions) |
|||
{ |
|||
LogError(source, property, inner); |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
LogError(source, property, notification.Error); |
|||
} |
|||
} |
|||
} |
|||
|
|||
private static void LogError(object source, AvaloniaProperty property, Exception e) |
|||
{ |
|||
var level = LogEventLevel.Warning; |
|||
|
|||
if (e is BindingChainException b && |
|||
!string.IsNullOrEmpty(b.Expression) && |
|||
string.IsNullOrEmpty(b.ExpressionErrorPoint)) |
|||
{ |
|||
// The error occurred at the root of the binding chain: it's possible that the
|
|||
// DataContext isn't set up yet, so log at Information level instead of Warning
|
|||
// to prevent spewing hundreds of errors.
|
|||
level = LogEventLevel.Information; |
|||
} |
|||
|
|||
Logger.Log( |
|||
level, |
|||
LogArea.Binding, |
|||
source, |
|||
"Error in binding to {Target}.{Property}: {Message}", |
|||
source, |
|||
property, |
|||
e.Message); |
|||
} |
|||
} |
|||
} |
|||
@ -1,23 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Avalonia.Utilities |
|||
{ |
|||
internal static class ExceptionUtilities |
|||
{ |
|||
public static string GetMessage(Exception e) |
|||
{ |
|||
var aggregate = e as AggregateException; |
|||
|
|||
if (aggregate != null) |
|||
{ |
|||
return string.Join(" | ", aggregate.InnerExceptions.Select(x => x.Message)); |
|||
} |
|||
|
|||
return e.Message; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue