diff --git a/docs/core/enrichment/custom-enricher.md b/docs/core/enrichment/custom-enricher.md new file mode 100644 index 0000000000000..4157f8cfef936 --- /dev/null +++ b/docs/core/enrichment/custom-enricher.md @@ -0,0 +1,93 @@ +--- +title: Custom log enricher +description: Learn how to use the custom log enricher in .NET. +ms.date: 10/13/2025 +--- + +# Custom log enricher + +You can easily create a custom enricher by creating a class that implements the interface. +After the class is created, you register it with . +Once registered, the logging infrastructure automatically calls the `Enrich()` method exactly once on every registered enricher for each log message produced. + +## Install the package + +To get started, install the [📦 Microsoft.Extensions.Telemetry.Abstractions](https://www.nuget.org/packages/Microsoft.Extensions.Telemetry.Abstractions) NuGet package: + +### [.NET CLI](#tab/dotnet-cli) + +```dotnetcli +dotnet add package Microsoft.Extensions.Telemetry.Abstractions +``` + +Or, if you're using .NET 10+ SDK: + +```dotnetcli +dotnet package add Microsoft.Extensions.Telemetry.Abstractions +``` + +### [PackageReference](#tab/package-reference) + +```xml + +``` + +--- + +## Implementation + +Your custom enricher only needs to implement a single method. +During enrichment, this method is called and given an instance. The enricher then calls one of the overloads of +the method to record any properties it wants. + +> [!NOTE] +> If your custom log enricher calls , +> it is acceptable to send any type of argument to the `value` parameter as is, because it is parsed into the actual type and serialized internally +> to be sent further down the logging pipeline. + +```csharp +public class CustomEnricher : ILogEnricher +{ + public void Enrich(IEnrichmentTagCollector collector) + { + collector.Add("customKey", "customValue"); + } +} + +``` + +And you register it as shown in the following code using : + +```csharp +var builder = Host.CreateApplicationBuilder(args); +builder.Logging.EnableEnrichment(); +builder.Services.AddLogEnricher(); +``` + +It's also possible to configure manual instantiation of custom enrichers: + +```csharp +public class AnotherEnricher : ILogEnricher +{ + private readonly string _key; + private readonly object _value; + public CustomEnricher(string key, object value) + { + _key = key; + _value = value; + } + public void Enrich(IEnrichmentTagCollector collector) + { + collector.Add(_key, _value); + } +} +``` + +And you register it as shown in the following code : + +```csharp +var builder = Host.CreateApplicationBuilder(); +builder.Logging.EnableEnrichment(); +builder.Services.AddLogEnricher(new AnotherEnricher("anotherKey", "anotherValue")); +``` diff --git a/docs/core/enrichment/json-output-all-enabled.json b/docs/core/enrichment/json-output-all-enabled.json new file mode 100644 index 0000000000000..31cabf3e1e4ce --- /dev/null +++ b/docs/core/enrichment/json-output-all-enabled.json @@ -0,0 +1,12 @@ +{ + "EventId": 0, + "LogLevel": "Information", + "Category": "Enrichment.Program", + "Message": "This is a sample log message", + "State": { + "Message": "This is a sample log message", + "process.pid": "12924", + "thread.id": "2", + "{OriginalFormat}": "This is a sample log message" + } +} \ No newline at end of file diff --git a/docs/core/enrichment/json-output.json b/docs/core/enrichment/json-output.json new file mode 100644 index 0000000000000..afe2ae1f4b8de --- /dev/null +++ b/docs/core/enrichment/json-output.json @@ -0,0 +1,11 @@ +{ + "EventId": 0, + "LogLevel": "Information", + "Category": "Enrichment.Program", + "Message": "This is a sample log message", + "State": { + "Message": "This is a sample log message", + "process.pid": "10696", + "{OriginalFormat}": "This is a sample log message" + } +} \ No newline at end of file diff --git a/docs/core/enrichment/overview.md b/docs/core/enrichment/overview.md new file mode 100644 index 0000000000000..aba4e39371bd5 --- /dev/null +++ b/docs/core/enrichment/overview.md @@ -0,0 +1,61 @@ +--- +title: Log enrichment overview +description: Learn about log enrichment in .NET and how to enhance your logs with contextual information. +ms.date: 10/13/2025 +--- + +# Overview + +Log enrichment is a powerful feature that automatically attaches contextual information to your application's logs. Instead of manually adding metadata to each log, enrichment provides a systematic way to inject relevant context automatically across your entire application. + +## What is enrichment? + +Enrichment augments telemetry objects with additional information that provides valuable context about the environment, application state, and execution context when the telemetry was generated. This contextual data helps with debugging, monitoring, performance analysis, and understanding application behavior in production environments. + +## Why is enrichment important? + +Enrichment plays a critical role in enhancing observability and diagnostics by adding standardized contextual information—such as process details, environment tags, or user identifiers—to telemetry data. This additional metadata transforms raw logs into structured, meaningful insights, making it easier to trace issues, correlate events, and improve application reliability. By enabling enrichment and configuring specific enrichers, teams can streamline troubleshooting, optimize performance monitoring, and ensure compliance with operational standards. Ultimately, enrichment is not just a technical add-on; it’s a foundational practice for building resilient, transparent systems that support informed decision-making and faster incident resolution. + +## How enrichment works + +The enrichment framework operates through a collection of enrichers that are registered with the dependency injection container. When telemetry is generated, all registered enrichers automatically contribute their contextual information to the telemetry payload. You just register the specific set of enrichers you want into an instance. The enrichers run automatically without requiring changes to your application code. You simply configure which enrichers you want to use during application startup. + +## Dimension names and tags + +Enrichers add information to telemetry using standardized dimension names (also called tags or keys). + +## Setting up enrichment + +To use log enrichment in your application, you need to: + +1. **Enable enrichment** for logging. +2. **Register specific enrichers** you want to use. +3. **Configure options** for each enricher (optional). + +### Basic setup example + +Here's a simple example showing how to set up log enrichment with process information: + +:::code language="csharp" source="snippets/enrichment/Program.cs" highlight="8,9"::: + +This configuration: + +- Enables enrichment for logging via `EnableEnrichment()`. +- Registers the process log enricher via `AddProcessLogEnricher()`. +- Configures JSON console output to display the enriched data. + +### Output example + +With enrichment enabled, your log output will automatically include additional contextual information: + +:::code language="json" source="json-output.json" highlight="8"::: + +## Available enrichers + +The .NET enrichment framework provides some built-in enrichers, like: + +- **[Process enricher](process-log-enricher.md)**: Process and thread information + +## Custom enrichers + +If the built-in enrichers don't meet your specific needs, you can create custom enrichers to add application-specific context. For more information, check [custom enrichment](custom-enricher.md). diff --git a/docs/core/enrichment/process-log-enricher.md b/docs/core/enrichment/process-log-enricher.md new file mode 100644 index 0000000000000..00ffad20d13ce --- /dev/null +++ b/docs/core/enrichment/process-log-enricher.md @@ -0,0 +1,91 @@ +--- +title: Process log enricher +description: Learn how to use the process log enricher in .NET. +ms.date: 10/10/2025 +--- + +# Process log enricher + +The process enricher augments telemetry logs with process-specific information. + +You can register the enrichers in an IoC container. Then, all registered enrichers are picked up automatically by the respective telemetry instances, such as logs or metrics, where they enrich the telemetry information. + +## Install the package + +To get started, install the [📦 Microsoft.Extensions.Telemetry](https://www.nuget.org/packages/Microsoft.Extensions.Telemetry) NuGet package: + +### [.NET CLI](#tab/dotnet-cli) + +```dotnetcli +dotnet add package Microsoft.Extensions.Telemetry +``` + +Or, if you're using .NET 10+ SDK: + +```dotnetcli +dotnet package add Microsoft.Extensions.Telemetry +``` + +### [PackageReference](#tab/package-reference) + +```xml + +``` + +--- + +## Usage + +To use the process log enricher, first you enable enrichment. Then you can add the with default properties, as shown in the following code: + +:::code language="csharp" source="snippets/enrichment/Program.cs" highlight="8,9"::: + +Given this code sample, the output should be similar to the following JSON: + +:::code language="json" source="json-output.json" highlight="8"::: + +## `ProcessLogEnricherOptions` + +The class provides fine-grained control over which process-related properties are included in your log enrichment. This options class allows you to selectively enable or disable specific enrichment features such as process ID and thread ID information. Although default properties are supplied by the process enricher, you can customize them by initializing an instance of and providing it when registering the enricher. + +You can enable or disable individual options of the enricher using : + +```csharp +serviceCollection.AddProcessLogEnricher(options => +{ + options.ThreadId = true; + options.ProcessId = true; +}); +``` + +You may also disable or enable individual options using _appsettings.json_ file configuration, for example: + +```json +{ + "ProcessLogEnricherOptions": { + "ThreadId": true, + "ProcessId": true + } +} +``` + +and apply it accordingly using : + +```csharp +serviceCollection.AddProcessLogEnricher( + hostBuilder.Configuration.GetSection("ProcessLogEnricherOptions")); +``` + +The console output after enabling both options should look like this: + +:::code language="json" source="json-output-all-enabled.json" highlight="8,9"::: + +## Default configuration + +The default configuration for process log enrichment is: + +| Property | Default Value | Description | +|-------------|---------------|---------------------------------------------------------| +| `ProcessId` | `true` | If true, logs are enriched with the current process ID. | +| `ThreadId` | `false` | If true, logs are enriched with the current thread ID | diff --git a/docs/core/enrichment/snippets/enrichment/Enrichment.csproj b/docs/core/enrichment/snippets/enrichment/Enrichment.csproj new file mode 100644 index 0000000000000..3b0e57857694e --- /dev/null +++ b/docs/core/enrichment/snippets/enrichment/Enrichment.csproj @@ -0,0 +1,17 @@ + + + + Exe + net9.0 + enable + enable + + + + + + + + + + diff --git a/docs/core/enrichment/snippets/enrichment/Program.cs b/docs/core/enrichment/snippets/enrichment/Program.cs new file mode 100644 index 0000000000000..c4e3aa8e35a68 --- /dev/null +++ b/docs/core/enrichment/snippets/enrichment/Program.cs @@ -0,0 +1,23 @@ +using System.Text.Json; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +var builder = Host.CreateApplicationBuilder(args); + +builder.Logging.EnableEnrichment(); +builder.Services.AddProcessLogEnricher(); + +builder.Logging.AddJsonConsole(op => +{ + op.JsonWriterOptions = new JsonWriterOptions + { + Indented = true + }; +}); + +var host = builder.Build(); +var logger = host.Services.GetRequiredService>(); + +logger.LogInformation("This is a sample log message"); +await host.RunAsync(); diff --git a/docs/core/testing/microsoft-testing-platform-extensions-diagnostics.md b/docs/core/testing/microsoft-testing-platform-extensions-diagnostics.md index cfd64bdbb9d4c..06b38ecbb09c3 100644 --- a/docs/core/testing/microsoft-testing-platform-extensions-diagnostics.md +++ b/docs/core/testing/microsoft-testing-platform-extensions-diagnostics.md @@ -38,9 +38,6 @@ You can also enable the diagnostics logs using the environment variables: This extension allows you to create a crash dump file if the process crashes. This extension is shipped as part of [Microsoft.Testing.Extensions.CrashDump](https://nuget.org/packages/Microsoft.Testing.Extensions.CrashDump) NuGet package. -> [!IMPORTANT] -> The package is shipped with Microsoft .NET library closed-source free to use licensing model. - To configure the crash dump file generation, use the following options: | Option | Description | @@ -56,9 +53,6 @@ To configure the crash dump file generation, use the following options: This extension allows you to create a dump file after a given timeout. This extension is shipped as part of [Microsoft.Testing.Extensions.HangDump](https://nuget.org/packages/Microsoft.Testing.Extensions.HangDump) package. -> [!IMPORTANT] -> The package is shipped with Microsoft .NET library closed-source free to use licensing model. - To configure the hang dump file generation, use the following options: | Option | Description | diff --git a/docs/csharp/language-reference/compiler-messages/array-declaration-errors.md b/docs/csharp/language-reference/compiler-messages/array-declaration-errors.md index 05bf90416f684..211d07ae2b64f 100644 --- a/docs/csharp/language-reference/compiler-messages/array-declaration-errors.md +++ b/docs/csharp/language-reference/compiler-messages/array-declaration-errors.md @@ -37,6 +37,11 @@ f1_keywords: - "CS9208" - "CS9209" - "CS9210" + - "CS9212" + - "CS9213" + - "CS9214" + - "CS9215" + - "CS9222" helpviewer_keywords: - "CS0022" - "CS0178" @@ -73,6 +78,11 @@ helpviewer_keywords: - "CS9208" - "CS9209" - "CS9210" + - "CS9212" + - "CS9213" + - "CS9214" + - "CS9215" + - "CS9222" ms.date: 11/02/2023 --- # Resolve errors and warnings in array and collection declarations and initialization expressions @@ -110,6 +120,11 @@ That's by design. The text closely matches the text of the compiler error / warn - [**CS9188**](#invalid-collection-builder): *Type has a CollectionBuilderAttribute but no element type.* - [**CS9203**](#invalid-collection-initializer): *A collection expression of this type cannot be used in this context because it may be exposed outside of the current scope.* - [**CS9210**](#invalid-collection-initializer): *This version of cannot be used with collection expressions.* +- [**CS9212**](#invalid-collection-initializer): *Spread operator '`..`' cannot operate on variables of type 'type' because 'type' does not contain a public instance or extension definition for 'member'.* +- [**CS9213**](#invalid-collection-initializer): *Collection expression target 'type' has no element type.* +- [**CS9214**](#invalid-collection-initializer): *Collection expression type must have an applicable constructor that can be called with no arguments.* +- [**CS9215**](#invalid-collection-initializer): *Collection expression type 'type' must have an instance or extension method 'Add' that can be called with a single argument.* +- [**CS9222**](#invalid-collection-initializer): *Collection initializer results in an infinite chain of instantiations of collection 'type'.* In addition, the following warnings are covered in this article: @@ -141,6 +156,11 @@ The following errors indicate that the code generated by the compiler for a coll - **CS9176**: *There is no target type for the collection literal.* - **CS9203**: *A collection expression of this type cannot be used in this context because it may be exposed outside of the current scope.* - **CS9210**: *This version of cannot be used with collection expressions.* +- **CS9212**: *Spread operator '`..`' cannot operate on variables of type 'type' because 'type' does not contain a public instance or extension definition for 'member'.* +- **CS9213**: *Collection expression target 'type' has no element type.* +- **CS9214**: *Collection expression type must have an applicable constructor that can be called with no arguments.* +- **CS9215**: *Collection expression type 'type' must have an instance or extension method 'Add' that can be called with a single argument.* +- **CS9222**: *Collection initializer results in an infinite chain of instantiations of collection 'type'.* The compiler might also generate the following warning: @@ -159,6 +179,11 @@ The errors all indicate that the code generated by the compiler for a collection - Collection expressions can initialize explicitly typed variables of a collection type. If the variable isn't a collection or array type, or is implicitly typed (using `var`), a collection initializer can't be used. - A `ref struct` type, like can't be initialized with a collection expression that may violate ref safety. - A collection expression can't correctly initialize an using the current version. Use a different version of the runtime, or change the initialization expression. +- The spread operator (`..`) in **CS9212** requires the type to implement a suitable method (like `GetEnumerator`) to enumerate its elements. Ensure your type implements the required enumeration pattern or provides an extension method. +- **CS9213** occurs when the compiler can't determine what element type to use for the collection expression. This typically happens with custom collection types. Make sure your collection type properly exposes its element type through its type definition or implements appropriate collection interfaces. +- **CS9214** is generated when a collection expression tries to initialize a type that doesn't have a parameterless constructor. Collection expressions require a constructor that can be called with no arguments to create the instance before adding elements. +- **CS9215** happens when the collection type doesn't provide an `Add` method that accepts a single parameter of the element type. The `Add` method must be accessible (typically public) and accept exactly one argument that matches the collection's element type. +- **CS9222** indicates a circular dependency in collection initialization. This occurs when initializing a collection triggers the creation of another instance of the same collection type, which in turn requires initializing another instance, creating an infinite loop. Review your collection type's constructor and initialization logic to break the circular dependency. The warnings indicates that the collection expression, including any [spread elements](../operators/collection-expressions.md#spread-element) might allocate memory. Creating different storage and converting might be more efficient. diff --git a/docs/csharp/language-reference/compiler-messages/inline-array-errors.md b/docs/csharp/language-reference/compiler-messages/inline-array-errors.md index d7a2438d26769..e55a0ade10b16 100644 --- a/docs/csharp/language-reference/compiler-messages/inline-array-errors.md +++ b/docs/csharp/language-reference/compiler-messages/inline-array-errors.md @@ -2,6 +2,7 @@ title: Resolve errors related to inline arrays description: These compiler errors and warnings are generated when you create an inline array struct that is invalid. This article helps you diagnose and fix those issues. f1_keywords: + - "CS9125" - "CS9164" - "CS9165" - "CS9166" @@ -18,6 +19,7 @@ f1_keywords: - "CS9189" - "CS9259" helpviewer_keywords: + - "CS9125" - "CS9164" - "CS9165" - "CS9166" @@ -42,6 +44,7 @@ This article covers the following compiler errors and warnings: +- [**CS9125**](#inline-array-declaration): *Attribute parameter 'SizeConst' must be specified.* - [**CS9164**](#conversions-to-span): *Cannot convert expression to `Span` because it is not an assignable variable* - [**CS9165**](#conversions-to-span): *Cannot convert expression to `ReadOnlySpan` because it may not be passed or returned by reference* - [**CS9166**](#element-access): *Index is outside the bounds of the inline array* @@ -62,6 +65,7 @@ That's by design. The text closely matches the text of the compiler error / warn You declare inline arrays as a `struct` type with a single field, and an attribute that specifies the length of the array. The compiler generates the following errors for invalid inline array declarations: +- **CS9125**: *Attribute parameter 'SizeConst' must be specified.* - **CS9167**: *Inline array length must be greater than 0.* - **CS9168**: *Inline array struct must not have explicit layout.* - **CS9169**: *Inline array struct must declare one and only one instance field which must not be a ref field.* diff --git a/docs/csharp/language-reference/compiler-messages/lambda-expression-errors.md b/docs/csharp/language-reference/compiler-messages/lambda-expression-errors.md index e612fc51b03fc..0d3ddb3a00a91 100644 --- a/docs/csharp/language-reference/compiler-messages/lambda-expression-errors.md +++ b/docs/csharp/language-reference/compiler-messages/lambda-expression-errors.md @@ -18,6 +18,7 @@ f1_keywords: - "CS9098" # ERR_ImplicitlyTypedDefaultParameter: Implicitly typed lambda parameter '{0}' cannot have a default value. - "CS9099" # WRN_OptionalParamValueMismatch: The default parameter value does not match in the target delegate type. - "CS9100" # WRN_ParamsArrayInLambdaOnly: Parameter has params modifier in lambda but not in target delegate type. + - "CS9236" # INF_TooManyBoundLambdas: Compiling requires binding the lambda expression at least {0} times. helpviewer_keywords: - "CS0748" - "CS0835" @@ -35,6 +36,7 @@ helpviewer_keywords: - "CS9098" - "CS9099" - "CS9100" + - "CS9236" ms.date: 05/04/2023 --- # Errors and warnings when using lambda expressions and anonymous functions @@ -64,6 +66,10 @@ In addition, there are several *warnings* related to declaring and using lambda - [**CS9099**](#lambda-expression-delegate-type): *The default parameter value does not match in the target delegate type.* - [**CS9100**](#lambda-expression-delegate-type): *Parameter has params modifier in lambda but not in target delegate type.* +The compiler also produces the following *informational* message: + +- [**CS9236**](#syntax-limitations-in-lambda-expressions): *Compiling requires binding the lambda expression at least count times. Consider declaring the lambda expression with explicit parameter types, or if the containing method call is generic, consider using explicit type arguments.* + ## Syntax limitations in lambda expressions Some C# syntax is prohibited in lambda expressions and anonymous methods. Using invalid constructs in a lambda expression causes the following errors: @@ -90,6 +96,12 @@ In addition, interpolated string handler types are ignored when applied to a lam - **CS8971**: *InterpolatedStringHandlerArgument has no effect when applied to lambda parameters and will be ignored at the call site.* +Certain expressions cause the compiler to emit the following informational warning: + +- **CS9236**: *Compiling requires binding the lambda expression at least count times. Consider declaring the lambda expression with explicit parameter types, or if the containing method call is generic, consider using explicit type arguments.* + +The complexity of the lambda expressions and how they invoke other lambda expressions is negatively impacting compiler performance. The reason is that the compiler must infer parameter and argument types through the lambda expressions and the potential types takes time. + ## Lambda expression parameters and returns These errors indicate a problem with a parameter declaration: diff --git a/docs/csharp/language-reference/compiler-messages/ref-modifiers-errors.md b/docs/csharp/language-reference/compiler-messages/ref-modifiers-errors.md index bd5adb8e581e8..448ec0708f014 100644 --- a/docs/csharp/language-reference/compiler-messages/ref-modifiers-errors.md +++ b/docs/csharp/language-reference/compiler-messages/ref-modifiers-errors.md @@ -66,6 +66,7 @@ f1_keywords: - "CS9199" - "CS9200" - "CS9201" + - "CS9205" - "CS9265" helpviewer_keywords: - "CS0192" @@ -132,6 +133,7 @@ helpviewer_keywords: - "CS9199" - "CS9200" - "CS9201" + - "CS9205" - "CS9265" ms.date: 11/06/2024 --- @@ -209,6 +211,7 @@ The following warnings are generated when reference variables are used incorrect - [**CS9198**](#reference-variable-restrictions): *Reference kind modifier of parameter doesn't match the corresponding parameter in target.* - [**CS9200**](#reference-variable-restrictions): *A default value is specified for `ref readonly` parameter, but `ref readonly` should be used only for references. Consider declaring the parameter as `in`.* - [**CS9201**](#reference-variable-restrictions): *Ref field should be ref-assigned before use.* +- [**CS9205**](#incorrect-syntax): *Expected interpolated string.* - [**CS9265**](#reference-variable-restrictions): *Field is never ref-assigned to, and will always have its default value (null reference)* These errors and warnings follow these themes: @@ -228,12 +231,14 @@ These errors indicate that you're using incorrect syntax regarding reference var - **CS8373**: *The left-hand side of a `ref` assignment must be a ref variable.* - **CS8388**: *An `out` variable cannot be declared as a ref local.* - **CS9190**: *`readonly` modifier must be specified after `ref`.* +- **CS9205**: *Expected interpolated string.* You can correct the error with one of these changes: - The left operand of an `= ref` operator must be a reference variable. For more information on the correct syntax, see [reference variables](../statements/declarations.md#reference-variables). - The parameter modifier `ref readonly` must be in that order. `readonly ref` is not a legal parameter modifier. Switch the order of the words. - A local variable can't be declared as `out`. To declare a local reference variable, use `ref`. +- An `out` argument can't be an interpolated string. ## Reference variable restrictions diff --git a/docs/csharp/language-reference/compiler-messages/source-generator-errors.md b/docs/csharp/language-reference/compiler-messages/source-generator-errors.md index bb32eddc74871..6b30f90f969bb 100644 --- a/docs/csharp/language-reference/compiler-messages/source-generator-errors.md +++ b/docs/csharp/language-reference/compiler-messages/source-generator-errors.md @@ -31,6 +31,11 @@ f1_keywords: - "CS9178" - "CS9206" - "CS9207" + - "CS9231" + - "CS9232" + - "CS9233" + - "CS9234" + - "CS9235" - "CS9270" helpviewer_keywords: - "CS9137" @@ -62,6 +67,11 @@ helpviewer_keywords: - "CS9178" - "CS9206" - "CS9207" + - "CS9231" + - "CS9232" + - "CS9233" + - "CS9234" + - "CS9235" - "CS9270" ms.date: 05/23/2025 --- @@ -70,7 +80,7 @@ ms.date: 05/23/2025 The following errors are generated when source generators or interceptors are loaded during a compilation: - [**CS9137**](#interceptors-are-experimental): *The 'interceptors' experimental feature is not enabled. Add `InterceptorsPreview` to your project.* -- [**CS9138**](#other-failures): *Method cannot be used as an interceptor because it or its containing type has type parameters.* +- [**CS9138**](#incorrect-interceptor-declaration): *Method cannot be used as an interceptor because it or its containing type has type parameters.* - [**CS9139**](#incorrect-mapping): *Cannot intercept: compilation does not contain a file with path.* - [**CS9140**](#incorrect-mapping): *Cannot intercept: compilation does not contain a file with path. Did you mean to use a different path?* - [**CS9141**](#incorrect-mapping): *The provided line and character number does not refer to an interceptable method name, but rather to a token.* @@ -78,28 +88,33 @@ The following errors are generated when source generators or interceptors are lo - [**CS9143**](#incorrect-mapping): *The given line is `c` characters long, which is fewer than the provided character number `n`.* - [**CS9144**](#signature-mismatch): *Cannot intercept method `M` with interceptor `V` because the signatures do not match.* - [**CS9145**](#incorrect-mapping): *Cannot intercept: Path is unmapped. Expected mapped path.* -- [**CS9146**](#other-failures): *An interceptor method must be an ordinary member method.* +- [**CS9146**](#incorrect-interceptor-declaration): *An interceptor method must be an ordinary member method.* - [**CS9147**](#incorrect-mapping): *The provided line and character number does not refer to the start of a token. Did you mean to use line `n` and character `c`?* - [**CS9148**](#signature-mismatch): *Interceptor must have a `this` parameter matching parameter.* - [**CS9149**](#signature-mismatch): *Interceptor must not have a `this` parameter because method does not have a `this` parameter.* - [**CS9150**](#incorrect-mapping): *Interceptor cannot have a `null` file path.* -- [**CS9151**](#other-failures): *Possible method name `M` cannot be intercepted because it is not being invoked.* -- [**CS9152**](#other-failures): *Cannot intercept a call in file with this path because multiple files in the compilation have this path.* -- [**CS9153**](#other-failures): *The indicated call is intercepted multiple times.* +- [**CS9151**](#incorrect-interceptor-declaration): *Possible method name `M` cannot be intercepted because it is not being invoked.* +- [**CS9152**](#incorrect-interceptor-declaration): *Cannot intercept a call in file with this path because multiple files in the compilation have this path.* +- [**CS9153**](#incorrect-interceptor-declaration): *The indicated call is intercepted multiple times.* - [**CS9155**](#signature-mismatch): *Cannot intercept call with `M` because it is not accessible within `V`.* - [**CS9156**](#signature-mismatch): *Cannot intercept call to `M` with `V` because of a difference in 'scoped' modifiers or `[UnscopedRef]` attributes.* - [**CS9157**](#incorrect-mapping): *Line and character numbers provided to `InterceptsLocationAttribute` must be positive.* -- [**CS9160**](#other-failures): *A nameof operator cannot be intercepted.* -- [**CS9161**](#other-failures): *An interceptor cannot be marked with `UnmanagedCallersOnlyAttribute`.* +- [**CS9160**](#incorrect-interceptor-declaration): *A nameof operator cannot be intercepted.* +- [**CS9161**](#incorrect-interceptor-declaration): *An interceptor cannot be marked with `UnmanagedCallersOnlyAttribute`.* - [**CS9177**](#signature-mismatch): *Interceptor must be non-generic or have matching arity.* - [**CS9178**](#signature-mismatch): *Method must be non-generic to match* -- [**CS9206**](#other-failures): *An interceptor cannot be declared in the global namespace.* -- [**CS9207**](#other-failures): *Cannot intercept because method is not an invocation of an ordinary member method.* +- [**CS9206**](#incorrect-interceptor-declaration): *An interceptor cannot be declared in the global namespace.* +- [**CS9207**](#incorrect-interceptor-declaration): *Cannot intercept because method is not an invocation of an ordinary member method.* +- [**CS9231**](#incorrect-interceptor-declaration): *The data argument to InterceptsLocationAttribute is not in the correct format.* +- [**CS9232**](#incorrect-interceptor-declaration): *Version 'version' of the interceptors format is not supported. The latest supported version is '1'.* +- [**CS9233**](#incorrect-interceptor-declaration): *Cannot intercept a call in file 'file' because it is duplicated elsewhere in the compilation.* +- [**CS9234**](#incorrect-interceptor-declaration): *Cannot intercept a call in file 'file' because a matching file was not found in the compilation.* +- [**CS9235**](#incorrect-interceptor-declaration): *The data argument to InterceptsLocationAttribute refers to an invalid position in file 'file'.* The following warnings are generated when source generators or interceptors are loaded during a compilation: -- [**CS8784**](#other-failures): *Generator '`YourSourceGeneratorName`' failed to initialize. It will not contribute to the output and compilation errors may occur as a result.* -- [**CS8785**](#other-failures): *Generator '`YourSourceGeneratorName`' failed to generate source. It will not contribute to the output and compilation errors may occur as a result.* +- [**CS8784**](#incorrect-interceptor-declaration): *Generator '`YourSourceGeneratorName`' failed to initialize. It will not contribute to the output and compilation errors may occur as a result.* +- [**CS8785**](#incorrect-interceptor-declaration): *Generator '`YourSourceGeneratorName`' failed to generate source. It will not contribute to the output and compilation errors may occur as a result.* - [**CS9154**](#signature-mismatch): *Intercepting a call to `M` with interceptor `V`, but the signatures do not match.* - [**CS9158**](#signature-mismatch): *Nullability of reference types in return type doesn't match interceptable method.* - [**CS9159**](#signature-mismatch): *Nullability of reference types in type of parameter doesn't match interceptable method.* @@ -158,12 +173,15 @@ Interceptors require a source mapping that maps the interceptable method and the - **CS9150**: *Interceptor cannot have a `null` file path.* - **CS9157**: *Line and character numbers provided to `InterceptsLocationAttribute` must be positive.* -An interceptor specifies the location in the source code of the interceptable method. You specify the location by applying an `[InterceptsLocation]` attribute. You specify the line and column numbers in a remapped source file where the interceptor should be injected. These errors indicate that something in the attribute or the location doesn't match a location for a valid interceptable method. For details on the format and values for this attribute, see the [feature specification](https://github.com/dotnet/roslyn/blob/main/docs/features/interceptors.md#interceptslocationattribute). +## Incorrect interceptor declaration -## Other failures - -These errors indicate other limitations for interceptors: +The following errors indicate issues with interceptor declarations, including problems with the `InterceptsLocationAttribute` format or violations of interceptor rules: +- **CS9231**: *The data argument to InterceptsLocationAttribute is not in the correct format.* +- **CS9232**: *Version 'version' of the interceptors format is not supported. The latest supported version is '1'.* +- **CS9233**: *Cannot intercept a call in file 'file' because it is duplicated elsewhere in the compilation.* +- **CS9234**: *Cannot intercept a call in file 'file' because a matching file was not found in the compilation.* +- **CS9235**: *The data argument to InterceptsLocationAttribute refers to an invalid position in file 'file'.* - **CS9138**: *Method cannot be used as an interceptor because it or its containing type has type parameters.* - **CS9146**: *An interceptor method must be an ordinary member method.* - **CS9151**: *Possible method name `M` cannot be intercepted because it is not being invoked.* @@ -174,12 +192,19 @@ These errors indicate other limitations for interceptors: - **CS9206**: *An interceptor cannot be declared in the global namespace.* - **CS9207**: *Cannot intercept because method is not an invocation of an ordinary member method.* -These errors indicate that your interceptor method violates one of the rules for interceptors: - -- Interceptors can't be generic methods, or members of generic classes. -- Interceptors must be ordinary members. They can't be operators, instance or static constructors, or finalizers. -- Interceptable methods must be ordinary members. They can't be operators, instance or static constructors, or finalizers, nor delegate invocations. -- Interceptable methods that are never invoked can't be intercepted. -- Interceptable methods can be intercepted at most once. -- Interceptors can't be methods limited to unmanaged callers. -- Interceptors must be contained in a namespace. +These errors occur when interceptor declarations violate the rules for interceptors or when the `InterceptsLocationAttribute` contains invalid data: + +- **CS9231** is generated when the data format doesn't match the expected structure. The attribute requires specifically formatted data that encodes the file path and position information. +- **CS9232** indicates you're using a version number that isn't supported. The interceptors feature uses version '1' format. Update your source generator to use the supported version. +- **CS9233** happens when the compilation contains multiple files with the same path, making it ambiguous which file to intercept in. Ensure each file in your compilation has a unique path. +- **CS9234** is emitted when the file path specified in the attribute doesn't match any file in the current compilation. Verify the file path is correct and the file is included in the compilation. +- **CS9235** occurs when the position data (line and character numbers) points to an invalid location in the specified file. This can happen if the position is outside the file's bounds or doesn't correspond to a valid interception point. +- **CS9138** prevents generic methods or members of generic classes from being interceptors. +- **CS9146** requires interceptors to be ordinary members (not operators, constructors, or finalizers). +- **CS9151** prevents intercepting methods that are never invoked. +- **CS9152** detects duplicate file paths in the compilation. +- **CS9153** prevents multiple interceptions of the same call. +- **CS9160** prevents intercepting `nameof` operators. +- **CS9161** prevents interceptors from being marked with `UnmanagedCallersOnlyAttribute`. +- **CS9206** requires interceptors to be contained in a namespace (not global). +- **CS9207** requires interceptable methods to be ordinary member invocations. diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index 3b1858ef70716..5deb522f27944 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -504,7 +504,7 @@ items: CS7084, CS8166, CS8167, CS8168, CS8169. CS8325, CS8326, CS8327, CS8329, CS8330, CS8331, CS8332, CS8337, CS8338, CS8351, CS8373, CS8374, CS8388, CS8977, CS9072, CS9077, CS9078, CS9079, CS9085, CS9086, CS9087, CS9089, CS9091, CS9092, CS9093, CS9094, CS9095, CS9096, CS9097, CS9101, CS9102, CS9104, CS9190, CS9191, CS9192, CS9193, CS9195, CS9196, CS9197, CS9198, - CS9199, CS9200, CS9201, CS9265 + CS9199, CS9200, CS9201, CS9205, CS9265 - name: "`ref struct` types" href: ./compiler-messages/ref-struct-errors.md displayName: > @@ -555,15 +555,15 @@ items: displayName: > CS0022, CS0178, CS0248, CS0251, CS0270, CS0611, CS0623, CS0650, CS0719, CS0747, CS0820, CS0826, CS0846, CS1062, CS1063, CS1064, CS1552, CS1586, CS1920, CS1921, CS1925, CS1950, CS1954, CS3007, CS3016, CS9174, CS9176, CS9185, CS9186, CS9187, - CS9188, CS9203, CS9208, CS9209, CS9210 + CS9188, CS9203, CS9208, CS9209, CS9210, CS9212, CS9213, CS9214, CS9215, CS9222 - name: Inline arrays href: ./compiler-messages/inline-array-errors.md - displayName: CS9164, CS9165, CS9166, CS9167, CS9168, CS9169, CS9172, CS9173, CS9180, CS9181, CS9182, CS9183, CS9184, CS9259 + displayName: CS9125, CS9164, CS9165, CS9166, CS9167, CS9168, CS9169, CS9172, CS9173, CS9180, CS9181, CS9182, CS9183, CS9184, CS9259 - name: Lambda expressions href: ./compiler-messages/lambda-expression-errors.md displayName: > CS0748, CS1621, CS1628, CS1632, CS1673, CS1686, CS1706, CS1989, CS3006, CS8030, CS8175, CS8916, CS8971, CS8972, CS8975, CS9098, - CS9099, CS9100 + CS9099, CS9100, CS9236 - name: Overload resolution href: ./compiler-messages/overload-resolution.md displayName: > @@ -585,7 +585,8 @@ items: href: ./compiler-messages/source-generator-errors.md displayName: > CS9137, CS9138, CS9139, CS9140, CS9141, CS9142, CS9143, CS9144, CS9145, CS9146, CS9147, CS9148, CS9149, CS9150, CS9151, - CS9152, CS9153, CS9154, CS9155, CS9156, CS9157, CS9158, CS9159, CS9160, CS9161, CS9177, CS9178, CS9206, CS9207, CS9270 + CS9152, CS9153, CS9154, CS9155, CS9156, CS9157, CS9158, CS9159, CS9160, CS9161, CS9177, CS9178, CS9206, CS9207, CS9231, + CS9232, CS9233, CS9234, CS9235, CS9270 - name: static abstract interface members href: ./compiler-messages/static-abstract-interfaces.md displayName: CS8920, CS8921, CS8922, CS8923, CS8924, CS8925, CS8926, CS8928, CS8930, 8931, 8932 diff --git a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md index 59076a4fed587..19935adcc92bd 100644 --- a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md +++ b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md @@ -575,23 +575,10 @@ f1_keywords: - "CS9095" - "CS9096" - "CS9097" - # C# 12 errors begin here - - "CS9123" - - "CS9125" - - "CS9205" - - "CS9212" - - "CS9213" - - "CS9214" - - "CS9215" - - "CS9222" - - "CS9229" - - "CS9230" - - "CS9231" - - "CS9232" - - "CS9233" - - "CS9234" - - "CS9235" - - "CS9236" +# C# 12 errors begin here + - "CS9123" # The '&' operator should not be used on parameters or local variables in async methods. (new unsafe file) + - "CS9229" # Modifiers cannot be placed on using declarations (using declarations) + - "CS9230" # Cannot perform a dynamic invocation on an expression with type 'type'. (dynamic binding) # C# 14 errors begin here - "CS9327" - "CS9328" diff --git a/docs/navigate/tools-diagnostics/toc.yml b/docs/navigate/tools-diagnostics/toc.yml index bd5206dcaaf63..6074077eebbdf 100644 --- a/docs/navigate/tools-diagnostics/toc.yml +++ b/docs/navigate/tools-diagnostics/toc.yml @@ -446,6 +446,14 @@ items: href: ../../core/diagnostics/distributed-tracing-collection-walkthroughs.md - name: Built-in activities href: ../../core/diagnostics/distributed-tracing-builtin-activities.md + - name: Enrichment + items: + - name: Overview + href: ../../core/enrichment/overview.md + - name: Process log enricher + href: ../../core/enrichment/process-log-enricher.md + - name: Custom enricher + href: ../../core/enrichment/custom-enricher.md - name: Specialized diagnostics items: - name: Overview