HardDev.CoreUtils
1.0.1
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
<PackageReference Include="HardDev.CoreUtils" Version="1.0.1" />
paket add HardDev.CoreUtils --version 1.0.1
#r "nuget: HardDev.CoreUtils, 1.0.1"
// 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
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:
- Log an error message with exception:
try
{
// Some code that might throw an exception
}
catch (Exception ex)
{
AppLogger.Log.Error(ex, "An error occurred");
}
- Log a warning message with properties:
int currentUsers = 50;
int maxUsers = 100;
AppLogger.Log.Warning("Current users reached {CurrentUsers} out of {MaxUsers}", currentUsers, maxUsers);
- 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 | Versions 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. |
-
.NETStandard 2.0
- Newtonsoft.Json (>= 13.0.3)
- Serilog (>= 2.12.0)
- Serilog.Enrichers.Demystifier (>= 1.0.2)
- Serilog.Sinks.Console (>= 4.1.0)
- Serilog.Sinks.File (>= 5.0.0)
- System.ComponentModel.Annotations (>= 5.0.0)
- System.Runtime.Serialization.Primitives (>= 4.3.0)
-
net6.0
- Newtonsoft.Json (>= 13.0.3)
- Serilog (>= 2.12.0)
- Serilog.Enrichers.Demystifier (>= 1.0.2)
- Serilog.Sinks.Console (>= 4.1.0)
- Serilog.Sinks.File (>= 5.0.0)
- System.ComponentModel.Annotations (>= 5.0.0)
- System.Runtime.Serialization.Primitives (>= 4.3.0)
-
net7.0
- Newtonsoft.Json (>= 13.0.3)
- Serilog (>= 2.12.0)
- Serilog.Enrichers.Demystifier (>= 1.0.2)
- Serilog.Sinks.Console (>= 4.1.0)
- Serilog.Sinks.File (>= 5.0.0)
- System.ComponentModel.Annotations (>= 5.0.0)
- System.Runtime.Serialization.Primitives (>= 4.3.0)
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 | 150 | 3/25/2024 |
2.2.6 | 114 | 3/24/2024 |
2.2.5 | 103 | 3/24/2024 |
2.2.4 | 111 | 3/24/2024 |
2.2.3 | 124 | 3/24/2024 |
2.2.2 | 111 | 3/23/2024 |
2.2.1 | 118 | 3/22/2024 |
2.2.0 | 111 | 3/22/2024 |
2.1.2 | 126 | 3/21/2024 |
2.1.1 | 107 | 3/21/2024 |
2.1.0 | 111 | 3/21/2024 |
2.0.2 | 134 | 3/13/2024 |
1.2.2 | 122 | 3/11/2024 |
1.2.1 | 113 | 3/11/2024 |
1.2.0 | 108 | 3/11/2024 |
1.1.0 | 109 | 3/10/2024 |
1.0.1 | 188 | 6/17/2023 |
Initial release of the CoreUtils library