CodeOfChaos.Extensions.Serilog 0.24.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package CodeOfChaos.Extensions.Serilog --version 0.24.1                
NuGet\Install-Package CodeOfChaos.Extensions.Serilog -Version 0.24.1                
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="CodeOfChaos.Extensions.Serilog" Version="0.24.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CodeOfChaos.Extensions.Serilog --version 0.24.1                
#r "nuget: CodeOfChaos.Extensions.Serilog, 0.24.1"                
#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.
// Install CodeOfChaos.Extensions.Serilog as a Cake Addin
#addin nuget:?package=CodeOfChaos.Extensions.Serilog&version=0.24.1

// Install CodeOfChaos.Extensions.Serilog as a Cake Tool
#tool nuget:?package=CodeOfChaos.Extensions.Serilog&version=0.24.1                

CodeOfChaos.Extensions.Serilog

CodeOfChaos.Extensions.Serilog extends the capabilities of Serilog, providing additional methods to simplify logging and exception management. It is particularly useful for elegantly handling fatal and error conditions, enhancing both logging clarity and application lifecycle management.


Features

Enhanced Logging and Exception Management

  • Throwable Errors:
    • Log an error and throw an exception in a single call.
    • Supports both generic exceptions and custom exception types.
  • Throwable Fatals:
    • Log a fatal error and throw an exception simultaneously.
    • Supports both generic and custom exceptions.
  • Exit Logging:
    • Log a fatal error and immediately terminate the application with a specified exit code.

Installation

This library targets .NET 9.0 and requires C# 13.0. Ensure your project meets these requirements before using.

Add the dependency to your project via NuGet:

dotnet add package CodeOfChaos.Extensions.Serilog

Usage

Here's how you can use the library to enhance logging in your applications:

Throwable Errors

Log an error and throw an exception:

using Serilog;

ILogger logger = new LoggerConfiguration().WriteTo.Console().CreateLogger();

try {
    throw logger.ThrowableError("An error occurred: {Error}", "SampleError");
} catch (Exception ex) {
    Console.WriteLine($"Caught exception: {ex.Message}");
}

Or, use a specific exception type:

try {
    throw logger.ThrowableError<InvalidOperationException>("Invalid operation: {Details}", "SampleDetails");
} catch (Exception ex) {
    Console.WriteLine($"Caught exception: {ex.Message}");
}

Throwable Fatals

Log a fatal error and throw an exception:

try {
    throw logger.ThrowableFatal("A fatal error occurred: {Error}", "CriticalError");
} catch (Exception ex) {
    Console.WriteLine($"Caught fatal exception: {ex.Message}");
}

Specify a custom exception type:

try {
    throw logger.ThrowableFatal<InvalidOperationException>("A critical failure: {Details}", "CriticalDetails");
} catch (Exception ex) {
    Console.WriteLine($"Caught fatal exception: {ex.Message}");
}

Exit Logging

Log a fatal error and immediately terminate the application with an exit code:

logger.ExitFatal(1, "The application encountered a critical error and will exit: {Reason}", "CriticalIssue");

Features in Detail

Method: ThrowableError

  • Logs an error-level message and throws an exception.
  • Supports custom exception types via the generic overload ThrowableError<TException>().

Method: ThrowableFatal

  • Logs a fatal-level message and throws an exception.
  • Supports custom exception types as well.

Method: ExitFatal

  • Logs a fatal-level message and terminates the application.
  • Takes an exit code to ensure proper command-line application management.

Contributing

Feel free to fork and contribute to the project by submitting pull requests. When contributing, ensure your changes align with the project’s coding standards.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on CodeOfChaos.Extensions.Serilog:

Package Downloads
CodeOfChaos.AspNetCore

A Library of Nuget Packages frequently used in other code-of-chaos projects or project written by Andreas

CodeOfChaos.Extensions.AspNetCore

A Library of broadly used extensions for AspNetCore

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.27.0 45 12/29/2024
0.26.0 53 12/28/2024
0.25.0 48 12/28/2024
0.24.3 47 12/27/2024
0.24.2 38 12/27/2024
0.24.1 37 12/27/2024
0.24.0 38 12/27/2024
0.23.1 39 12/27/2024
0.23.0 40 12/27/2024
0.22.2 44 12/27/2024
0.22.0 76 12/23/2024
0.21.0 86 12/22/2024
0.20.0-preview.1 44 12/21/2024
0.4.1 107 10/31/2024
0.4.0 158 9/4/2024
0.3.1 139 8/30/2024
0.3.0 181 7/8/2024
0.2.3 119 7/8/2024
0.2.2 121 7/6/2024
0.2.1 131 7/1/2024
0.2.0 268 6/8/2024
0.1.0 191 6/7/2024