HardDev.CoreUtils 1.0.1

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

// Install HardDev.CoreUtils as a Cake Tool
#tool nuget:?package=HardDev.CoreUtils&version=1.0.1

HardDev.CoreUtils

NuGet

HardDev.CoreUtils is a utility library providing core functionality for .NET applications. It includes advanced configuration handling, logging, and more. This library is designed to simplify common tasks and help developers create cleaner and more efficient code.

Features

  • Advanced configuration handling with support for JSON files and built-in default values and validation.
  • Easy-to-use logging setup built on top of the Serilog library.
  • Highly customizable logger configuration with adjustable log levels, output format, and more.
  • Supports .NET 6.0 and .NET 7.0 target frameworks

Getting Started

Install the latest version of the HardDev.CoreUtils library from NuGet.

dotnet add package HardDev.CoreUtils

Usage

Configuration

The HardDev.CoreUtils configuration system treats configurations as singletons, providing consistently updated values across your application.

Creating a Configuration file:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using HardDev.CoreUtils.Config;

[DataContract]
public class SampleConfig : BaseConfiguration<SampleConfig>
{
    [DataMember, DefaultValue(42)]
    public int IntegerValue { get; set; }

    [DataMember, DefaultValue("http://default_url.com"), Url(ErrorMessage = "Invalid URL format")]
    public string Url { get; set; } 

    [DataMember, CollectionDefaultValue(typeof(string[]), "Acc1", "Acc2"), Required(ErrorMessage = "Accounts cannot be null"),
     MinLength(1, ErrorMessage = "Accounts cannot be empty")]
    public IReadOnlyList<string> Accounts { get; set; }

    [DataMember, CollectionDefaultValue(typeof(List<int>), 1, 2, 3), Required(ErrorMessage = "Numbers cannot be null"),
     MinLength(1, ErrorMessage = "Numbers cannot be empty")]
    public IReadOnlyList<int> Numbers { get; set; }

    [DataMember, CollectionDefaultValue(typeof(Dictionary<string, int>), "Key1", 1, "Key2", 2),
     Required(ErrorMessage = "Example dictionary cannot be null"), MinLength(1, ErrorMessage = "ExampleDictionary cannot be empty")]
    public IDictionary<string, int> ExampleDictionary { get; set; }

    public SampleConfig() : base("Configs/SampleConfig.json")
    {
    }
}

Using a Configuration in your application:

// Loads SampleConfig and demonstrates its configurations
Logger.Information("Loading SampleConfig...");

// Get or create a SampleConfig instance
var sampleConfig = AppConfig.Get<SampleConfig>();

// Print the loaded values
Logger.Information("Loaded values from SampleConfig:");
Logger.Information("IntegerValue: {IntegerValue}", sampleConfig.IntegerValue);
Logger.Information("Url: {Url}", sampleConfig.Url);
Logger.Information("Accounts: {Accounts}", string.Join(", ", sampleConfig.Accounts));
Logger.Information("Numbers: {Numbers}", string.Join(", ", sampleConfig.Numbers));
Logger.Information("ExampleDictionary: {ExampleDictionary}", string.Join(", ", sampleConfig.ExampleDictionary));

// Change some values
Logger.Information("Changing some values in SampleConfig...");
sampleConfig.IntegerValue = 24;

// Save the configuration asynchronously
Logger.Information("Saving SampleConfig...");
sampleConfig.Save();

Logging

HardDev.CoreUtils.Logging provides an easy-to-use logger configuration and management built on top of the Serilog library.

Here's an example of setting up the logger and logging a message:

using HardDev.CoreUtils.Logging;
using Serilog.Events;

// Configure the logger
var loggerConfig = new AppLoggerConfig
{
    LogDirectory = "Logs",
    EnableConsoleLogging = true,
    EnableFileLogging = true,
    ConsoleLogLevel = LogEventLevel.Debug,
    FileLogLevel = LogEventLevel.Verbose,
};
AppLogger.Configure(loggerConfig);

// Log a message
AppLogger.Log.Information("Hello, World!");

More examples of using the logger:

  1. Log an error message with exception:
try
{
    // Some code that might throw an exception
}
catch (Exception ex)
{
    AppLogger.Log.Error(ex, "An error occurred");
}
  1. Log a warning message with properties:
int currentUsers = 50;
int maxUsers = 100;
AppLogger.Log.Warning("Current users reached {CurrentUsers} out of {MaxUsers}", currentUsers, maxUsers);
  1. Scoped logger with context:
var scopedLogger = AppLogger.ForName("ScopedContext");
scopedLogger.Information("This message has a custom context.");

Build

HardDev.CoreUtils supports the following target frameworks:

  • .NET Standard 2.0
  • .NET 6.0
  • .NET 7.0

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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. 
.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.

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
2.2.7 105 3/25/2024
2.2.6 84 3/24/2024
2.2.5 73 3/24/2024
2.2.4 83 3/24/2024
2.2.3 89 3/24/2024
2.2.2 84 3/23/2024
2.2.1 97 3/22/2024
2.2.0 88 3/22/2024
2.1.2 81 3/21/2024
2.1.1 78 3/21/2024
2.1.0 80 3/21/2024
2.0.2 107 3/13/2024
1.2.2 94 3/11/2024
1.2.1 87 3/11/2024
1.2.0 85 3/11/2024
1.1.0 83 3/10/2024
1.0.1 158 6/17/2023

Initial release of the CoreUtils library