From a0b6fa1b38f4dd9fb96dda268fdc01a1bd2f2256 Mon Sep 17 00:00:00 2001 From: "Andy De George (from Dev Box)" Date: Thu, 23 Jul 2026 11:20:00 -0700 Subject: [PATCH 1/6] Pushed code out to snippets. Include missing classes --- .../csharp/BasicUsage/DynamicLog.cs | 16 ++ .../csharp/BasicUsage/ExtensionLog.cs | 15 ++ .../csharp/BasicUsage/InstanceLog.cs | 21 ++ .../csharp/BasicUsage/PrimaryCtorLog.cs | 14 ++ .../csharp/BasicUsage/StaticLog.cs | 15 ++ .../CaseInsensitiveExample.cs | 27 +++ .../LogMethodAnatomy/IndeterminateOrder.cs | 18 ++ .../csharp/LogMethodAnatomy/LogMethods.cs | 17 ++ .../MoreLoggingExamples/LoggingSample.cs | 45 +++++ .../source-generation/csharp/Program.cs | 1 + .../RedactionSetup.cs | 67 +++++++ .../source-generation/csharp/csharp.csproj | 17 ++ .../extensions/logging/source-generation.md | 187 ++---------------- 13 files changed, 287 insertions(+), 173 deletions(-) create mode 100644 docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/DynamicLog.cs create mode 100644 docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/ExtensionLog.cs create mode 100644 docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/InstanceLog.cs create mode 100644 docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/PrimaryCtorLog.cs create mode 100644 docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/StaticLog.cs create mode 100644 docs/core/extensions/logging/snippets/source-generation/csharp/LogMethodAnatomy/CaseInsensitiveExample.cs create mode 100644 docs/core/extensions/logging/snippets/source-generation/csharp/LogMethodAnatomy/IndeterminateOrder.cs create mode 100644 docs/core/extensions/logging/snippets/source-generation/csharp/LogMethodAnatomy/LogMethods.cs create mode 100644 docs/core/extensions/logging/snippets/source-generation/csharp/MoreLoggingExamples/LoggingSample.cs create mode 100644 docs/core/extensions/logging/snippets/source-generation/csharp/Program.cs create mode 100644 docs/core/extensions/logging/snippets/source-generation/csharp/RedactingSensitiveInformation/RedactionSetup.cs create mode 100644 docs/core/extensions/logging/snippets/source-generation/csharp/csharp.csproj diff --git a/docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/DynamicLog.cs b/docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/DynamicLog.cs new file mode 100644 index 0000000000000..ec0aec3f505a3 --- /dev/null +++ b/docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/DynamicLog.cs @@ -0,0 +1,16 @@ +using Microsoft.Extensions.Logging; + +namespace BasicUsage.Dynamic; + +// +public static partial class Log +{ + [LoggerMessage( + EventId = 0, + Message = "Could not open socket to `{HostName}`")] + public static partial void CouldNotOpenSocket( + ILogger logger, + LogLevel level, + string hostName); +} +// diff --git a/docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/ExtensionLog.cs b/docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/ExtensionLog.cs new file mode 100644 index 0000000000000..0ff5bfd2455ee --- /dev/null +++ b/docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/ExtensionLog.cs @@ -0,0 +1,15 @@ +using Microsoft.Extensions.Logging; + +namespace BasicUsage.Extension; + +// +public static partial class Log +{ + [LoggerMessage( + EventId = 0, + Level = LogLevel.Critical, + Message = "Could not open socket to `{HostName}`")] + public static partial void CouldNotOpenSocket( + this ILogger logger, string hostName); +} +// diff --git a/docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/InstanceLog.cs b/docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/InstanceLog.cs new file mode 100644 index 0000000000000..5ce993f48fb75 --- /dev/null +++ b/docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/InstanceLog.cs @@ -0,0 +1,21 @@ +using Microsoft.Extensions.Logging; + +namespace BasicUsage.InstanceField; + +// +public partial class InstanceLoggingExample +{ + private readonly ILogger _logger; + + public InstanceLoggingExample(ILogger logger) + { + _logger = logger; + } + + [LoggerMessage( + EventId = 0, + Level = LogLevel.Critical, + Message = "Could not open socket to `{HostName}`")] + public partial void CouldNotOpenSocket(string hostName); +} +// diff --git a/docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/PrimaryCtorLog.cs b/docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/PrimaryCtorLog.cs new file mode 100644 index 0000000000000..7ae8459b3f780 --- /dev/null +++ b/docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/PrimaryCtorLog.cs @@ -0,0 +1,14 @@ +using Microsoft.Extensions.Logging; + +namespace BasicUsage.PrimaryConstructor; + +// +public partial class InstanceLoggingExample(ILogger logger) +{ + [LoggerMessage( + EventId = 0, + Level = LogLevel.Critical, + Message = "Could not open socket to `{HostName}`")] + public partial void CouldNotOpenSocket(string hostName); +} +// diff --git a/docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/StaticLog.cs b/docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/StaticLog.cs new file mode 100644 index 0000000000000..fc4cb8b9c5195 --- /dev/null +++ b/docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/StaticLog.cs @@ -0,0 +1,15 @@ +using Microsoft.Extensions.Logging; + +namespace BasicUsage.Static; + +// +public static partial class Log +{ + [LoggerMessage( + EventId = 0, + Level = LogLevel.Critical, + Message = "Could not open socket to `{HostName}`")] + public static partial void CouldNotOpenSocket( + ILogger logger, string hostName); +} +// diff --git a/docs/core/extensions/logging/snippets/source-generation/csharp/LogMethodAnatomy/CaseInsensitiveExample.cs b/docs/core/extensions/logging/snippets/source-generation/csharp/LogMethodAnatomy/CaseInsensitiveExample.cs new file mode 100644 index 0000000000000..f3d3cf430d886 --- /dev/null +++ b/docs/core/extensions/logging/snippets/source-generation/csharp/LogMethodAnatomy/CaseInsensitiveExample.cs @@ -0,0 +1,27 @@ +using Microsoft.Extensions.Logging; + +namespace LogMethodAnatomy; + +// +public partial class LoggingExample +{ + private readonly ILogger _logger; + + public LoggingExample(ILogger logger) + { + _logger = logger; + } + + [LoggerMessage( + EventId = 10, + Level = LogLevel.Information, + Message = "Welcome to {City} {Province}!")] + public partial void LogMethodSupportsPascalCasingOfNames( + string city, string province); + + public void TestLogging() + { + LogMethodSupportsPascalCasingOfNames("Vancouver", "BC"); + } +} +// diff --git a/docs/core/extensions/logging/snippets/source-generation/csharp/LogMethodAnatomy/IndeterminateOrder.cs b/docs/core/extensions/logging/snippets/source-generation/csharp/LogMethodAnatomy/IndeterminateOrder.cs new file mode 100644 index 0000000000000..f6801bbd0ffc2 --- /dev/null +++ b/docs/core/extensions/logging/snippets/source-generation/csharp/LogMethodAnatomy/IndeterminateOrder.cs @@ -0,0 +1,18 @@ +using Microsoft.Extensions.Logging; + +namespace LogMethodAnatomy; + +public static partial class IndeterminateOrderLog +{ + // + [LoggerMessage( + EventId = 110, + Level = LogLevel.Debug, + Message = "M1 {Ex3} {Ex2}")] + static partial void LogMethod( + Exception ex, + Exception ex2, + Exception ex3, + ILogger logger); + // +} diff --git a/docs/core/extensions/logging/snippets/source-generation/csharp/LogMethodAnatomy/LogMethods.cs b/docs/core/extensions/logging/snippets/source-generation/csharp/LogMethodAnatomy/LogMethods.cs new file mode 100644 index 0000000000000..61cc61a771beb --- /dev/null +++ b/docs/core/extensions/logging/snippets/source-generation/csharp/LogMethodAnatomy/LogMethods.cs @@ -0,0 +1,17 @@ +using Microsoft.Extensions.Logging; + +namespace LogMethodAnatomy; + +public static partial class LogMethods +{ + // + // This is a valid attribute usage + [LoggerMessage( + EventId = 110, Level = LogLevel.Debug, Message = "M1 {Ex3} {Ex2}")] + public static partial void ValidLogMethod( + ILogger logger, + Exception ex, + Exception ex2, + Exception ex3); + // +} diff --git a/docs/core/extensions/logging/snippets/source-generation/csharp/MoreLoggingExamples/LoggingSample.cs b/docs/core/extensions/logging/snippets/source-generation/csharp/MoreLoggingExamples/LoggingSample.cs new file mode 100644 index 0000000000000..d67f705765b02 --- /dev/null +++ b/docs/core/extensions/logging/snippets/source-generation/csharp/MoreLoggingExamples/LoggingSample.cs @@ -0,0 +1,45 @@ +using Microsoft.Extensions.Logging; + +namespace MoreLoggingExamples; + +// +public partial class LoggingSample +{ + private readonly ILogger _logger; + + public LoggingSample(ILogger logger) + { + _logger = logger; + } + + [LoggerMessage( + EventId = 20, + Level = LogLevel.Critical, + Message = "Value is {Value:E}")] + public static partial void UsingFormatSpecifier( + ILogger logger, double value); + + [LoggerMessage( + EventId = 9, + Level = LogLevel.Trace, + Message = "Fixed message", + EventName = "CustomEventName")] + public partial void LogWithCustomEventName(); + + [LoggerMessage( + EventId = 10, + Message = "Welcome to {City} {Province}!")] + public partial void LogWithDynamicLogLevel( + string city, LogLevel level, string province); + + public void TestLogging() + { + LogWithCustomEventName(); + + LogWithDynamicLogLevel("Vancouver", LogLevel.Warning, "BC"); + LogWithDynamicLogLevel("Vancouver", LogLevel.Information, "BC"); + + UsingFormatSpecifier(_logger, 12345.6789); + } +} +// diff --git a/docs/core/extensions/logging/snippets/source-generation/csharp/Program.cs b/docs/core/extensions/logging/snippets/source-generation/csharp/Program.cs new file mode 100644 index 0000000000000..1bc52a60a8ba3 --- /dev/null +++ b/docs/core/extensions/logging/snippets/source-generation/csharp/Program.cs @@ -0,0 +1 @@ +Console.WriteLine("Hello, World!"); diff --git a/docs/core/extensions/logging/snippets/source-generation/csharp/RedactingSensitiveInformation/RedactionSetup.cs b/docs/core/extensions/logging/snippets/source-generation/csharp/RedactingSensitiveInformation/RedactionSetup.cs new file mode 100644 index 0000000000000..76ff7774ba413 --- /dev/null +++ b/docs/core/extensions/logging/snippets/source-generation/csharp/RedactingSensitiveInformation/RedactionSetup.cs @@ -0,0 +1,67 @@ +using Microsoft.Extensions.Compliance.Classification; +using Microsoft.Extensions.Compliance.Redaction; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace RedactingSensitiveInformation; + +// Supporting types copied from the data classification taxonomy pattern. +// DataClassification.Private is used as a value for SetRedactor(). +// DataClassification.PrivateAttribute (as [MyTaxonomyClassifications.Private]) is used to +// annotate logging parameters. +public static class MyTaxonomyClassifications +{ + private static string Name => "MyTaxonomy"; + + public static DataClassification Private { get; } = new(Name, nameof(Private)); + + // Accessible as [MyTaxonomyClassifications.Private] due to C# attribute name convention. + public sealed class PrivateAttribute : DataClassificationAttribute + { + public PrivateAttribute() : base(MyTaxonomyClassifications.Private) { } + } +} + +public sealed class StarRedactor : Redactor +{ + private const string Stars = "*****"; + + public override int GetRedactedLength(ReadOnlySpan input) => Stars.Length; + + public override int Redact(ReadOnlySpan source, Span destination) + { + Stars.CopyTo(destination); + return Stars.Length; + } +} + +// +public static partial class LogRedactionExample +{ + [LoggerMessage(0, LogLevel.Information, "User SSN: {SSN}")] + public static partial void LogPrivateInformation( + this ILogger logger, + [MyTaxonomyClassifications.Private] string SSN); +} +// + +public static class RedactionSetupExample +{ + public static void SetupServices() + { + // + var services = new ServiceCollection(); + services.AddLogging(builder => + { + // Enable redaction. + builder.EnableRedaction(); + }); + + services.AddRedaction(builder => + { + // Configure redactors for your data classifications. + builder.SetRedactor(MyTaxonomyClassifications.Private); + }); + // + } +} diff --git a/docs/core/extensions/logging/snippets/source-generation/csharp/csharp.csproj b/docs/core/extensions/logging/snippets/source-generation/csharp/csharp.csproj new file mode 100644 index 0000000000000..304a19fa28b68 --- /dev/null +++ b/docs/core/extensions/logging/snippets/source-generation/csharp/csharp.csproj @@ -0,0 +1,17 @@ + + + + Exe + net10.0 + enable + enable + + + + + + + + + + diff --git a/docs/core/extensions/logging/source-generation.md b/docs/core/extensions/logging/source-generation.md index 2b86442a00ad7..485deeff27b49 100644 --- a/docs/core/extensions/logging/source-generation.md +++ b/docs/core/extensions/logging/source-generation.md @@ -15,64 +15,19 @@ The source generator is triggered when [!TIP] > The order of the parameters on a log method isn't required to correspond to the order of the template placeholders. Instead, the placeholder names in the template are expected to match the parameters. Consider the following `JsonConsole` output and the order of the errors. @@ -241,47 +148,7 @@ The following samples demonstrate how to retrieve the event name, set the log le - `LogWithDynamicLogLevel`: Set log level dynamically, to allow log level to be set based on configuration input. - `UsingFormatSpecifier`: Use format specifiers to format logging parameters. -```csharp -public partial class LoggingSample -{ - private readonly ILogger _logger; - - public LoggingSample(ILogger logger) - { - _logger = logger; - } - - [LoggerMessage( - EventId = 20, - Level = LogLevel.Critical, - Message = "Value is {Value:E}")] - public static partial void UsingFormatSpecifier( - ILogger logger, double value); - - [LoggerMessage( - EventId = 9, - Level = LogLevel.Trace, - Message = "Fixed message", - EventName = "CustomEventName")] - public partial void LogWithCustomEventName(); - - [LoggerMessage( - EventId = 10, - Message = "Welcome to {City} {Province}!")] - public partial void LogWithDynamicLogLevel( - string city, LogLevel level, string province); - - public void TestLogging() - { - LogWithCustomEventName(); - - LogWithDynamicLogLevel("Vancouver", LogLevel.Warning, "BC"); - LogWithDynamicLogLevel("Vancouver", LogLevel.Information, "BC"); - - UsingFormatSpecifier(logger, 12345.6789); - } -} -``` +:::code language="csharp" source="./snippets/source-generation/csharp/MoreLoggingExamples/LoggingSample.cs" id="MoreLoggingExamples"::: Consider the example logging output when using the `SimpleConsole` formatter: @@ -363,37 +230,11 @@ To use redaction with source-generated logging methods, you should: For example, if you have a log message that has a parameter that is considered private: -```csharp -[LoggerMessage(0, LogLevel.Information, "User SSN: {SSN}")] -public static partial void LogPrivateInformation( - this ILogger logger, - [MyTaxonomyClassifications.Private] string SSN); -``` +:::code language="csharp" source="./snippets/source-generation/csharp/RedactingSensitiveInformation/RedactionSetup.cs" id="LogPrivateInformation"::: You'll need to have a setting similar to this: -```csharp -using Microsoft.Extensions.Telemetry; -using Microsoft.Extensions.Compliance.Redaction; - -var services = new ServiceCollection(); -services.AddLogging(builder => -{ - // Enable redaction. - builder.EnableRedaction(); -}); - -services.AddRedaction(builder => -{ - // configure redactors for your data classifications - builder.SetRedactor(MyTaxonomyClassifications.Private); -}); - -public void TestLogging() -{ - LogPrivateInformation("MySSN"); -} -``` +:::code language="csharp" source="./snippets/source-generation/csharp/RedactingSensitiveInformation/RedactionSetup.cs" id="RedactionSetup"::: The output should be like this: From 9429c631c86842ecdce8973ebc3593ab04d3c4b5 Mon Sep 17 00:00:00 2001 From: "Andy De George (from Dev Box)" Date: Thu, 23 Jul 2026 12:38:10 -0700 Subject: [PATCH 2/6] Fix code comment; add usings --- .../RedactingSensitiveInformation/RedactionSetup.cs | 8 ++++---- .../snippets/source-generation/csharp/Usings.cs | 12 ++++++++++++ docs/core/extensions/logging/source-generation.md | 13 ++++++++++--- 3 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 docs/core/extensions/logging/snippets/source-generation/csharp/Usings.cs diff --git a/docs/core/extensions/logging/snippets/source-generation/csharp/RedactingSensitiveInformation/RedactionSetup.cs b/docs/core/extensions/logging/snippets/source-generation/csharp/RedactingSensitiveInformation/RedactionSetup.cs index 76ff7774ba413..e411874b581b8 100644 --- a/docs/core/extensions/logging/snippets/source-generation/csharp/RedactingSensitiveInformation/RedactionSetup.cs +++ b/docs/core/extensions/logging/snippets/source-generation/csharp/RedactingSensitiveInformation/RedactionSetup.cs @@ -5,10 +5,10 @@ namespace RedactingSensitiveInformation; -// Supporting types copied from the data classification taxonomy pattern. -// DataClassification.Private is used as a value for SetRedactor(). -// DataClassification.PrivateAttribute (as [MyTaxonomyClassifications.Private]) is used to -// annotate logging parameters. +// MyTaxonomyClassifications.Private serves two roles: +// - As a DataClassification value passed to SetRedactor(). +// - As [MyTaxonomyClassifications.Private] on logging parameters, resolved via the +// nested PrivateAttribute class (C# drops the "Attribute" suffix in attribute syntax). public static class MyTaxonomyClassifications { private static string Name => "MyTaxonomy"; diff --git a/docs/core/extensions/logging/snippets/source-generation/csharp/Usings.cs b/docs/core/extensions/logging/snippets/source-generation/csharp/Usings.cs new file mode 100644 index 0000000000000..3f6938a458454 --- /dev/null +++ b/docs/core/extensions/logging/snippets/source-generation/csharp/Usings.cs @@ -0,0 +1,12 @@ +#pragma warning disable CS0105 // Using directive appeared previously in this namespace +// +using Microsoft.Extensions.Logging; +// + +// +using Microsoft.Extensions.Compliance.Classification; +using Microsoft.Extensions.Compliance.Redaction; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +// +#pragma warning restore CS0105 // Using directive appeared previously in this namespace diff --git a/docs/core/extensions/logging/source-generation.md b/docs/core/extensions/logging/source-generation.md index 485deeff27b49..8812478ef6246 100644 --- a/docs/core/extensions/logging/source-generation.md +++ b/docs/core/extensions/logging/source-generation.md @@ -13,6 +13,11 @@ The source generator is triggered when [IMPORTANT] +> The examples in this article use the following namespace: +> +> :::code language="csharp" source="./snippets/source-generation/csharp/Usings.cs" id="CommonUsings"::: + To use the `LoggerMessageAttribute`, the consuming class and method need to be `partial`. The code generator is triggered at compile time, and generates an implementation of the `partial` method. :::code language="csharp" source="./snippets/source-generation/csharp/BasicUsage/StaticLog.cs" id="StaticLogMethod"::: @@ -221,6 +226,10 @@ The [Microsoft.Extensions.Telemetry](https://www.nuget.org/packages/Microsoft.Ex To enable redaction, use the [Microsoft.Extensions.Compliance.Redaction](https://www.nuget.org/packages/Microsoft.Extensions.Compliance.Redaction) library. This library provides **redactors**—components that transform sensitive data (for example, by erasing, masking, or hashing it) so that it's safe to output. Redactors are selected based on **data classification**, which lets you label data according to its sensitivity (such as personal, private, or public). +The redaction examples also require the following additional namespaces: + +:::code language="csharp" source="./snippets/source-generation/csharp/Usings.cs" id="RedactionUsings"::: + To use redaction with source-generated logging methods, you should: 1. Classify your sensitive data using a data classification system. @@ -236,9 +245,7 @@ You'll need to have a setting similar to this: :::code language="csharp" source="./snippets/source-generation/csharp/RedactingSensitiveInformation/RedactionSetup.cs" id="RedactionSetup"::: -The output should be like this: - -`User SSN: *****` +The output should be like this: `User SSN: *****`. This approach ensures that only redacted data is logged, even when using compile-time generated logging APIs. You can use different redactors for different data types or classifications, and update your redaction logic centrally. From 27f0d50e06865b7164e1964016fcef12a397db0e Mon Sep 17 00:00:00 2001 From: "Andy De George (from Dev Box)" Date: Thu, 23 Jul 2026 12:49:38 -0700 Subject: [PATCH 3/6] Fix wording --- docs/core/extensions/logging/source-generation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core/extensions/logging/source-generation.md b/docs/core/extensions/logging/source-generation.md index 8812478ef6246..0b0756526a94a 100644 --- a/docs/core/extensions/logging/source-generation.md +++ b/docs/core/extensions/logging/source-generation.md @@ -226,7 +226,7 @@ The [Microsoft.Extensions.Telemetry](https://www.nuget.org/packages/Microsoft.Ex To enable redaction, use the [Microsoft.Extensions.Compliance.Redaction](https://www.nuget.org/packages/Microsoft.Extensions.Compliance.Redaction) library. This library provides **redactors**—components that transform sensitive data (for example, by erasing, masking, or hashing it) so that it's safe to output. Redactors are selected based on **data classification**, which lets you label data according to its sensitivity (such as personal, private, or public). -The redaction examples also require the following additional namespaces: +The redaction examples require the following namespaces: :::code language="csharp" source="./snippets/source-generation/csharp/Usings.cs" id="RedactionUsings"::: From 4c88c99503c344d42a5e72052545e6f2d96de794 Mon Sep 17 00:00:00 2001 From: "Andy (Steve) De George" <67293991+adegeo@users.noreply.github.com> Date: Thu, 23 Jul 2026 15:11:31 -0700 Subject: [PATCH 4/6] Update docs/core/extensions/logging/source-generation.md Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- docs/core/extensions/logging/source-generation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core/extensions/logging/source-generation.md b/docs/core/extensions/logging/source-generation.md index 0b0756526a94a..ff921dd818cca 100644 --- a/docs/core/extensions/logging/source-generation.md +++ b/docs/core/extensions/logging/source-generation.md @@ -18,7 +18,7 @@ The source generator is triggered when > :::code language="csharp" source="./snippets/source-generation/csharp/Usings.cs" id="CommonUsings"::: -To use the `LoggerMessageAttribute`, the consuming class and method need to be `partial`. The code generator is triggered at compile time, and generates an implementation of the `partial` method. +To use the `LoggerMessageAttribute`, the consuming class and method need to be `partial`. The code generator is triggered at compile time and generates an implementation of the `partial` method. :::code language="csharp" source="./snippets/source-generation/csharp/BasicUsage/StaticLog.cs" id="StaticLogMethod"::: From fd8322e9cf43c5462cd46ed2a05f058189e6e5a1 Mon Sep 17 00:00:00 2001 From: "Andy (Steve) De George" <67293991+adegeo@users.noreply.github.com> Date: Thu, 23 Jul 2026 15:11:39 -0700 Subject: [PATCH 5/6] Update docs/core/extensions/logging/source-generation.md Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- docs/core/extensions/logging/source-generation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core/extensions/logging/source-generation.md b/docs/core/extensions/logging/source-generation.md index ff921dd818cca..36749188a36e6 100644 --- a/docs/core/extensions/logging/source-generation.md +++ b/docs/core/extensions/logging/source-generation.md @@ -13,7 +13,7 @@ The source generator is triggered when [IMPORTANT] +> [!TIP] > The examples in this article use the following namespace: > > :::code language="csharp" source="./snippets/source-generation/csharp/Usings.cs" id="CommonUsings"::: From 4dfef52c71ad1709485c94eaad7ac54eee2cf580 Mon Sep 17 00:00:00 2001 From: "Andy De George (from Dev Box)" Date: Thu, 23 Jul 2026 15:13:08 -0700 Subject: [PATCH 6/6] Move usings --- .../logging/snippets/source-generation/csharp/Usings.cs | 8 -------- docs/core/extensions/logging/source-generation.md | 6 ++++-- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/docs/core/extensions/logging/snippets/source-generation/csharp/Usings.cs b/docs/core/extensions/logging/snippets/source-generation/csharp/Usings.cs index 3f6938a458454..f045a7ab8cd69 100644 --- a/docs/core/extensions/logging/snippets/source-generation/csharp/Usings.cs +++ b/docs/core/extensions/logging/snippets/source-generation/csharp/Usings.cs @@ -1,12 +1,4 @@ -#pragma warning disable CS0105 // Using directive appeared previously in this namespace -// -using Microsoft.Extensions.Logging; -// - -// using Microsoft.Extensions.Compliance.Classification; using Microsoft.Extensions.Compliance.Redaction; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -// -#pragma warning restore CS0105 // Using directive appeared previously in this namespace diff --git a/docs/core/extensions/logging/source-generation.md b/docs/core/extensions/logging/source-generation.md index 36749188a36e6..3f50eba2271c1 100644 --- a/docs/core/extensions/logging/source-generation.md +++ b/docs/core/extensions/logging/source-generation.md @@ -16,7 +16,9 @@ The source generator is triggered when [!TIP] > The examples in this article use the following namespace: > -> :::code language="csharp" source="./snippets/source-generation/csharp/Usings.cs" id="CommonUsings"::: +> ```csharp +> using Microsoft.Extensions.Logging; +> ``` To use the `LoggerMessageAttribute`, the consuming class and method need to be `partial`. The code generator is triggered at compile time and generates an implementation of the `partial` method. @@ -228,7 +230,7 @@ To enable redaction, use the [Microsoft.Extensions.Compliance.Redaction](https:/ The redaction examples require the following namespaces: -:::code language="csharp" source="./snippets/source-generation/csharp/Usings.cs" id="RedactionUsings"::: +:::code language="csharp" source="./snippets/source-generation/csharp/Usings.cs"::: To use redaction with source-generated logging methods, you should: