Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug view for the state manager #19110

Merged
merged 1 commit into from
Dec 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions src/EFCore.Cosmos/Diagnostics/Internal/CosmosLoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,7 @@ private static void FormatParameterValue(StringBuilder builder, object parameter
builder.Append(dateTimeOffsetValue.ToString("o"));
break;
case byte[] binaryValue:
builder.Append("0x");

for (var i = 0; i < binaryValue.Length; i++)
{
if (i > 31)
{
builder.Append("...");
break;
}

builder.Append(binaryValue[i].ToString("X2", CultureInfo.InvariantCulture));
}

builder.AppendBytes(binaryValue);
break;
default:
builder.Append(Convert.ToString(parameterValue, CultureInfo.InvariantCulture));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,7 @@ private static void FormatParameterValue(StringBuilder builder, object parameter
}
else if (parameterValue.GetType() == typeof(byte[]))
{
var buffer = (byte[])parameterValue;
builder.Append("'0x");

for (var i = 0; i < buffer.Length; i++)
{
if (i > 31)
{
builder.Append("...");
break;
}

builder.Append(buffer[i].ToString("X2", CultureInfo.InvariantCulture));
}

builder.Append('\'');
builder.AppendBytes((byte[])parameterValue);
}
else
{
Expand Down
13 changes: 13 additions & 0 deletions src/EFCore/ChangeTracking/ChangeTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Utilities;

Expand Down Expand Up @@ -396,6 +397,18 @@ Task IResettableService.ResetStateAsync(CancellationToken cancellationToken)
return default;
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
[EntityFrameworkInternal]
public virtual DebugView DebugView
=> new DebugView(
() => StateManager.ToDebugString(StateManagerDebugStringOptions.ShortDefault),
() => StateManager.ToDebugString(StateManagerDebugStringOptions.LongDefault));

#region Hidden System.Object members

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;

namespace Microsoft.EntityFrameworkCore.ChangeTracking.Internal
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
[Flags]
public enum StateManagerDebugStringOptions
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
IncludeProperties = 1,

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
IncludeNavigations = 2,

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
ShortDefault = 0,

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
LongDefault = IncludeProperties | IncludeNavigations,
}
}
205 changes: 205 additions & 0 deletions src/EFCore/ChangeTracking/Internal/StateManagerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Update;

namespace Microsoft.EntityFrameworkCore.ChangeTracking.Internal
{
Expand Down Expand Up @@ -47,5 +54,203 @@ public static IReadOnlyList<InternalEntityEntry> ToListForState(
public static IReadOnlyList<InternalEntityEntry> ToList(
[NotNull] this IStateManager stateManager)
=> stateManager.ToListForState(added: true, modified: true, deleted: true, unchanged: true);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static string ToDebugString(
[NotNull] this IStateManager stateManager,
StateManagerDebugStringOptions options,
[NotNull] string indent = "")
{
var builder = new StringBuilder();

void AppendValue(object value)
{
if (value == null)
{
builder.Append("<null>");
}
else if (value.GetType().IsNumeric())
{
builder.Append(value);
}
else if (value is byte[] bytes)
{
builder.AppendBytes(bytes);
}
else
{
var stringValue = value.ToString();
if (stringValue.Length > 63)
{
stringValue = stringValue.Substring(0, 60) + "...";
}

builder
.Append('\'')
.Append(stringValue)
.Append('\'');
}
}

void AppendRelatedKey(IEntityType targetType, object value)
{
var otherEntry = stateManager.TryGetEntry(value, targetType, throwOnTypeMismatch: false);

builder.Append(
otherEntry == null
? "<not found>"
: otherEntry.BuildCurrentValuesString(targetType.FindPrimaryKey().Properties));
}

foreach (var item in stateManager.Entries
.Select(
e =>
new
{
KeyString = e.BuildCurrentValuesString(e.EntityType.FindPrimaryKey().Properties),
Entry = e
})
.OrderBy(e => e.Entry.EntityType.DisplayName())
.ThenBy(e => e.KeyString))
{
var entry = item.Entry;
var entityType = entry.EntityType;

builder
.Append(entityType.DisplayName())
.Append(' ')
.Append(item.KeyString)
.Append(' ')
.Append(entry.EntityState.ToString());

if ((options & StateManagerDebugStringOptions.IncludeProperties) != 0)
{
builder.AppendLine();

foreach (var property in entityType.GetProperties())
{
var currentValue = entry.GetCurrentValue(property);
builder
.Append(" ")
.Append(property.Name)
.Append(": ");

AppendValue(currentValue);

if (property.IsPrimaryKey())
{
builder.Append(" PK");
}
else if (property.IsKey())
{
builder.Append(" AK");
}

if (property.IsForeignKey())
{
builder.Append(" FK");
}

if (entry.IsModified(property))
{
builder.Append(" Modified");
}

if (entry.HasTemporaryValue(property))
{
builder.Append(" Temporary");
}

if (entry.HasOriginalValuesSnapshot
&& property.GetOriginalValueIndex() != -1)
{
var originalValue = entry.GetOriginalValue(property);
if (!Equals(originalValue, currentValue))
{
builder.Append(" Originally ");
AppendValue(originalValue);
}
}

builder.AppendLine();
}
}
else
{
foreach (var alternateKey in entityType.GetKeys().Where(k => !k.IsPrimaryKey()))
{
builder
.Append(" AK ")
.Append(entry.BuildCurrentValuesString(alternateKey.Properties));
}

foreach (var foreignKey in entityType.GetForeignKeys())
{
builder
.Append(" FK ")
.Append(entry.BuildCurrentValuesString(foreignKey.Properties));
}

builder.AppendLine();
}

if ((options & StateManagerDebugStringOptions.IncludeNavigations) != 0)
{
foreach (var navigation in entityType.GetNavigations())
{
var currentValue = entry.GetCurrentValue(navigation);
var targetType = navigation.GetTargetType();

builder
.Append(" ")
.Append(navigation.Name)
.Append(": ");

if (currentValue == null)
{
builder.Append("<null>");
}
else if (navigation.IsCollection())
{
builder.Append('[');

var relatedEntities = ((IEnumerable)currentValue).Cast<object>().Take(33).ToList();

for (var i = 0; i < relatedEntities.Count; i++)
{
if (i != 0)
{
builder.Append(", ");
}

if (i < 32)
{
AppendRelatedKey(targetType, relatedEntities[i]);
}
else
{
builder.Append("...");
}
}

builder.Append(']');
}
else
{
AppendRelatedKey(targetType, currentValue);
}

builder.AppendLine();
}
}
}

return builder.ToString();
}
}
}
Loading