From 34c72d71073fd578e312e22696dfe1ce350346e2 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Fri, 5 Apr 2024 07:19:18 +1100 Subject: [PATCH] #357 add in dotnet 6 TFM and tweak dependencies (#361) * #357 add in dotnet 6 TFM and tweak dependencies * fix better nullability compiler errors on net6.0 --------- Co-authored-by: Martijn Laarman --- .../EcsDocument.DefaultService.cs | 4 +++- src/Elastic.CommonSchema/EcsDocument.cs | 12 +++++++----- .../Elastic.CommonSchema.csproj | 11 +++++++---- src/Elastic.CommonSchema/Location.cs | 2 +- .../EcsDocumentJsonConverterFactory.cs | 13 ++++++++----- 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/Elastic.CommonSchema/EcsDocument.DefaultService.cs b/src/Elastic.CommonSchema/EcsDocument.DefaultService.cs index 694c9230..36dfaa2a 100644 --- a/src/Elastic.CommonSchema/EcsDocument.DefaultService.cs +++ b/src/Elastic.CommonSchema/EcsDocument.DefaultService.cs @@ -89,8 +89,10 @@ from frame in stackFrames } - internal static bool IsMsOrElastic(byte[] array) + internal static bool IsMsOrElastic(byte[]? array) { + if (array == null) return false; + var elasticApmToken = new byte[] { 174, 116, 0, 210, 193, 137, 207, 34 }; var mscorlibToken = new byte[] { 183, 122, 92, 86, 25, 52, 224, 137 }; var systemWebToken = new byte[] { 176, 63, 95, 127, 17, 213, 10, 58 }; diff --git a/src/Elastic.CommonSchema/EcsDocument.cs b/src/Elastic.CommonSchema/EcsDocument.cs index cbf9c4ba..23fbb888 100644 --- a/src/Elastic.CommonSchema/EcsDocument.cs +++ b/src/Elastic.CommonSchema/EcsDocument.cs @@ -100,11 +100,13 @@ internal static IDictionary GetOTelResourceAttributes() return ParseOTelResourceAttributes(resourceAttributes); } - private static IDictionary ParseOTelResourceAttributes(string resourceAttributes) + private static IDictionary ParseOTelResourceAttributes(string? resourceAttributes) { if (string.IsNullOrEmpty(resourceAttributes)) return new Dictionary(); - var keyValues = resourceAttributes + // Needed to satisfy all TFM's sadly. + // ReSharper disable once RedundantSuppressNullableWarningExpression + var keyValues = resourceAttributes! .Split(new[] { ',' }, RemoveEmptyEntries) .Select(k => k.Split(new[] { '=' }, 2, RemoveEmptyEntries)) .Where(kv => kv.Length == 2) @@ -125,14 +127,14 @@ public static Agent CreateAgent(Type typeFromAgentLibrary) return new Agent { Type = type, Version = version }; } - private static (string, string) GetAssemblyVersion(Assembly assembly) + private static (string?, string?) GetAssemblyVersion(Assembly assembly) { var name = assembly.GetName(); var type = name.Name; var versionAttribute = assembly.GetCustomAttributes(false) .OfType() .FirstOrDefault(); - var version = versionAttribute?.InformationalVersion ?? name.Version.ToString(); + var version = versionAttribute?.InformationalVersion ?? name?.Version?.ToString(); return (type, version); } @@ -209,7 +211,7 @@ Process ReturnFromCache() private static readonly string UserDomainName = Environment.UserDomainName; //Can not cache current thread's identity as it's used for role based security, different threads can have different identities - private static User GetUser() => new () { Id = Thread.CurrentPrincipal?.Identity.Name, Name = UserName, Domain = UserDomainName }; + private static User GetUser() => new () { Id = Thread.CurrentPrincipal?.Identity?.Name, Name = UserName, Domain = UserDomainName }; private static Error? GetError(Exception? exception) { diff --git a/src/Elastic.CommonSchema/Elastic.CommonSchema.csproj b/src/Elastic.CommonSchema/Elastic.CommonSchema.csproj index a5391761..05ff26a9 100644 --- a/src/Elastic.CommonSchema/Elastic.CommonSchema.csproj +++ b/src/Elastic.CommonSchema/Elastic.CommonSchema.csproj @@ -1,7 +1,7 @@ - + - netstandard2.0;netstandard2.1;net461 + netstandard2.0;netstandard2.1;net461;net6.0 Elastic Common Schema (ECS) Types Maps Elastic Common Schema (ECS) to .NET types including (de)serialization using System.Text.Json latest @@ -10,12 +10,15 @@ - - runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + diff --git a/src/Elastic.CommonSchema/Location.cs b/src/Elastic.CommonSchema/Location.cs index 9c6a9117..996bbff6 100644 --- a/src/Elastic.CommonSchema/Location.cs +++ b/src/Elastic.CommonSchema/Location.cs @@ -55,7 +55,7 @@ public override string ToString() => Longitude.ToString("#0.0#######", CultureInfo.InvariantCulture); /// > - public override bool Equals(object obj) + public override bool Equals(object? obj) { if (ReferenceEquals(null, obj)) return false; diff --git a/src/Elastic.CommonSchema/Serialization/EcsDocumentJsonConverterFactory.cs b/src/Elastic.CommonSchema/Serialization/EcsDocumentJsonConverterFactory.cs index 640f28bd..167c9271 100644 --- a/src/Elastic.CommonSchema/Serialization/EcsDocumentJsonConverterFactory.cs +++ b/src/Elastic.CommonSchema/Serialization/EcsDocumentJsonConverterFactory.cs @@ -14,10 +14,13 @@ public class EcsDocumentJsonConverterFactory : JsonConverterFactory public override bool CanConvert(Type typeToConvert) => typeof(EcsDocument).IsAssignableFrom(typeToConvert); /// - public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) => - typeToConvert == typeof(EcsDocument) - ? EcsJsonConfiguration.DefaultEcsDocumentJsonConverter - // TODO validate this is only called once - : (JsonConverter)Activator.CreateInstance(typeof(EcsDocumentJsonConverter<>).MakeGenericType(typeToConvert)); + public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) + { + if (typeToConvert == typeof(EcsDocument)) + return EcsJsonConfiguration.DefaultEcsDocumentJsonConverter; + + var instance = Activator.CreateInstance(typeof(EcsDocumentJsonConverter<>).MakeGenericType(typeToConvert)); + return (JsonConverter)instance!; + } } }