Add an example for LoggerMessageAttribute (#2792)
This commit is contained in:
parent
8138e5ebc5
commit
fcb6b40bfa
|
|
@ -4,7 +4,7 @@
|
|||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<!-- https://dotnet.microsoft.com/download/dotnet-core -->
|
||||
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
|
||||
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
|
||||
<!-- https://dotnet.microsoft.com/download/dotnet-framework -->
|
||||
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);net461;net462;net47;net471;net472;net48</TargetFrameworks>
|
||||
</PropertyGroup>
|
||||
|
|
@ -14,6 +14,6 @@
|
|||
Please sort alphabetically.
|
||||
Refer to https://docs.microsoft.com/nuget/concepts/package-versioning for semver syntax.
|
||||
-->
|
||||
<MicrosoftExtensionsLoggingPkgVer>[5.0.0,6.0)</MicrosoftExtensionsLoggingPkgVer>
|
||||
<MicrosoftExtensionsLoggingPkgVer>[6.0.0,)</MicrosoftExtensionsLoggingPkgVer>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,38 @@
|
|||
// <copyright file="FoodSupplyLogs.cs" company="OpenTelemetry Authors">
|
||||
// Copyright The OpenTelemetry Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// </copyright>
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
public static partial class FoodSupplyLogs
|
||||
{
|
||||
[LoggerMessage(
|
||||
EventId = 1,
|
||||
Level = LogLevel.Information,
|
||||
Message = "Food `{name}` price changed to `{price}`.")]
|
||||
public static partial void FoodPriceChanged(this ILogger logger, string name, double price);
|
||||
|
||||
[LoggerMessage(
|
||||
EventId = 2,
|
||||
Message = "A `{productType}` recall notice was published for `{brandName} {productDescription}` produced by `{companyName}` ({recallReasonDescription}).")]
|
||||
public static partial void FoodRecallNotice(
|
||||
this ILogger logger,
|
||||
LogLevel logLevel,
|
||||
string brandName,
|
||||
string productDescription,
|
||||
string productType,
|
||||
string recallReasonDescription,
|
||||
string companyName);
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
// <copyright file="Program.cs" company="OpenTelemetry Authors">
|
||||
// Copyright The OpenTelemetry Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// </copyright>
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OpenTelemetry.Logs;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static void Main()
|
||||
{
|
||||
using var loggerFactory = LoggerFactory.Create(builder =>
|
||||
{
|
||||
builder.AddOpenTelemetry(options =>
|
||||
{
|
||||
options.IncludeScopes = true;
|
||||
options.ParseStateValues = true;
|
||||
options.IncludeFormattedMessage = true;
|
||||
options.AddConsoleExporter();
|
||||
});
|
||||
});
|
||||
|
||||
var logger = loggerFactory.CreateLogger<Program>();
|
||||
|
||||
logger.FoodPriceChanged("artichoke", 9.99);
|
||||
|
||||
logger.FoodRecallNotice(
|
||||
logLevel: LogLevel.Critical,
|
||||
brandName: "Contoso",
|
||||
productDescription: "Salads",
|
||||
productType: "Food & Beverages",
|
||||
recallReasonDescription: "due to a possible health risk from Listeria monocytogenes",
|
||||
companyName: "Contoso Fresh Vegetables, Inc.");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
# Compile-time logging source generation
|
||||
|
||||
.NET 6 has introduced a more usable and performant logging solution with
|
||||
[`LoggerMessageAttribute`](https://docs.microsoft.com/dotnet/api/microsoft.extensions.logging.loggermessageattribute).
|
||||
|
||||
## References
|
||||
|
||||
* [Compile-time logging source generation](https://docs.microsoft.com/dotnet/core/extensions/logger-message-generator)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="$(MicrosoftExtensionsLoggingPkgVer)" />
|
||||
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Console\OpenTelemetry.Exporter.Console.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Loading…
Reference in New Issue