diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..dbd3b25 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,7 @@ +[*.cs] + +# IDE0008: Use explicit type +csharp_style_var_elsewhere = false + +# IDE0008: Use explicit type +csharp_style_var_for_built_in_types = false diff --git a/AspNetCore.Examples.OpenTelemetry.ServiceDefaults/Extensions.cs b/AspNetCore.Examples.OpenTelemetry.ServiceDefaults/Extensions.cs index 5d5b872..c510276 100644 --- a/AspNetCore.Examples.OpenTelemetry.ServiceDefaults/Extensions.cs +++ b/AspNetCore.Examples.OpenTelemetry.ServiceDefaults/Extensions.cs @@ -7,7 +7,9 @@ using OpenTelemetry.Metrics; using OpenTelemetry.Trace; +#pragma warning disable IDE0130 // Namespace does not match folder structure namespace Microsoft.Extensions.Hosting; +#pragma warning restore IDE0130 // Namespace does not match folder structure // Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry. // This project should be referenced by each service project in your solution. diff --git a/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/ActivitySourceOptions.cs b/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/ActivitySourceOptions.cs index f3e54c0..de4b9fa 100644 --- a/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/ActivitySourceOptions.cs +++ b/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/ActivitySourceOptions.cs @@ -1,4 +1,6 @@ -namespace System.Diagnostics; +#pragma warning disable IDE0130 // Namespace does not match folder structure +namespace System.Diagnostics; +#pragma warning restore IDE0130 // Namespace does not match folder structure /// /// Options for creating an . @@ -15,11 +17,7 @@ public string Name get => _name; set { - if (value is null) - { - throw new ArgumentNullException(nameof(value)); - } - + ArgumentNullException.ThrowIfNull(value, nameof(Name)); _name = value; } } @@ -40,8 +38,7 @@ public string Name /// The ActivitySource name. public ActivitySourceOptions(string name) { - Name = name; - - Debug.Assert(_name is not null); + ArgumentNullException.ThrowIfNull(name, nameof(name)); + _name = name; } } diff --git a/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/Extensions.cs b/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/Extensions.cs index 24506f8..a9b8cc0 100644 --- a/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/Extensions.cs +++ b/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/Extensions.cs @@ -4,8 +4,9 @@ using System.Diagnostics.CodeAnalysis; using System.Reflection; +#pragma warning disable IDE0130 // Namespace does not match folder structure namespace Microsoft.Extensions.Hosting; - +#pragma warning restore IDE0130 // Namespace does not match folder structure public static class Extensions { private static readonly Type TelemetryType = typeof(Telemetry<>); @@ -57,7 +58,7 @@ private static IServiceCollection AddTelemetryInternal(this IServiceCo private static bool TryGetBaseTelemetryType(this Type implementationType, [MaybeNullWhen(returnValue: false)] out Type baseTelemetryType) { baseTelemetryType = null; - var type = implementationType; + Type? type = implementationType; while (type != null && type != typeof(object)) { if (type.IsGenericType && type.GetGenericTypeDefinition() == TelemetryType) diff --git a/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/ITelemetry.cs b/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/ITelemetry.cs index 237e650..ed666bb 100644 --- a/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/ITelemetry.cs +++ b/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/ITelemetry.cs @@ -1,7 +1,9 @@ using Microsoft.Extensions.Logging; using System.Diagnostics.Metrics; +#pragma warning disable IDE0130 // Namespace does not match folder structure namespace System.Diagnostics; +#pragma warning restore IDE0130 // Namespace does not match folder structure public interface ITelemetry { diff --git a/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/Telemetry.cs b/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/Telemetry.cs index aeb75b5..31955a4 100644 --- a/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/Telemetry.cs +++ b/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/Telemetry.cs @@ -2,9 +2,11 @@ using Microsoft.Extensions.Logging; using System.Diagnostics.Metrics; +#pragma warning disable IDE0130 // Namespace does not match folder structure namespace System.Diagnostics; +#pragma warning restore IDE0130 // Namespace does not match folder structure -public class Telemetry : ITelemetry +public class Telemetry : ITelemetry, IDisposable { private bool disposedValue; @@ -21,8 +23,8 @@ public Telemetry(ILoggerFactory loggerFactory, IMeterFactory meterFactory) public ActivitySource ActivitySource { get; } public Meter Meter { get; } - protected virtual ActivitySourceOptions ActivitySourceOptions => new ActivitySourceOptions(CategoryName); - protected virtual MeterOptions MeterOptions => new MeterOptions(CategoryName); + protected virtual ActivitySourceOptions ActivitySourceOptions => new(CategoryName); + protected virtual MeterOptions MeterOptions => new(CategoryName); protected virtual void Dispose(bool disposing) { diff --git a/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/TypeNameHelper.cs b/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/TypeNameHelper.cs index 4b87acd..43acc6d 100644 --- a/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/TypeNameHelper.cs +++ b/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/TypeNameHelper.cs @@ -12,7 +12,7 @@ internal static class TypeNameHelper { private const char DefaultNestedTypeDelimiter = '+'; - private static readonly Dictionary _builtInTypeNames = new Dictionary + private static readonly Dictionary _builtInTypeNames = new() { { typeof(void), "void" }, { typeof(bool), "bool" }, @@ -177,22 +177,14 @@ private static void ProcessGenericType(StringBuilder builder, Type type, Type[] } } - private readonly struct DisplayNameOptions + private readonly struct DisplayNameOptions(bool fullName, bool includeGenericParameterNames, bool includeGenericParameters, char nestedTypeDelimiter) { - public DisplayNameOptions(bool fullName, bool includeGenericParameterNames, bool includeGenericParameters, char nestedTypeDelimiter) - { - FullName = fullName; - IncludeGenericParameters = includeGenericParameters; - IncludeGenericParameterNames = includeGenericParameterNames; - NestedTypeDelimiter = nestedTypeDelimiter; - } - - public bool FullName { get; } + public bool FullName { get; } = fullName; - public bool IncludeGenericParameters { get; } + public bool IncludeGenericParameters { get; } = includeGenericParameters; - public bool IncludeGenericParameterNames { get; } + public bool IncludeGenericParameterNames { get; } = includeGenericParameterNames; - public char NestedTypeDelimiter { get; } + public char NestedTypeDelimiter { get; } = nestedTypeDelimiter; } } \ No newline at end of file diff --git a/AspNetCore.Examples.OpenTelemetry.sln b/AspNetCore.Examples.OpenTelemetry.sln index fbb046d..2d1f67d 100644 --- a/AspNetCore.Examples.OpenTelemetry.sln +++ b/AspNetCore.Examples.OpenTelemetry.sln @@ -7,6 +7,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNetCore.Examples.OpenTel EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A98F4720-450D-4CA0-B137-932A7FFA1F1A}" ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig README.md = README.md EndProjectSection EndProject