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

Revisit Razor logging #10641

Merged
merged 16 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ public CodeWriter WriteLine([InterpolatedStringHandlerArgument("")] ref WriteInt

public string GenerateCode()
{
// Eventually, we need to remove this and not return a giant string, which can
// easily be allocated on the LOH. The work to remove this is tracked by
// https://github.com/dotnet/razor/issues/8076.
return CreateString(Length, _pages, static (span, pages) =>
{
foreach (var page in pages)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ private readonly struct FormattedMessageState
{
// The leading whitespace matches the time space length "[hh:mm:ss.fffffff] "
private static readonly ReadOnlyMemory<char> s_leadingWhiteSpace = " ".AsMemory();
private static readonly ReadOnlyMemory<char> s_newLine = Environment.NewLine.AsMemory();

private readonly ReadOnlyMemory<char> _message;
private readonly ReadOnlyMemory<Range> _messageLineRanges;
Expand Down Expand Up @@ -99,7 +100,7 @@ public static FormattedMessageState Create(
ref MemoryBuilder<Range> exceptionLineRangeBuilder)
{
var messageText = message.AsMemory();
var newLine = Environment.NewLine.AsMemory();
var newLine = s_newLine;

var categoryNamePart = ('[' + categoryName + "] ").AsMemory();

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

using System;
using Xunit;

namespace Microsoft.AspNetCore.Razor.Utilities.Shared.Test;

public class MemoryBuilderTests
{
[Fact]
public void StartWithDefault()
{
using MemoryBuilder<int> builder = default;

for (var i = 0; i < 1000; i++)
{
builder.Append(i);
}

var result = builder.AsMemory();

for (var i = 0; i < 1000; i++)
{
Assert.Equal(i, result.Span[i]);
}
}

[Fact]
public void StartWithNew()
{
using MemoryBuilder<int> builder = new();

for (var i = 0; i < 1000; i++)
{
builder.Append(i);
}

var result = builder.AsMemory();

for (var i = 0; i < 1000; i++)
{
Assert.Equal(i, result.Span[i]);
}
}

[Fact]
public void StartWithInitialCapacity()
{
using MemoryBuilder<int> builder = new(1024);

for (var i = 0; i < 1000; i++)
{
builder.Append(i);
}

var result = builder.AsMemory();

for (var i = 0; i < 1000; i++)
{
Assert.Equal(i, result.Span[i]);
}
}

[Fact]
public void StartWithInitialArray()
{
using MemoryBuilder<int> builder = new(1024);

for (var i = 0; i < 1000; i++)
{
builder.Append(i);
}

var result = builder.AsMemory();

for (var i = 0; i < 1000; i++)
{
Assert.Equal(i, result.Span[i]);
}
}

[Fact]
public void AppendChunks()
{
using MemoryBuilder<int> builder = default;

ReadOnlySpan<int> chunk = [1, 2, 3, 4, 5, 6, 7, 8];

for (var i = 0; i < 1000; i++)
{
builder.Append(chunk);
}

var result = builder.AsMemory();

for (var i = 0; i < 1000; i++)
{
for (var j = 0; j < chunk.Length; j++)
{
Assert.Equal(chunk[j], result.Span[(i * 8) + j]);
}
}
}
}
112 changes: 13 additions & 99 deletions src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/ArgHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpres
#else
if (argument is null)
{
ThrowArgumentNullException(paramName);
ThrowHelper.ThrowArgumentNullException(paramName);
}
#endif
}
Expand All @@ -31,36 +31,20 @@ public static unsafe void ThrowIfNull([NotNull] void* argument, [CallerArgumentE
#else
if (argument is null)
{
ThrowArgumentNullException(paramName);
ThrowHelper.ThrowArgumentNullException(paramName);
}
#endif
}

#if !NET8_0_OR_GREATER
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowArgumentNullException(string? paramName)
{
throw new ArgumentNullException(paramName);
}
#endif

public static void ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
#if NET8_0_OR_GREATER
ArgumentException.ThrowIfNullOrEmpty(argument, paramName);
#else
if (argument.IsNullOrEmpty())
{
ThrowException(argument, paramName);
}

[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowException(string? argument, string? paramName)
{
ThrowIfNull(argument, paramName);
throw new ArgumentException(SR.The_value_cannot_be_an_empty_string, paramName);
ThrowHelper.ThrowArgumentException(paramName, SR.The_value_cannot_be_an_empty_string);
}
#endif
}
Expand All @@ -72,16 +56,9 @@ public static void ThrowIfNullOrWhiteSpace([NotNull] string? argument, [CallerAr
#else

if (argument.IsNullOrWhiteSpace())
{
ThrowException(argument, paramName);
}

[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowException(string? argument, string? paramName)
{
ThrowIfNull(argument, paramName);
throw new ArgumentException(SR.The_value_cannot_be_an_empty_string_composed_entirely_of_whitespace, paramName);
ThrowHelper.ThrowArgumentException(paramName, SR.The_value_cannot_be_an_empty_string_composed_entirely_of_whitespace);
}
#endif
}
Expand All @@ -93,14 +70,7 @@ public static void ThrowIfZero(int value, [CallerArgumentExpression(nameof(value
#else
if (value == 0)
{
ThrowException(value, paramName);
}

[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowException(int value, string? paramName)
{
throw new ArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_a_non_zero_value(paramName, value));
ThrowHelper.ThrowArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_a_non_zero_value(paramName, value));
}
#endif
}
Expand All @@ -112,14 +82,7 @@ public static void ThrowIfNegative(int value, [CallerArgumentExpression(nameof(v
#else
if (value < 0)
{
ThrowException(value, paramName);
}

[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowException(int value, string? paramName)
{
throw new ArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_a_non_negative_value(paramName, value));
ThrowHelper.ThrowArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_a_non_negative_value(paramName, value));
}
#endif
}
Expand All @@ -131,14 +94,7 @@ public static void ThrowIfNegativeOrZero(int value, [CallerArgumentExpression(na
#else
if (value <= 0)
{
ThrowException(value, paramName);
}

[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowException(int value, string? paramName)
{
throw new ArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_a_non_negative_and_non_zero_value(paramName, value));
ThrowHelper.ThrowArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_a_non_negative_and_non_zero_value(paramName, value));
}
#endif
}
Expand All @@ -151,14 +107,7 @@ public static void ThrowIfEqual<T>(T value, T other, [CallerArgumentExpression(n
#else
if (EqualityComparer<T>.Default.Equals(value, other))
{
ThrowException(value, other, paramName);
}

[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowException(T value, T other, string? paramName)
{
throw new ArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_not_be_equal_to_2(paramName, (object?)value ?? "null", (object?)other ?? "null"));
ThrowHelper.ThrowArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_not_be_equal_to_2(paramName, (object?)value ?? "null", (object?)other ?? "null"));
}
#endif
}
Expand All @@ -171,14 +120,7 @@ public static void ThrowIfNotEqual<T>(T value, T other, [CallerArgumentExpressio
#else
if (!EqualityComparer<T>.Default.Equals(value, other))
{
ThrowException(value, other, paramName);
}

[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowException(T value, T other, string? paramName)
{
throw new ArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_equal_to_2(paramName, (object?)value ?? "null", (object?)other ?? "null"));
ThrowHelper.ThrowArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_equal_to_2(paramName, (object?)value ?? "null", (object?)other ?? "null"));
}
#endif
}
Expand All @@ -191,14 +133,7 @@ public static void ThrowIfGreaterThan<T>(T value, T other, [CallerArgumentExpres
#else
if (value.CompareTo(other) > 0)
{
ThrowException(value, other, paramName);
}

[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowException(T value, T other, string? paramName)
{
throw new ArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_less_than_or_equal_to_2(paramName, value, other));
ThrowHelper.ThrowArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_less_than_or_equal_to_2(paramName, value, other));
}
#endif
}
Expand All @@ -211,14 +146,7 @@ public static void ThrowIfGreaterThanOrEqual<T>(T value, T other, [CallerArgumen
#else
if (value.CompareTo(other) >= 0)
{
ThrowException(value, other, paramName);
}

[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowException(T value, T other, string? paramName)
{
throw new ArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_less_than_2(paramName, value, other));
ThrowHelper.ThrowArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_less_than_2(paramName, value, other));
}
#endif
}
Expand All @@ -231,14 +159,7 @@ public static void ThrowIfLessThan<T>(T value, T other, [CallerArgumentExpressio
#else
if (value.CompareTo(other) < 0)
{
ThrowException(value, other, paramName);
}

[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowException(T value, T other, string? paramName)
{
throw new ArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_greater_than_or_equal_to_2(paramName, value, other));
ThrowHelper.ThrowArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_greater_than_or_equal_to_2(paramName, value, other));
}
#endif
}
Expand All @@ -251,14 +172,7 @@ public static void ThrowIfLessThanOrEqual<T>(T value, T other, [CallerArgumentEx
#else
if (value.CompareTo(other) <= 0)
{
ThrowException(value, other, paramName);
}

[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowException(T value, T other, string? paramName)
{
throw new ArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_greater_than_2(paramName, value, other));
ThrowHelper.ThrowArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_greater_than_2(paramName, value, other));
}
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;

#if !NET
using ThrowHelper = Microsoft.AspNetCore.Razor.Utilities.ThrowHelper;
#endif
using Microsoft.AspNetCore.Razor;

namespace Microsoft.AspNetCore.Razor;

Expand Down Expand Up @@ -43,7 +40,7 @@ public static ReadOnlySpan<T> AsSpan<T>(this T[]? array, Index startIndex)
{
if (!startIndex.Equals(Index.Start))
{
ThrowHelper.ThrowArgumentOutOfRange(nameof(startIndex));
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(startIndex));
}

return default;
Expand Down Expand Up @@ -86,7 +83,7 @@ public static ReadOnlySpan<T> AsSpan<T>(this T[]? array, Range range)
{
if (!range.Start.Equals(Index.Start) || !range.End.Equals(Index.Start))
{
ThrowHelper.ThrowArgumentNull(nameof(array));
ThrowHelper.ThrowArgumentNullException(nameof(array));
}

return default;
Expand Down Expand Up @@ -128,7 +125,7 @@ public static ReadOnlyMemory<T> AsMemory<T>(this T[]? array, Index startIndex)
{
if (!startIndex.Equals(Index.Start))
{
ThrowHelper.ThrowArgumentOutOfRange(nameof(startIndex));
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(startIndex));
}

return default;
Expand Down Expand Up @@ -172,7 +169,7 @@ public static ReadOnlyMemory<T> AsMemory<T>(this T[]? array, Range range)
{
if (!range.Start.Equals(Index.Start) || !range.End.Equals(Index.Start))
{
ThrowHelper.ThrowArgumentNull(nameof(array));
ThrowHelper.ThrowArgumentNullException(nameof(array));
}

return default;
Expand Down
Loading
Loading