Skip to content
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
2 changes: 2 additions & 0 deletions AutoLoggerMessage.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
<Project Path="src/AutoLoggerMessageGenerator/AutoLoggerMessageGenerator.Pack.csproj" />
<Project Path="src/AutoLoggerMessageGenerator/AutoLoggerMessageGenerator.Roslyn4_11.csproj" />
<Project Path="src/AutoLoggerMessageGenerator/AutoLoggerMessageGenerator.Roslyn4_8.csproj" />
<Project Path="src/AutoLoggerMessageGenerator/AutoLoggerMessageGenerator.Roslyn4_14.csproj" />
</Folder>
<Folder Name="/Tests/">
<Project Path="tests/AutoLoggerMessageGenerator.IntegrationTests/AutoLoggerMessageGenerator.IntegrationTests.csproj" />
<Project Path="tests/AutoLoggerMessageGenerator.UnitTests/AutoLoggerMessageGenerator.UnitTests.Roslyn4_11.csproj" />
<Project Path="tests/AutoLoggerMessageGenerator.UnitTests/AutoLoggerMessageGenerator.UnitTests.Roslyn4_8.csproj" />
<Project Path="tests/AutoLoggerMessageGenerator.UnitTests/AutoLoggerMessageGenerator.UnitTests.Roslyn4_14.csproj" />
</Folder>
<Project Path="src/AutoLoggerMessageGenerator.Sandbox/AutoLoggerMessageGenerator.Sandbox.csproj" />
</Solution>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<PropertyGroup>
<DefineConstants>$(DefineConstants);JETBRAINS_ANNOTATIONS</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" PrivateAssets="all"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>$(AssemblyName).UnitTests.Roslyn4_11</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>$(AssemblyName).UnitTests.Roslyn4_14</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>$(AssemblyName).IntegrationTests</_Parameter1>
</AssemblyAttribute>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@

<Import Project="AutoLoggerMessageGenerator.Build.targets" />

<PropertyGroup>
<DefineConstants>$(DefineConstants);JETBRAINS_ANNOTATIONS</DefineConstants>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="AutoLoggerMessageGenerator.Roslyn4_11.csproj" PrivateAssets="All" ReferenceOutputAssembly="false" />
<ProjectReference Include="AutoLoggerMessageGenerator.Roslyn4_8.csproj" PrivateAssets="All" ReferenceOutputAssembly="false" />
<ProjectReference Include="AutoLoggerMessageGenerator.Roslyn4_11.csproj" PrivateAssets="All" ReferenceOutputAssembly="false" />
<ProjectReference Include="AutoLoggerMessageGenerator.Roslyn4_14.csproj" PrivateAssets="All" ReferenceOutputAssembly="false" />
</ItemGroup>

<ItemGroup>
Expand All @@ -18,6 +23,8 @@
Pack="true" PackagePath="analyzers/dotnet/roslyn4.8/cs" Visible="false" />
<None Include="$(OutputPath)\..\..\AutoLoggerMessageGenerator.Roslyn4_11\$(ArtifactsPivots)\$(AssemblyName).dll"
Pack="true" PackagePath="analyzers/dotnet/roslyn4.11/cs" Visible="false" />
<None Include="$(OutputPath)\..\..\AutoLoggerMessageGenerator.Roslyn4_14\$(ArtifactsPivots)\$(AssemblyName).dll"
Pack="true" PackagePath="analyzers/dotnet/roslyn4.14/cs" Visible="false" />
<None Include="$(OutputPath)\AutoLoggerMessageGenerator.BuildOutput.dll" Pack="true" PackagePath="lib\netstandard2.0" Visible="false" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RoslynApiVersion>4.14.0</RoslynApiVersion>
<DefineConstants>$(DefineConstants);HASH_BASED_INTERCEPTORS;EMBEDDED</DefineConstants>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<Compile Update="Generators\AutoLoggerMessageGenerator.LoggerMessage.cs">
<DependentUpon>AutoLoggerMessageGenerator.cs</DependentUpon>
</Compile>
<Compile Update="Generators\AutoLoggerMessageGenerator.LoggerScopes.cs">
<DependentUpon>AutoLoggerMessageGenerator.cs</DependentUpon>
</Compile>
</ItemGroup>

<Import Project="AutoLoggerMessageGenerator.Build.targets" />
</Project>
2 changes: 2 additions & 0 deletions src/AutoLoggerMessageGenerator/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ internal static class Constants
$"[global::System.CodeDom.Compiler.GeneratedCodeAttribute(" +
$"\"{typeof(Generators.AutoLoggerMessageGenerator).Assembly.GetName().Name}\", " +
$"\"{typeof(Generators.AutoLoggerMessageGenerator).Assembly.GetName().Version}\")]";

public const string EmbeddedAttribute = "[global::Microsoft.CodeAnalysis.EmbeddedAttribute]";
public const string EditorNotBrowsableAttribute = "[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]";

public const string GeneratedFileHeader = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public static string Emit()
sb.WriteLine('{');
sb.Indent++;

#if EMBEDDED
sb.WriteLine(Constants.EmbeddedAttribute);
#endif

sb.WriteLine(Constants.GeneratedCodeAttribute);
sb.WriteLine(Constants.EditorNotBrowsableAttribute);
sb.WriteLine("[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public static string Emit(IEnumerable<LogMessageCall> logCalls)
sb.WriteLine('{');
sb.Indent++;

#if EMBEDDED
sb.WriteLine(Constants.EmbeddedAttribute);
#endif

sb.WriteLine(Constants.GeneratedCodeAttribute);
sb.WriteLine(Constants.EditorNotBrowsableAttribute);
sb.WriteLine(Constants.ExcludeFromCoverageAttribute);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public static string Emit(IEnumerable<LoggerScopeCall> loggerScopes)
sb.WriteLine('{');
sb.Indent++;

#if EMBEDDED
sb.WriteLine(Constants.EmbeddedAttribute);
#endif

sb.WriteLine(Constants.GeneratedCodeAttribute);
sb.WriteLine(Constants.EditorNotBrowsableAttribute);
sb.WriteLine(Constants.ExcludeFromCoverageAttribute);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ public static string Emit(ImmutableArray<LoggerScopeCall> loggerScopes)
sb.WriteLine('{');
sb.Indent++;

#if EMBEDDED
sb.WriteLine(EmbeddedAttribute);
#endif

sb.WriteLine(Constants.GeneratedCodeAttribute);
sb.WriteLine(EditorNotBrowsableAttribute);
sb.WriteLine($"public static class {LoggerScopesGeneratorName}");
sb.WriteLine($"internal static class {LoggerScopesGeneratorName}");
sb.WriteLine('{');
sb.Indent++;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
GenerateInterceptorAttribute(context, configuration);
}

