Skip to content

Commit

Permalink
Fix NativeAOT and remove warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Jul 22, 2024
1 parent 7d18097 commit c2b7784
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 40 deletions.
2 changes: 1 addition & 1 deletion sandbox/ConsoleApp/SampleCustomFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 46,7 @@ internal class CLEFMessageTemplateFormatter : IZLoggerFormatter
Utf8JsonWriter? jsonWriter;
ArrayBufferWriter<byte> originalFormatWriter = new ArrayBufferWriter<byte>();

public void FormatLogEntry<TEntry>(IBufferWriter<byte> writer, TEntry entry) where TEntry : IZLoggerEntry
public void FormatLogEntry(IBufferWriter<byte> writer, IZLoggerEntry entry)
{
// FormatLogEntry is guaranteed call in single-thread so reuse Utf8JsonWriter
jsonWriter?.Reset(writer);
Expand Down
2 changes: 1 addition & 1 deletion src/ZLogger.MessagePack/MessagePackZLoggerFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 79,7 @@ public class MessagePackZLoggerFormatter : IZLoggerFormatter
public MessagePackPropertyNames PropertyNames { get; set; } = MessagePackPropertyNames.Default;
public IKeyNameMutator? KeyNameMutator { get; set; }

public void FormatLogEntry<TEntry>(IBufferWriter<byte> writer, TEntry entry) where TEntry : IZLoggerEntry
public void FormatLogEntry(IBufferWriter<byte> writer, IZLoggerEntry entry)
{
var messagePackWriter = new MessagePackWriter(writer);
var propCount = BitOperations.PopCount((uint)IncludeProperties);
Expand Down
2 changes: 1 addition & 1 deletion src/ZLogger/Formatters/PlainTextZLoggerFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 35,7 @@ public void SetExceptionFormatter(Action<IBufferWriter<byte>, Exception> formatt
this.exceptionFormatter = formatter;
}

public void FormatLogEntry<TEntry>(IBufferWriter<byte> writer, TEntry entry) where TEntry : IZLoggerEntry
public void FormatLogEntry(IBufferWriter<byte> writer, IZLoggerEntry entry)
{
if (prefixTemplate != null)
{
Expand Down
5 changes: 4 additions & 1 deletion src/ZLogger/Formatters/SystemTextJsonZLoggerFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,4 1,5 @@
using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand Down Expand Up @@ -126,7 127,9 @@ public class SystemTextJsonZLoggerFormatter : IZLoggerFormatter

Utf8JsonWriter? jsonWriter;

public void FormatLogEntry<TEntry>(IBufferWriter<byte> writer, TEntry entry) where TEntry : IZLoggerEntry
[UnconditionalSuppressMessage("Trimming", "IL3050:RequiresDynamicCode")]
[UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode")]
public void FormatLogEntry(IBufferWriter<byte> writer, IZLoggerEntry entry)
{
jsonWriter?.Reset(writer);
jsonWriter ??= new Utf8JsonWriter(writer);
Expand Down
3 changes: 1 addition & 2 deletions src/ZLogger/IZLoggerFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 6,6 @@ namespace ZLogger
public interface IZLoggerFormatter
{
bool WithLineBreak { get; }
void FormatLogEntry<TEntry>(IBufferWriter<byte> writer, TEntry entry)
where TEntry : IZLoggerEntry;
void FormatLogEntry(IBufferWriter<byte> writer, IZLoggerEntry entry);
}
}
9 changes: 7 additions & 2 deletions src/ZLogger/Internal/CodeGeneratorUtil.cs
Original file line number Diff line number Diff line change
@@ -1,4 1,5 @@
using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using Utf8StringInterpolation;

Expand All @@ -25,7 26,9 @@ static Utf8JsonWriter GetThreadStaticUtf8JsonWriter(IBufferWriter<byte> bufferWr
return writer;
}

public static void AppendAsJson<T>(ref Utf8StringWriter<IBufferWriter<byte>> stringWriter, T value)
[UnconditionalSuppressMessage("Trimming", "IL3050:RequiresDynamicCode")]
[UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode")]
public static void AppendAsJson<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(ref Utf8StringWriter<IBufferWriter<byte>> stringWriter, T value)
{
stringWriter.ClearState();

Expand All @@ -35,6 38,8 @@ public static void AppendAsJson<T>(ref Utf8StringWriter<IBufferWriter<byte>> str
utf8JsonWriter.Reset();
}

[UnconditionalSuppressMessage("Trimming", "IL3050:RequiresDynamicCode")]
[UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode")]
public static void AppendAsJson(ref Utf8StringWriter<IBufferWriter<byte>> stringWriter, object? value, Type inputType)
{
stringWriter.ClearState();
Expand All @@ -45,7 50,7 @@ public static void AppendAsJson(ref Utf8StringWriter<IBufferWriter<byte>> string
utf8JsonWriter.Reset();
}

public static unsafe void WriteJsonEnum<T>(Utf8JsonWriter writer, JsonEncodedText key, T value)
public static unsafe void WriteJsonEnum<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(Utf8JsonWriter writer, JsonEncodedText key, T value)
{
var enumValue = EnumDictionary<T>.GetJsonEncodedName(value);
if (enumValue == null)
Expand Down
5 changes: 3 additions & 2 deletions src/ZLogger/Internal/EnumDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 8,19 @@

namespace ZLogger.Internal;

internal static unsafe class EnumDictionary<T>
internal static unsafe class EnumDictionary<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>
{
static object dictionary;

public static readonly delegate* managed<T, string?> GetStringName;
public static readonly delegate* managed<T, ReadOnlySpan<byte>> GetUtf8Name; // be careful, zero is not found.
public static readonly delegate* managed<T, JsonEncodedText?> GetJsonEncodedName;

[UnconditionalSuppressMessage("Trimming", "IL3050:RequiresDynamicCode")]
[UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode")]
static EnumDictionary()
{
var type = typeof(T);

var source = new List<(T, EnumName)>();
foreach (var key in Enum.GetValues(type))
{
Expand Down
17 changes: 10 additions & 7 deletions src/ZLogger/Internal/MagicalBox.cs
Original file line number Diff line number Diff line change
@@ -1,5 1,6 @@
using System.Buffers;
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Text.Json;
using Utf8StringInterpolation;
Expand All @@ -18,7 19,7 @@ public MagicalBox(byte[] storage)

public int Written => written;

public bool TryWrite<T>(T value, out int offset)
public bool TryWrite<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(T value, out int offset)
{
if (!IsSupportedType<T>())
{
Expand Down Expand Up @@ -307,7 308,7 @@ sealed class Handlers(
public static bool TryReadTo(Type type, MagicalBox box, int offset, ref Utf8StringWriter<IBufferWriter<byte>> writer, int alignment, string? format) => cache.TryGetValue(type, out var value) ? value.Utf8StringWriter(box, offset, ref writer, alignment, format) : false;
public static object? ReadBoxed(Type type, MagicalBox box, int offset) => cache.TryGetValue(type, out var value) ? value.ReadBoxed(box, offset) : null;

public static void Register<T>()
public static void Register<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>()
{
if (IsRegistered<T>.Value)
{
Expand Down Expand Up @@ -339,7 340,7 @@ public static void Register<T>()
IsRegistered<T>.Value = true;
}

static object? ReadBoxed<T>(MagicalBox box, int offset)
static object? ReadBoxed<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(MagicalBox box, int offset)
{
if (box.TryRead<T>(offset, out var v))
{
Expand All @@ -348,7 349,7 @@ public static void Register<T>()
return null;
}

static bool EnumJsonWrite<T>(MagicalBox box, int offset, Utf8JsonWriter writer, JsonSerializerOptions? options)
static bool EnumJsonWrite<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(MagicalBox box, int offset, Utf8JsonWriter writer, JsonSerializerOptions? options)
{
if (box.TryRead<T>(offset, out var v))
{
Expand Down Expand Up @@ -436,7 437,9 @@ static bool DateTimeOffsetJsonWrite(MagicalBox box, int offset, Utf8JsonWriter w
return false;
}

static bool JsonSerialize<T>(MagicalBox box, int offset, Utf8JsonWriter writer, JsonSerializerOptions? options)
[UnconditionalSuppressMessage("Trimming", "IL3050:RequiresDynamicCode")]
[UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode")]
static bool JsonSerialize<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(MagicalBox box, int offset, Utf8JsonWriter writer, JsonSerializerOptions? options)
{
if (box.TryRead<T>(offset, out var v))
{
Expand All @@ -447,7 450,7 @@ static bool JsonSerialize<T>(MagicalBox box, int offset, Utf8JsonWriter writer,
return false;
}

static bool StringAppendFormatted<T>(MagicalBox box, int offset, ref DefaultInterpolatedStringHandler handler, int alignment, string? format)
static bool StringAppendFormatted<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(MagicalBox box, int offset, ref DefaultInterpolatedStringHandler handler, int alignment, string? format)
{
if (box.TryRead<T>(offset, out var v))
{
Expand All @@ -458,7 461,7 @@ static bool StringAppendFormatted<T>(MagicalBox box, int offset, ref DefaultInte
return false;
}

static bool Utf8AppendFormatted<T>(MagicalBox box, int offset, ref Utf8StringWriter<IBufferWriter<byte>> writer, int alignment, string? format)
static bool Utf8AppendFormatted<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(MagicalBox box, int offset, ref Utf8StringWriter<IBufferWriter<byte>> writer, int alignment, string? format)
{
if (box.TryRead<T>(offset, out var v))
{
Expand Down
3 changes: 3 additions & 0 deletions src/ZLogger/LogStates/InterpolatedStringLogState.cs
Original file line number Diff line number Diff line change
@@ -1,5 1,6 @@
using System.Buffers;
using System.Collections;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Text.Json;
using ZLogger.Formatters;
Expand Down Expand Up @@ -114,6 115,8 @@ public void WriteOriginalFormat(IBufferWriter<byte> writer)
messageSequence.WriteOriginalFormat(writer, parameters);
}

[UnconditionalSuppressMessage("Trimming", "IL3050:RequiresDynamicCode")]
[UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode")]
public void WriteJsonParameterKeyValues(Utf8JsonWriter jsonWriter, JsonSerializerOptions jsonSerializerOptions, IKeyNameMutator? keyNameMutator = null)
{
for (var i = 0; i < ParameterCount; i )
Expand Down
3 changes: 3 additions & 0 deletions src/ZLogger/LogStates/StringFormatterLogState.cs
Original file line number Diff line number Diff line change
@@ -1,4 1,5 @@
using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using System.Text.Json;
using ZLogger.Internal;
Expand Down Expand Up @@ -81,6 82,8 @@ public void WriteOriginalFormat(IBufferWriter<byte> writer)
}
}

[UnconditionalSuppressMessage("Trimming", "IL3050:RequiresDynamicCode")]
[UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode")]
public void WriteJsonParameterKeyValues(Utf8JsonWriter jsonWriter, JsonSerializerOptions jsonSerializerOptions, IKeyNameMutator? keyNameMutator = null)
{
if (originalStateParameters == null) return;
Expand Down
12 changes: 10 additions & 2 deletions src/ZLogger/ZLogger.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 8,20 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>1701;1702;1591;1573</NoWarn>

<!-- NuGet Packaging -->
<!-- Enable for using [DynamicallyAccessedMembers] attribute -->
<PolySharpIncludeRuntimeSupportedAttributes>true</PolySharpIncludeRuntimeSupportedAttributes>

<!-- NuGet Packaging -->
<PackageTags>logging;</PackageTags>
<Description>Zero Allocation Text/Strcutured Logger for .NET Core, built on top of a Microsoft.Extensions.Logging.</Description>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<PropertyGroup Condition="$(TargetFramework) == 'net8.0'">
<!-- Enable AOT analyzers -->
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\Icon.png" Pack="true" PackagePath="/" />
<EmbeddedResource Include="..\..\LICENSE" />
Expand All @@ -36,7 44,7 @@
<PackageReference Include="Microsoft.Bcl.TimeProvider" Version="8.0.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageReference Include="System.Collections.Immutable" Version="8.0.0" />
<PackageReference Include="PolySharp" Version="1.13.2">
<PackageReference Include="PolySharp" Version="1.14.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Loading

0 comments on commit c2b7784

Please sign in to comment.