docker-compose部署LOKI
docker-compose.yaml
version: "3"
networks:
loki:
services:
loki:
image: grafana/loki:2.5.0
ports:
- "3100:3100"
command: -config.file=/etc/loki/local-config.yaml
networks:
- loki
promtail:
image: grafana/promtail:2.5.0
volumes:
- /var/log:/var/log
command: -config.file=/etc/promtail/config.yml
networks:
- loki
grafana:
image: grafana/grafana:latest
ports:
- "3000:3000"
networks:
- loki
当前目录执行命令
docker-compose up -d
配置数据源
启动后使用浏览器访问 http://localhost:3000
账号
admin
密码admin
登录成功后会提示修改密码
配置所用数据库
黄色代表我们配置成功
配置Asp.Net Core程序
使用到的库
- Serilog
- Serilog.Exceptions
- Serilog.Sinks.Grafana.Loki
初始化NLog时配置
Code
public static IWebHostBuilder UseSerilogDefault(this IWebHostBuilder hostBuilder)
{
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{environmentName}.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.Enrich.WithExceptionDetails()
.Enrich.WithMachineName()
.WriteTo.Debug()
.WriteTo.Console()
.WriteTo.GrafanaLoki("http://http:localhost:3100/")
// .WriteTo.Elasticsearch(ConfigureElasticSink(configuration, environmentName))
.Enrich.WithProperty("Client", "appClient")
.ReadFrom.Configuration(configuration)
.CreateLogger();
hostBuilder.UseSerilog();
return hostBuilder;
}
最终效果
日志浏览
筛选条件
效果图