1.下载安装Grafana
官网:https://grafana.com/grafana/download?platform=windows
使用安装版本即可,自动创建windows服务。
安装完成之后,配置文件在相对路径./conf下面,一般情况下默认即可。
2.下载安装 promtail和loki
安装文档:https://grafana.com/docs/loki/latest/installation/local/
安装包发布地址:https://github.com/grafana/loki/releases
选择Windows版本进行下载。
2.1.loki配置文件
新建loki-local-config.yaml
auth_enabled: falseserver:http_listen_port: 3100ingester:lifecycler:address: 127.0.0.1ring:kvstore:store: inmemoryreplication_factor: 1final_sleep: 0schunk_idle_period: 5mchunk_retain_period: 30smax_transfer_retries: 0schema_config:configs:- from: 2020-06-22store: boltdbobject_store: filesystemschema: v11index:prefix: index_period: 168hstorage_config:boltdb:directory: D:\pulish\grafana-loki\tmp\loki\indexfilesystem:directory: D:\pulish\grafana-loki\tmp\loki\chunkslimits_config:enforce_metric_name: falsereject_old_samples: truereject_old_samples_max_age: 168hchunk_store_config:max_look_back_period: 0stable_manager:retention_deletes_enabled: falseretention_period: 0s
配置文件:https://grafana.com/docs/loki/latest/configuration/
2.2.promtail配置文件
新建promtail-local-config.yaml
server:http_listen_port: 9080grpc_listen_port: 0positions:filename: D:\pulish\grafana-loki\tmp\positions.yamlclients:- url: http://localhost:3100/loki/api/v1/pushscrape_configs:
配置文件:
https://grafana.com/docs/loki/latest/getting-started/get-logs-into-loki/
https://grafana.com/docs/loki/latest/clients/promtail/configuration/
2.3.安装
在对应的安装目录下,使用cmd命令行工具直接加载配置启动即可
lokiloki-windows-amd64.exe --config.file=loki-local-config.yaml
promtailpromtail-windows-amd64.exe --config.file=promtail-local-config.yaml
但是,这样可能重启就不行,我们做成windows服务,这样更好一点。
使用**winsw**工具将exe制作成服务。
通过https://github.com/winsw/winsw/releases)下载对应的版本。
promteil实例配置,命名为promtail-windows-server.xml
<service><id>promtail</id><name>promtail</name><description>promtail</description><executable>promtail-windows-amd64.exe</executable><arguments> --config.file=promtail-local-config.yaml</arguments></service>
然后将下载下来的WinSw-X64.exe重命名为promtail-windows-server.exe
在此路径下启动cmd命令行工具
输入promtail-windows-server.exe install即可安装服务

可以制作成.bat文件,使用install和start命令快捷启动。
更多命令见:https://github.com/winsw/winsw#usage
3.启动程序和配置数据源
通过http://localhost:3000/即可进入首页。首次登录默认账号密码都是admin
在Configuration下Data sources配置loki


配置完成之后,进入Explore即可查看。
4.使用.NET Core使用Serilog将日志发送到Loki
通过Nuget
安装Serilog.AspNetCoreSerilog.Sinks.Grafana.Loki
public static IHostBuilder CreateHostBuilder(string[] args) =>Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>{webBuilder.UseStartup<Startup>();}).UseSerilog((context, config) =>{config.Enrich.FromLogContext().Enrich.WithProperty("app", context.HostingEnvironment.ApplicationName).WriteTo.GrafanaLoki("http://localhost:3100").WriteTo.Console();});
