前言
分布式日志是一种系统,其中日志消息可以在多个节点之间进行分布和复制。这样可以实现高可用性和容错性,避免单点故障和数据丢失。
在分布式系统中,节点之间需要协调和共享信息,日志就是其中重要的一种。通过记录节点之间的通信、状态变化等信息,可以方便地进行故障排除和系统调试。
常见的分布式日志系统包括Apache Kafka、Apache Flume、Log4j2等。这些系统通常具有高吞吐量、低延迟、可靠性高等特点,广泛应用于大规模数据处理、实时监控等方面。
一、RemoteLog的分布式日志
1.RemoteLog的下载和安装
RemoteLog程序下载地址:https://cccscls-my.sharepoint.com/:f:/g/personal/jack_mutc_ca/Et7VbP7sX31EiN-NQkPL0RgBL5RBG15_PyepR5Tx0PaqsQ?e=BTngox
下载对应操作系统的RemoteLogServer压缩文件,解压后,运行Jack.RemoteLog.WebApi.exe(windows)或者Jack.RemoteLog.WebApi(linux)
或者用Docker安装
docker pull jackframework/jackremotelogwebapi:latest
2.使用
2.1 安装包
Jack.RemoteLog
2.2 配置
在appsettings.json文件添加如下配置:
"Logging": {
"ServerUrl": "http://127.0.0.1:9000",
"ContextName": "YourContextName",
"LogLevel": {
"Default": "Debug"
},
"Console": {
"LogLevel": {
"Default": "Information"
}
}
}
注册 Jack.RemoteLog 为底层日志处理引擎
#region 配置分布式日志
builder.Services.AddLogging(bud =>
{
bud.AddConfiguration(builder.Configuration.GetSection("Logging"));
bud.AddConsole();
bud.UseJackRemoteLogger(builder.Configuration);
});
#endregion
如果 RemoteLog 服务器端设置了身份验证,则这里需要设置用户名、密码:
services.AddLogging(bud =>
{
bud .AddConfiguration(builder.Configuration.GetSection("Logging"));
bud .AddConsole();
bud .UseJackRemoteLogger(builder.Configuration , new Options
{
UserName = "",
Password = ""
});
});
2.3 使用
当要使用 ILogger 接口实例来记录信息时,该信息将被记录到日志服务器。
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
_logger.LogInformation("我是RemoteLog的分布式日志");
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
2.4 查看日志
而要查看服务器上的所有日志可以通过浏览器打开 http://127.0.0.1:9000,可以轻松地获取你的程序日志以进行监视和分析。