Skip to content

Add source code to ensure buildability #32043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 26, 2022
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
35 changes: 5 additions & 30 deletions docs/core/extensions/logger-message-generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,32 +65,7 @@ public static partial class Log

You can omit the logging message and <xref:System.String.Empty?displayProperty=nameWithType> will be provided for the message. The state will contain the arguments, formatted as key-value pairs.

```csharp
using System.Text.Json;
using Microsoft.Extensions.Logging;

using ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
builder.AddJsonConsole(
options =>
options.JsonWriterOptions = new JsonWriterOptions
{
Indented = true
}));

ILogger<SampleObject> logger = loggerFactory.CreateLogger<SampleObject>();

logger.CustomLogEvent(LogLevel.Information, "Liana", "Seattle");

public class SampleObject { }

public static partial class Log
{
[LoggerMessage(EventId = 23)]
public static partial void CustomLogEvent(
this ILogger logger, LogLevel logLevel,
string name, string city);
}
```
:::code source="snippets/logging/logger-message-generator/Program.cs":::

Consider the example logging output when using the `JsonConsole` formatter.

Expand All @@ -111,7 +86,7 @@ Consider the example logging output when using the `JsonConsole` formatter.

## Log method constraints

When using the `LoggerMessageAttribute` on logging methods, there are some constraints that must be followed:
When using the `LoggerMessageAttribute` on logging methods, some constraints must be followed:

- Logging methods must be `partial` and return `void`.
- Logging method names must *not* start with an underscore.
Expand Down Expand Up @@ -176,7 +151,7 @@ public static partial void WarningLogMethod(

### Case-insensitive template name support

The generator does case-insensitive comparison between items in message template and argument names in the log message. This means that when the `ILogger` enumerates the state, the argument is picked up by message template, which can make the logs nicer to consume:
The generator does a case-insensitive comparison between items in the message template and argument names in the log message. This means that when the `ILogger` enumerates the state, the argument is picked up by the message template, which can make the logs nicer to consume:

```csharp
public partial class LoggingExample
Expand Down Expand Up @@ -372,8 +347,8 @@ With the advent of C# source generators, writing highly performant logging APIs

- Allows the logging structure to be preserved and enables the exact format syntax required by [Message Templates](https://messagetemplates.org).
- Allows supplying alternative names for the template placeholders and using format specifiers.
- Allows the passing of all original data as-is, without any complication around how it's stored prior to something being done with it (other than creating a `string`).
- Provides logging-specific diagnostics, emits warnings for duplicate event IDs.
- Allows the passing of all original data as-is, without any complication around how it's stored before something is done with it (other than creating a `string`).
- Provides logging-specific diagnostics, and emits warnings for duplicate event IDs.

Additionally, there are benefits over manually using <xref:Microsoft.Extensions.Logging.LoggerMessage.Define%2A?displayProperty=nameWithType>:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Text.Json;
using Microsoft.Extensions.Logging;

using ILoggerFactory loggerFactory = LoggerFactory.Create(
builder =>
builder.AddJsonConsole(
options =>
options.JsonWriterOptions = new JsonWriterOptions()
{
Indented = true
}));

ILogger<SampleObject> logger = loggerFactory.CreateLogger<SampleObject>();
logger.CustomLogEvent(LogLevel.Information, "Liana", "Seattle");

public class SampleObject { }

public static partial class Log
{
[LoggerMessage(EventId = 23)]
public static partial void CustomLogEvent(
this ILogger logger,
LogLevel logLevel,
string name,
string city);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>true</ImplicitUsings>
<RootNamespace>LoggerMessage.Generator</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
</ItemGroup>

</Project>