-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Fix redacting log article: pushed code out to snippets and added missing classes #55006
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
Open
adegeo
wants to merge
6
commits into
main
Choose a base branch
from
adegeo/54570/logging
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a0b6fa1
Pushed code out to snippets. Include missing classes
adegeo 9429c63
Fix code comment; add usings
adegeo 27f0d50
Fix wording
adegeo 4c88c99
Update docs/core/extensions/logging/source-generation.md
adegeo fd8322e
Update docs/core/extensions/logging/source-generation.md
adegeo 4dfef52
Move usings
adegeo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/DynamicLog.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace BasicUsage.Dynamic; | ||
|
|
||
| // <DynamicLogLevel> | ||
| 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); | ||
| } | ||
| // </DynamicLogLevel> |
15 changes: 15 additions & 0 deletions
15
docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/ExtensionLog.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace BasicUsage.Extension; | ||
|
|
||
| // <ExtensionLogMethod> | ||
| 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); | ||
| } | ||
| // </ExtensionLogMethod> |
21 changes: 21 additions & 0 deletions
21
docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/InstanceLog.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace BasicUsage.InstanceField; | ||
|
|
||
| // <InstanceLogWithField> | ||
| 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); | ||
| } | ||
| // </InstanceLogWithField> |
14 changes: 14 additions & 0 deletions
14
docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/PrimaryCtorLog.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace BasicUsage.PrimaryConstructor; | ||
|
|
||
| // <InstanceLogWithPrimaryCtor> | ||
| 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); | ||
| } | ||
| // </InstanceLogWithPrimaryCtor> |
15 changes: 15 additions & 0 deletions
15
docs/core/extensions/logging/snippets/source-generation/csharp/BasicUsage/StaticLog.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace BasicUsage.Static; | ||
|
|
||
| // <StaticLogMethod> | ||
| 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); | ||
| } | ||
| // </StaticLogMethod> |
27 changes: 27 additions & 0 deletions
27
...ions/logging/snippets/source-generation/csharp/LogMethodAnatomy/CaseInsensitiveExample.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace LogMethodAnatomy; | ||
|
|
||
| // <CaseInsensitiveNames> | ||
| 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"); | ||
| } | ||
| } | ||
| // </CaseInsensitiveNames> |
18 changes: 18 additions & 0 deletions
18
...tensions/logging/snippets/source-generation/csharp/LogMethodAnatomy/IndeterminateOrder.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace LogMethodAnatomy; | ||
|
|
||
| public static partial class IndeterminateOrderLog | ||
| { | ||
| // <IndeterminateParameterOrder> | ||
| [LoggerMessage( | ||
| EventId = 110, | ||
| Level = LogLevel.Debug, | ||
| Message = "M1 {Ex3} {Ex2}")] | ||
| static partial void LogMethod( | ||
| Exception ex, | ||
| Exception ex2, | ||
| Exception ex3, | ||
| ILogger logger); | ||
| // </IndeterminateParameterOrder> | ||
| } |
17 changes: 17 additions & 0 deletions
17
.../core/extensions/logging/snippets/source-generation/csharp/LogMethodAnatomy/LogMethods.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace LogMethodAnatomy; | ||
|
|
||
| public static partial class LogMethods | ||
| { | ||
| // <ValidLogMethod> | ||
| // 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); | ||
| // </ValidLogMethod> | ||
| } |
45 changes: 45 additions & 0 deletions
45
...extensions/logging/snippets/source-generation/csharp/MoreLoggingExamples/LoggingSample.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace MoreLoggingExamples; | ||
|
|
||
| // <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); | ||
| } | ||
| } | ||
| // </MoreLoggingExamples> |
1 change: 1 addition & 0 deletions
1
docs/core/extensions/logging/snippets/source-generation/csharp/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Console.WriteLine("Hello, World!"); |
67 changes: 67 additions & 0 deletions
67
...logging/snippets/source-generation/csharp/RedactingSensitiveInformation/RedactionSetup.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| using Microsoft.Extensions.Compliance.Classification; | ||
| using Microsoft.Extensions.Compliance.Redaction; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace RedactingSensitiveInformation; | ||
|
|
||
| // 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"; | ||
|
|
||
| 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<char> input) => Stars.Length; | ||
|
|
||
| public override int Redact(ReadOnlySpan<char> source, Span<char> destination) | ||
| { | ||
| Stars.CopyTo(destination); | ||
| return Stars.Length; | ||
| } | ||
| } | ||
|
|
||
| // <LogPrivateInformation> | ||
| public static partial class LogRedactionExample | ||
| { | ||
| [LoggerMessage(0, LogLevel.Information, "User SSN: {SSN}")] | ||
| public static partial void LogPrivateInformation( | ||
| this ILogger logger, | ||
| [MyTaxonomyClassifications.Private] string SSN); | ||
| } | ||
| // </LogPrivateInformation> | ||
|
|
||
| public static class RedactionSetupExample | ||
| { | ||
| public static void SetupServices() | ||
| { | ||
| // <RedactionSetup> | ||
| var services = new ServiceCollection(); | ||
| services.AddLogging(builder => | ||
| { | ||
| // Enable redaction. | ||
| builder.EnableRedaction(); | ||
| }); | ||
|
|
||
| services.AddRedaction(builder => | ||
| { | ||
| // Configure redactors for your data classifications. | ||
| builder.SetRedactor<StarRedactor>(MyTaxonomyClassifications.Private); | ||
| }); | ||
| // </RedactionSetup> | ||
| } | ||
| } | ||
4 changes: 4 additions & 0 deletions
4
docs/core/extensions/logging/snippets/source-generation/csharp/Usings.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| using Microsoft.Extensions.Compliance.Classification; | ||
| using Microsoft.Extensions.Compliance.Redaction; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; |
17 changes: 17 additions & 0 deletions
17
docs/core/extensions/logging/snippets/source-generation/csharp/csharp.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net10.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Extensions.Compliance.Redaction" Version="10.8.0" /> | ||
| <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.10" /> | ||
| <PackageReference Include="Microsoft.Extensions.Logging" Version="10.0.10" /> | ||
| <PackageReference Include="Microsoft.Extensions.Telemetry" Version="10.8.0" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.