private static void GenerateInterceptorAttribute(IncrementalGeneratorInitializationContext context,
private static void GenerateInterceptorAttribute(
IncrementalGeneratorInitializationContext context,
IncrementalValueProvider<SourceGeneratorConfiguration> configuration)
{
#if EMBEDDED
context.RegisterPostInitializationOutput(ctx =>
{
ctx.AddEmbeddedAttributeDefinition();
});
#endif

context.RegisterImplementationSourceOutput(configuration, static (ctx, configuration) =>
{
if (configuration.GenerateInterceptorAttribute)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,16 @@ static string GetAttributeDisplayName(INamedTypeSymbol attributeType)
/// 2. <see href="https://github.com/dotnet/roslyn/blob/6c9697c56fe39d2335b61ae7c6b342e7b76779ef/docs/features/code-generation.cookbook.md#consume-msbuild-properties-and-metadata">
/// Reading MSBuild Properties in Source Generators</see>.
/// </summary>
#pragma warning disable RS1035
/// <param name="context"><see cref="GeneratorExecutionContext"/>.</param>
#pragma warning restore RS1035
/// <param name="msBuildProperty">The name of the MSBuild property that determines whether to produce a report.</param>
/// <returns>bool value to indicate if reports should be generated.</returns>
public static bool ShouldGenerateReport(GeneratorExecutionContext context, string msBuildProperty)
{
#pragma warning disable RS1035
_ = context.AnalyzerConfigOptions.GlobalOptions.TryGetValue(msBuildProperty, out var generateFiles);
#pragma warning restore RS1035

return string.Equals(generateFiles, bool.TrueString, StringComparison.OrdinalIgnoreCase);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
using System.Text;

namespace AutoLoggerMessageGenerator.PostProcessing;

internal static class LoggerMessageResultAdjuster
{
public static string? Adjust(string? generatedCode) =>
generatedCode?.Replace(
$"static partial void {Constants.LogMethodPrefix}",
public static string? Adjust(string? generatedCode)
{
var builder = new StringBuilder(generatedCode).Replace(
$"static partial void {Constants.LogMethodPrefix}",
$"static void {Constants.LogMethodPrefix}"
);

#if EMBEDDED
builder = builder.Replace(
$"partial class {Constants.LoggerClassName}",
$"{Constants.EmbeddedAttribute} partial class {Constants.LoggerClassName}"
);
#endif

return builder.ToString();
}
}
8 changes: 8 additions & 0 deletions tests/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[*.{received,verified}.{json,txt,xml}]
charset = utf-8-bom
end_of_line = lf
indent_size = unset
indent_style = unset
insert_final_newline = false
tab_width = unset
trim_trailing_whitespace = false
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

<PropertyGroup Condition="$([MSBuild]::VersionLessThan('$(NETCoreSdkVersion)', '8.0.402'))">
<RoslynVersion>4_8</RoslynVersion>
</PropertyGroup>

<PropertyGroup Condition="$([MSBuild]::VersionGreaterThanOrEquals('$(NETCoreSdkVersion)', '8.0.402'))">
<RoslynVersion>4_11</RoslynVersion>
</PropertyGroup>

<PropertyGroup Condition="$([MSBuild]::VersionGreaterThanOrEquals('$(NETCoreSdkVersion)', '8.0.402'))">
<UseNewRoslyn>true</UseNewRoslyn>
<RoslynVersion>4_14</RoslynVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\AutoLoggerMessageGenerator.BuildOutput\AutoLoggerMessageGenerator.BuildOutput.csproj" />
</ItemGroup>

<ItemGroup Condition="$(UseNewRoslyn) == true">
<ProjectReference Include="..\..\src\AutoLoggerMessageGenerator\AutoLoggerMessageGenerator.Roslyn4_11.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="true"/>
</ItemGroup>

<ItemGroup Condition="$(UseNewRoslyn) != true">
<ProjectReference Include="..\..\src\AutoLoggerMessageGenerator\AutoLoggerMessageGenerator.Roslyn4_8.csproj"
<ProjectReference Include="..\..\src\AutoLoggerMessageGenerator\AutoLoggerMessageGenerator.Roslyn$(RoslynVersion).csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="true"/>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0"/>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="$(RoslynApiVersion)"/>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.1"/>
<PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage" Version="17.14.2"/>
<PackageReference Include="TUnit" Version="0.19.116"/>
<PackageReference Include="Verify.TUnit" Version="30.0.0"/>
<PackageReference Include="TUnit" Version="0.56.5"/>
<PackageReference Include="Verify.TUnit" Version="30.7.2"/>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<RoslynApiVersion>4.11.0</RoslynApiVersion>
<DefineConstants>$(DefineConstants);HASH_BASED_INTERCEPTORS</DefineConstants>
<DefineConstants>$(DefineConstants);HASH_BASED_INTERCEPTORS;Roslyn411</DefineConstants>
</PropertyGroup>

<Import Project="AutoLoggerMessageGenerator.UnitTests.Build.targets" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="AutoLoggerMessageGenerator.UnitTests.Build.targets" />

<PropertyGroup>
<TargetFrameworks>net9.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<RoslynApiVersion>4.14.0</RoslynApiVersion>
<DefineConstants>$(DefineConstants);HASH_BASED_INTERCEPTORS;EMBEDDED;Roslyn414</DefineConstants>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\AutoLoggerMessageGenerator\AutoLoggerMessageGenerator.Roslyn4_14.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<RoslynApiVersion>4.8.0</RoslynApiVersion>
<DefineConstants>$(DefineConstants);PATH_BASED_INTERCEPTORS</DefineConstants>
<DefineConstants>$(DefineConstants);PATH_BASED_INTERCEPTORS;Roslyn408</DefineConstants>
</PropertyGroup>

<Import Project="AutoLoggerMessageGenerator.UnitTests.Build.targets"/>
Expand All @@ -11,4 +11,5 @@
<ProjectReference Include="..\..\src\AutoLoggerMessageGenerator\AutoLoggerMessageGenerator.Roslyn4_8.csproj"
ReferenceOutputAssembly="true"/>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// <auto-generated/>
#nullable enable

using System;

namespace System.Runtime.CompilerServices
{
[global::Microsoft.CodeAnalysis.EmbeddedAttribute]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("AutoLoggerMessageGenerator", "1.2.3.4")]
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
internal sealed class InterceptsLocationAttribute : Attribute
{
public InterceptsLocationAttribute(int version, string data) {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// <auto-generated/>
#nullable enable

using System;

namespace System.Runtime.CompilerServices
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("AutoLoggerMessageGenerator", "1.2.3.4")]
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
internal sealed class InterceptsLocationAttribute : Attribute
{
public InterceptsLocationAttribute(string filePath, int line, int character) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ public async Task Emit_ShouldGenerateValidInterceptorAttribute()
{
var sourceCode = InterceptorAttributeEmitter.Emit();
var configuration = InterceptorConfigurationUtilities.GetInterceptorConfiguration();
var roslynVersion = RoslynConfigurationUtilities.GetRoslynVersion();

await Verify(sourceCode)
.UseTextForParameters(configuration)
.UseTextForParameters($"{configuration}_{roslynVersion}")
.AddCodeGeneratedAttributeScrubber();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <auto-generated/>
// <auto-generated/>
#nullable enable

using System;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// <auto-generated/>
#nullable enable

using System;

namespace Microsoft.Extensions.Logging.AutoLoggerMessage
{
[global::Microsoft.CodeAnalysis.EmbeddedAttribute]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("AutoLoggerMessageGenerator", "1.2.3.4")]
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[System.Diagnostics.DebuggerStepThrough]
internal static class LoggerInterceptors
{
[FakeInterceptableLocation(-1, "ZmlsZSgxLDExKQ==")]
public static void Log_namespace1class1_1_11(this ILogger @logger, string @message)
{
Microsoft.Extensions.Logging.AutoLoggerMessage.AutoLoggerMessage.Log_namespace1class1_1_11(@logger);
}

[FakeInterceptableLocation(-1, "ZmlsZTIoMiwyMik=")]
public static void Log_namespace2class2_2_22(this ILogger @logger, string @message, int @intParam)
{
Microsoft.Extensions.Logging.AutoLoggerMessage.AutoLoggerMessage.Log_namespace2class2_2_22(@logger, @intParam);
}

[FakeInterceptableLocation(-1, "ZmlsZTMoMywzMyk=")]
public static void Log_namespace3class3_3_33(this ILogger @logger, string @message, int @intParam, bool @boolParam, SomeClass @objectParam)
{
Microsoft.Extensions.Logging.AutoLoggerMessage.AutoLoggerMessage.Log_namespace3class3_3_33(@logger, @intParam, @boolParam, @objectParam);
}

}
}
Loading