ErrorCodes.Net.Analyzers 0.2.2

dotnet add package ErrorCodes.Net.Analyzers --version 0.2.2
                    
NuGet\Install-Package ErrorCodes.Net.Analyzers -Version 0.2.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ErrorCodes.Net.Analyzers" Version="0.2.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ErrorCodes.Net.Analyzers" Version="0.2.2" />
                    
Directory.Packages.props
<PackageReference Include="ErrorCodes.Net.Analyzers" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ErrorCodes.Net.Analyzers --version 0.2.2
                    
#r "nuget: ErrorCodes.Net.Analyzers, 0.2.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ErrorCodes.Net.Analyzers@0.2.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ErrorCodes.Net.Analyzers&version=0.2.2
                    
Install as a Cake Addin
#tool nuget:?package=ErrorCodes.Net.Analyzers&version=0.2.2
                    
Install as a Cake Tool

๐Ÿ› ๏ธ ErrorCodes.Net

ErrorCodes.Net is a .NET library and Roslyn analyzer that helps you define and manage structured, traceable error codes across your applications.

It enforces a consistent format:

0xPPMMTTTT

Where:

  • PP = Product ID (e.g., 00 for Service A, 01 for Service B)
  • MM = Project ID (e.g., 06)
  • TT = Error Type ID
  • TT = Error Code

This structure ensures that each error code is unique, traceable, and easy to interpret.

๐Ÿš€ Features

  • โœ… Enforces consistent error code formatting via Roslyn analyzers
  • โœ… YAML-based configuration for defining error codes
  • โœ… Supports multiple error types per project
  • โœ… Generates C# code for defined error codes
  • โœ… Integrates seamlessly with your build process

โฌ‡๏ธ Installation

Install the analyzer via NuGet:

dotnet add package ErrorCodes.Net.Analyzers

In your .csproj file, ensure the package is referenced with PrivateAssets="all" to prevent it from being exposed to consumers:

<PackageReference Include="ErrorCodes.Net.Analyzers" PrivateAssets="all" />

โš™๏ธ Configuration

Create an ErrorCodes.yaml or ErrorCodes.yml file in your project root with the following structure:

projectId: 6
namespace: ErrorCodes.Net.Generated
errorTypes:
  - name: TestErrors
    errors:
      - code: 1
        name: InvalidInput
      - code: 2
        name: NotFound

This configuration defines the project ID, the namespace for the generated code, and a list of error types with their corresponding error codes and messages.

๐Ÿงช Example Usage

Once configured, the analyzer will generate a static class with your error codes.

Make sure to include the YAML file in your .csproj so the analyzer can find it:

<ItemGroup>
  <AdditionalFiles Include="ErrorCodes.yaml" />
</ItemGroup>

Then you can use the generated error codes like this:

using ErrorCodes.Net.Generated;

public class Example
{
    public void DoSomething()
    {
        var errorCode = ErrorCodeLookup.TestErrors.InvalidInput;
        Console.WriteLine(errorCode); // Outputs: 0x06010001
    }
}

See a full sample project here.

๐Ÿงฐ Advanced Usage

You can define multiple error types within the same project:

projectId: 6
namespace: ErrorCodes.Net.Generated
errorTypes:
  - name: ValidationErrors
    errors:
      - code: 1
        name: MissingField
  - name: DatabaseErrors
    errors:
      - code: 1
        name: ConnectionFailed

This will generate separate classes for each error type, such as ValidationErrors and DatabaseErrors, each containing their respective error codes.

๐Ÿ“ฆ NuGet Package

The analyzer is available on NuGet:

ErrorCodes.Net.Analyzers on NuGet

๐Ÿค Contributing

Contributions are welcome! If you have suggestions for improvements or new features, feel free to open an issue or submit a pull request.

๐Ÿ“„ License

This project is licensed under the MIT License. See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.2 118 6/18/2026
0.2.2-preview.3 65 6/18/2026
0.2.2-preview.2 211 4/14/2025
0.2.2-preview.1 203 4/14/2025
0.2.1 241 5/7/2024
0.2.0 197 4/30/2024
0.1.1-preview.8 114 4/30/2024
0.1.1-preview.7 118 4/26/2024
0.1.1-preview.6 116 4/25/2024
0.1.1-preview.5 117 4/23/2024
0.1.1-preview.4 107 4/23/2024
0.1.1-preview.3 122 4/22/2024