65.9K
CodeProject 正在变化。 阅读更多。
Home

CliverRoutines

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.54/5 (4投票s)

2021 年 2 月 3 日

MIT

3分钟阅读

viewsIcon

5145

应用程序设置管理器。记录器。辅助程序。

引言

CliverRoutines 包含

  • 应用程序设置管理器,取代 .NET ConfigurationManager;
  • 带有线程和会话支持的记录器;
  • 辅助程序;

CliverRoutines 是在 .NET Standard 2.0 中开发的,应该可以在任何采用 C# 的平台上运行,包括 .NET、Xamarin、Mono。

本文是一个简要概述。 详情请参考文档源代码

背景

每次我使用 .NET ConfigurationManager 时,它都很痛苦,无论是需要存储自定义类型、使用长数据初始化设置,还是覆盖设置。 总的来说,试图让 ConfigurationManager 做一些自定义的事情,会陷入充满限制的尴尬编码。 更不用说通过 Visual Studio 设置表编辑设置非常不方便了。

这就是为什么多年前我开发了设置管理器 Cliver.Config。它背后的想法显得如此合适,以至于从那时起,我就再也没有回头看过。它已被用于 Windows 和 Xamarin.Mac 上的任何复杂性的许多应用程序中,并且始终证明使用简单、功能强大且健壮。

此外,CliverRoutines 还公开了记录器 Cliver.Log 和一些辅助程序。

配置

Cliver.Config 是应用程序设置管理器,旨在替代 .NET ConfigurationManager

特点

  • 跨平台
  • 支持设置任何复杂性和功能的类型
  • 设置可以在代码中轻松直接修改
  • 支持设置多态性
  • 线程安全
  • 以 JSON 格式序列化数据

想法

设置类型是普通的 C# 类,您可以根据需要,在代码中定义它们,从而实现极大的灵活性。 Cliver.Config 自动检测代码中这些类型的字段/属性,并促进它们与磁盘之间的序列化/反序列化。

虽然 Cliver.Config 主要设计为设置管理器,但与 System.Linq 结合使用,它可以作为简单的 nosql 数据库。

用法

在你的项目中引用 CliverRoutines
设置您的项目公司名称,因为它共同定义了存储目录。

根据您的需要定义设置类型。 一般来说,设置类型是一个普通的类,它继承自 Cliver.Settings 或其派生类之一,例如 Cliver.UserSettings

在这种自定义定义的设置类中的公共非静态字段或属性将通过命令自动序列化/反序列化。

查看设置类型的示例

//settings type
public class GeneralSettings : Cliver.UserSettings //serialized to the 
                                                   //user-specific data directory
{
	public int Host; //it can be a property
	public int Port = 25;
	public List<Client> Clients = new List<Client>();
}

public class Client
{
	public string Name;
	public string Email;
}

在需要的地方,声明一个静态字段或属性,其类型是定义的设置类型

public class Settings
{
	public static readonly GeneralSettings General;  //readonly is optional. 
                                                     //It can be a property.
}

在应用程序的开头添加此调用,以使 Cliver.Config 检测代码中的所有此类字段/属性并初始化它们

//it can be called multiple times; every call re-initializes settings
Cliver.Config.Reload();   //mandatory call before using settings 

现在设置已准备好使用

//modify
Settings.General.Host = "smtp.server.com";
Settings.General.Clients.Add(new Client {Name = "Tom", Email = "tom@company.com"});
...
//save on disk
Settings.General.Save();
//or, restore the previous values instead of saving the new ones
Settings.General.Reload();
//or, reset to the initial values
Settings.General.Reset();
...
//pass to a function
Client client = Settings.General.Clients.Find(a => a.Name == "Tom");
notify(Settings.General.Host, Settings.General.Port, client.Email);

实时示例可以在 CliverRoutines 解决方案中的 CliverRoutinesExample 项目中找到。
有关完整的用法选项,请查看 Cliver.Config API。

Log

Cliver.Log 是以可用性为宗旨设计的记录器。

特点

  • 跨平台
  • 线程安全
  • 面向会话 - 应用程序可以连续或同时写入多个日志会话。 当应用程序执行多个独立任务时,它很有帮助。
  • 面向线程 - 它可以自动为每个线程编写日志
  • 自动清理旧日志
  • 诊断输出

用法

在你的项目中引用 CliverRoutines
设置您的项目公司名称,因为它共同定义了日志目录。

在应用程序的开头,添加可选的初始化

using Cliver;
...

//it can be called many times; every call closes all the logs and re-initializes the engine.
Log.Initialize(Log.Mode.FOLDER_PER_SESSION);  //this call is not mandatory 
                                              //if you are ok with default settings.

当所有内容都写入同一个文件时,进行简单的日志记录

Log.Inform("test message");

记录到显式创建的会话的命名日志

//writing to the session "Game1"
Log.Session game1Session = Log.Session.Get("Game1");

//to the main log
game1Session.Warning("test message");

//to the log named "Test"
game1Session["Test"].Warning("test message");

在此示例中,每个线程写入自己的日志

using Cliver;
...

//each download thread writes its own log
static void download(string uri)
{
	try
	{		
		Log.Thread.Inform("test message"); 
	}
	catch (Exception e)
	{
		Log.Thread.Error2(e);
	}
}

static void Main(string[] args)
{
	//launch a number of parallel theads
	ThreadRoutines.Start(() => { download("http://file.com/1"); });
	...
	ThreadRoutines.Start(() => { download("http://file.com/N"); });
}

实时示例可以在 CliverRoutines 解决方案中的 CliverRoutinesExample 项目中找到。
有关完整的用法选项,请查看 Cliver.Log API。

尽情享用!

历史

  • 2021 年 2 月 3 日:初始版本
© . All rights reserved.