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程序
使用到的库
- Nlog
- Nlog.Targets.Loki
初始化NLog时配置
Code
static Logger()
{
var config = new NLog.Config.LoggingConfiguration();
string layout = LoggerConfig.Layout;
AddTarget(new LokiTarget()
{
Name = "loki",
BatchSize = 200,
TaskDelayMilliseconds = 500,
Endpoint = "http://122.112.248.242:3101/",
OrderWrites = true,
CompressionLevel=CompressionLevel.NoCompression,
Layout = layout,
Labels = { new LokiTargetLabel()
{
Name= "Client",
Layout = "AppApi"
}
}
});
NLog.LogManager.Configuration = config;
void AddTarget(NLog.Targets.Target target)
{
config.AddTarget(target);
config.AddRuleForAllLevels(target);
}
}
配置文件配置
Nlog.config
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<extensions>
<add assembly="NLog.Loki" />
</extensions>
<targets>
<target
name="loki"
xsi:type="loki"
batchSize="200"
taskDelayMilliseconds="500"
endpoint="http://localhost:3100"
username="myusername"
password="secret"
orderWrites="true"
compressionLevel="noCompression"
layout="${level}|${message}${onexception:|${exception:format=type,message,method:maxInnerExceptionLevel=5:innerFormat=shortType,message,method}}|source=${logger}">
<label name="app" layout="my-app-name" />
<label name="server" layout="${hostname:lowercase=true}" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="loki" />
</rules>
</nlog>
最终效果
日志浏览
筛选条件
效果图