Skip to content

Commit fcb6b40

Browse files
authored
Add an example for LoggerMessageAttribute (#2792)
1 parent 8138e5e commit fcb6b40

File tree

5 files changed

+101
-2
lines changed

5 files changed

+101
-2
lines changed

docs/Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<PropertyGroup>
55
<OutputType>Exe</OutputType>
66
<!-- https://dotnet.microsoft.com/download/dotnet-core -->
7-
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
7+
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
88
<!-- https://dotnet.microsoft.com/download/dotnet-framework -->
99
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);net461;net462;net47;net471;net472;net48</TargetFrameworks>
1010
</PropertyGroup>
@@ -14,6 +14,6 @@
1414
Please sort alphabetically.
1515
Refer to https://docs.microsoft.com/nuget/concepts/package-versioning for semver syntax.
1616
-->
17-
<MicrosoftExtensionsLoggingPkgVer>[5.0.0,6.0)</MicrosoftExtensionsLoggingPkgVer>
17+
<MicrosoftExtensionsLoggingPkgVer>[6.0.0,)</MicrosoftExtensionsLoggingPkgVer>
1818
</PropertyGroup>
1919
</Project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// <copyright file="FoodSupplyLogs.cs" company="OpenTelemetry Authors">
2+
// Copyright The OpenTelemetry Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// </copyright>
16+
17+
using Microsoft.Extensions.Logging;
18+
19+
public static partial class FoodSupplyLogs
20+
{
21+
[LoggerMessage(
22+
EventId = 1,
23+
Level = LogLevel.Information,
24+
Message = "Food `{name}` price changed to `{price}`.")]
25+
public static partial void FoodPriceChanged(this ILogger logger, string name, double price);
26+
27+
[LoggerMessage(
28+
EventId = 2,
29+
Message = "A `{productType}` recall notice was published for `{brandName} {productDescription}` produced by `{companyName}` ({recallReasonDescription}).")]
30+
public static partial void FoodRecallNotice(
31+
this ILogger logger,
32+
LogLevel logLevel,
33+
string brandName,
34+
string productDescription,
35+
string productType,
36+
string recallReasonDescription,
37+
string companyName);
38+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// <copyright file="Program.cs" company="OpenTelemetry Authors">
2+
// Copyright The OpenTelemetry Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// </copyright>
16+
17+
using Microsoft.Extensions.Logging;
18+
using OpenTelemetry.Logs;
19+
20+
public class Program
21+
{
22+
public static void Main()
23+
{
24+
using var loggerFactory = LoggerFactory.Create(builder =>
25+
{
26+
builder.AddOpenTelemetry(options =>
27+
{
28+
options.IncludeScopes = true;
29+
options.ParseStateValues = true;
30+
options.IncludeFormattedMessage = true;
31+
options.AddConsoleExporter();
32+
});
33+
});
34+
35+
var logger = loggerFactory.CreateLogger<Program>();
36+
37+
logger.FoodPriceChanged("artichoke", 9.99);
38+
39+
logger.FoodRecallNotice(
40+
logLevel: LogLevel.Critical,
41+
brandName: "Contoso",
42+
productDescription: "Salads",
43+
productType: "Food & Beverages",
44+
recallReasonDescription: "due to a possible health risk from Listeria monocytogenes",
45+
companyName: "Contoso Fresh Vegetables, Inc.");
46+
}
47+
}

docs/logs/source-generation/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Compile-time logging source generation
2+
3+
.NET 6 has introduced a more usable and performant logging solution with
4+
[`LoggerMessageAttribute`](https://docs.microsoft.com/dotnet/api/microsoft.extensions.logging.loggermessageattribute).
5+
6+
## References
7+
8+
* [Compile-time logging source generation](https://docs.microsoft.com/dotnet/core/extensions/logger-message-generator)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<ItemGroup>
3+
<PackageReference Include="Microsoft.Extensions.Logging" Version="$(MicrosoftExtensionsLoggingPkgVer)" />
4+
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Console\OpenTelemetry.Exporter.Console.csproj" />
5+
</ItemGroup>
6+
</Project>

0 commit comments

Comments
 (0)