-
UPD: code snippet was updated Hello! Thank you for your very useful library! I tried to use it, but unfortunately it doesn’t seem to work as expected. Here’s a minimal example with just two files: a .csproj and a .cs file with a benchmark. Could you please advise what might be wrong or missing? csproj declaration: <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>12</LangVersion>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.1" />
<PackageReference Include="stbychkov.AutoLoggerMessage" Version="1.0.10">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project> using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Microsoft.Extensions.Logging;
BenchmarkRunner.Run<AutoLoggingBenchmark>();
[MemoryDiagnoser]
[WarmupCount(50)]
[IterationCount(50)]
[InvocationCount(100)]
public class AutoLoggingBenchmark
{
private readonly ILogger _logger;
public AutoLoggingBenchmark()
{
var loggerFactory = LoggerFactory.Create(builder =>
builder.AddSimpleConsole().SetMinimumLevel(LogLevel.Trace)
);
_logger = loggerFactory.CreateLogger<Program>();
}
[Benchmark(Baseline = true)]
public void BuiltInLogging()
{
for (int t = 0; t < 100; t++)
LoggerExtensions.LogDebug(_logger, "Test log started at {time} with {Id}", DateTime.Now, t);
}
[Benchmark]
public void DefinedLoggerLogging()
{
for (int t = 0; t < 100; t++)
DefinedMessagesLogger.DefinedDebug(_logger, DateTime.Now, t);
}
[Benchmark]
public void AutoLoggerMessageLogging()
{
for (int t = 0; t < 100; t++)
LoggerExtensions.LogDebug(_logger, "Test log started at {time} with {Id}", DateTime.Now, t);
}
}
public static partial class DefinedMessagesLogger
{
[LoggerMessage(Level = LogLevel.Debug, Message = "Test log started at {time} with {Id}")]
public static partial void DefinedDebug(ILogger logger, DateTime time, int id);
} Generated file InterceptorAttribute.g.cs: // <auto-generated/>
#nullable enable
using System;
namespace System.Runtime.CompilerServices
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("AutoLoggerMessageGenerator", "1.0.10.0")]
[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) {}
}
} Results:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hey, @MiKorH! Thanks for asking the question. There are a couple of things going on here: 1. Project setup You’ve manually specified what to include from the library: <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> That’s almost right, but you're missing So you should update it to: <IncludeAssets>compile; runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> Alternatively, if you don’t need fine-grained control, you can skip the <PackageReference Include="stbychkov.AutoLoggerMessage" Version="1.0.10" /> 2. Benchmark method You're currently calling the extension method directly: LoggerExtensions.LogDebug(_logger, "Test log started at {time} with {Id}", DateTime.Now, t); But the library is designed to detect usage of the standard So to trigger source generation correctly, your benchmark for _logger.LogDebug("Test log started at {time} with {Id}", DateTime.Now, t); 3. Benchmark isolation It’s a good idea to isolate your benchmark from external dependencies like
You can check out this example benchmark for the reference. Hope that helps! |
Beta Was this translation helpful? Give feedback.
Hey, @MiKorH!
Thanks for asking the question. There are a couple of things going on here:
1. Project setup
You’ve manually specified what to include from the library:
That’s almost right, but you're missing
compile
, which is important here. The*.BuildOutput.dll
library contains theGenericLoggerExtensions
class, and it needs to be available at compile time.So you should update it to:
Alternatively, if you don’t need fine-grained control, you can skip the
IncludeAssets
altogether - it works…