A cross-platform C# lib with what is needed in the most applications. Settings manager. Logger. Auxiliary routines.
Introduction
CliverRoutines
contains:
- application settings manager superseding .NET
ConfigurationManager
; - logger with threading and session support;
- auxiliary routines;
CliverRoutines
has been developed in .NET Standard 2.0 and is supposed to run on any platform adopting C# including .NET, Xamarin, Mono.
This article is a brief overview. For details, refer to the documentation and the source code.
Background
Every time I was using .NET ConfigurationManager
, it was a pain whether it was needed to store custom types, or initialize settings with long data, or override settings. In general, trying to make ConfigurationManager
do something custom, throws into awkward coding full of restrictions. Not to mention that editing settings via the Visual Studio settings table is terribly unhandy.
That's why years ago, I developed the settings manager Cliver.Config
. The idea behind it appeared so suitable that since then, I have never looked back. It has been used in many apps of any complexity on Windows and Xamarin.Mac
and always proved to be simple in use, capable and robust.
Also, CliverRoutines
exposes the logger Cliver.Log
and some auxiliary routines.
Config
Cliver.Config
is application settings manager intended as a replacement for .NET ConfigurationManager
.
Features
- Cross-platform
- Enables setting types of any complexity and functionality
- Settings are easily modified directly in code
- Enables settings polymorphism
- Thread-safe
- Serializes data in JSON
Idea
Settings types are ordinary C# classes that you define in your code according to your needs thus achieving a great flexibility. Cliver.Config
automatically detects fields/properties of those types in your code and facilitates their serialization/deserialization to/from disk.
While Cliver.Config
was designed primarily as a settings manager, in conjunction with System.Linq
, it can be used as a simple nosql database.
Usage
Reference CliverRoutines
in your project.
Set your project's company name because it co-defines the storage directory.
Define settings types according to your needs. Generally, a settings type is an ordinary class that inherits from Cliver.Settings
or one of its derivatives, e.g. Cliver.UserSettings
.
Public non-static fields or properties in such a custom defined settings class will be automatically serialized/deserialized by command.
See the example of a settings type:
public class GeneralSettings : Cliver.UserSettings
{
public int Host;
public int Port = 25;
public List<Client> Clients = new List<Client>();
}
public class Client
{
public string Name;
public string Email;
}
Where you need it, declare a static field or property whose type is the defined settings type:
public class Settings
{
public static readonly GeneralSettings General;
}
Add this call at the beginning of your application, to make Cliver.Config
detect all such fields/properties in your code and initialize them:
Cliver.Config.Reload();
Now settings are ready to be used:
Settings.General.Host = "smtp.server.com";
Settings.General.Clients.Add(new Client {Name = "Tom", Email = "tom@company.com"});
...
Settings.General.Save();
Settings.General.Reload();
Settings.General.Reset();
...
Client client = Settings.General.Clients.Find(a => a.Name == "Tom");
notify(Settings.General.Host, Settings.General.Port, client.Email);
Live examples can be found in CliverRoutinesExample
project in CliverRoutines
solution.
For the complete usage options, review Cliver.Config
API.
Log
Cliver.Log
is logger designed with usability in mind.
Features
- Cross-platform
- Thread-safe
- Session oriented - an application can write multiple log sessions successively or simultaneously. It is helpful when an application performs multiple independent tasks.
- Thread oriented - it can automatically write a log per thread
- Auto-cleanup of old logs
- Diagnostic output
Usage
Reference CliverRoutines
in your project.
Set your project's company name because it co-defines the log directory.
At the beginning of the application, add optional initialization:
using Cliver;
...
Log.Initialize(Log.Mode.FOLDER_PER_SESSION);
Trivial logging when everything is written to the same file:
Log.Inform("test message");
Logging to a named log of an explicitly created session:
Log.Session game1Session = Log.Session.Get("Game1");
game1Session.Warning("test message");
game1Session["Test"].Warning("test message");
In this example, each thread writes its own log:
using Cliver;
...
static void download(string uri)
{
try
{
Log.Thread.Inform("test message");
}
catch (Exception e)
{
Log.Thread.Error2(e);
}
}
static void Main(string[] args)
{
ThreadRoutines.Start(() => { download("http://file.com/1"); });
...
ThreadRoutines.Start(() => { download("http://file.com/N"); });
}
Live examples can be found in CliverRoutinesExample
project in CliverRoutines
solution.
For complete usage options, review Cliver.Log
API.
Enjoy!
History
- 3rd February, 2021: Initial version