Skip to content

Commit 5183bb6

Browse files
authored
Merge pull request #374 from askrinnik/GrafanaLoki
Logging to Grafana Loki
2 parents 7cb7fe1 + 77fd379 commit 5183bb6

File tree

6 files changed

+93
-2
lines changed

6 files changed

+93
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Greetings from Cairo, Egypt. You can [sponsor](https://github.com/sponsors/dodyg
3030
| [Health Check](/projects/health-check) | 6 | |
3131
| [IHttpClientFactory](/projects/httpclientfactory) | 4 | |
3232
| [IHostedService](/projects/ihosted-service) | 2 | |
33-
| [Logging](/projects/logging) | 3 | |
33+
| [Logging](/projects/logging) | 4 | |
3434
| [Localization and Globalization](/projects/localization) | 6 | |
3535
| [Middleware](/projects/middleware) | 14 | |
3636
| [Mini Apps](/projects/mini) | 2 | |

projects/logging/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Logging (3)
1+
# Logging (4)
22

33
* [Basic Logging](/projects/logging/logging-1)
44

@@ -12,4 +12,8 @@
1212

1313
This example shows how to display log to console in structured JSON logs.
1414

15+
* [Logging to Grafana Loki](/projects/logging/logging-Loki)
16+
17+
This example shows how to log to [Grafana Loki](https://grafana.com/oss/loki/).
18+
1519
dotnet6
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using LokiLoggingProvider.Options;
2+
3+
var builder = WebApplication.CreateBuilder(args);
4+
5+
builder.Logging.AddLoki(configure =>
6+
{
7+
configure.Client = PushClient.Grpc;
8+
configure.StaticLabels.JobName = "LokiWebApplication";
9+
});
10+
11+
var app = builder.Build();
12+
13+
app.MapGet("/test", (ILoggerFactory loggerFactory) =>
14+
{
15+
var logger = loggerFactory.CreateLogger("Start");
16+
logger.LogTrace("Trace message");
17+
logger.LogDebug("Debug message");
18+
logger.LogInformation("Information message");
19+
logger.LogWarning("Warning message");
20+
logger.LogError("Error message");
21+
logger.LogCritical("Critical message");
22+
return "OK";
23+
});
24+
25+
app.Run();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Log into Grafana Loki
2+
3+
This example shows how to log to [Grafana Loki](https://grafana.com/oss/loki/). The Docker is used for the software hosting.
4+
5+
- open a command-line window in the folder and execute:
6+
7+
```sh
8+
docker compose up
9+
```
10+
- make sure that [Grafana Loki](https://grafana.com/oss/loki/) and [Grafana Dashboard](https://grafana.com/grafana/) containers are succesfully installed
11+
- Compile and run the application
12+
```sh
13+
dotnet build
14+
dotnet run
15+
```
16+
- Navigate to /test endpoint. The response should be 'OK'. The console window should contain **info**, **warn**, **fail** and **crit** log messages.
17+
- Navigate in a browser to the Grafana Dashboard website: <http://localhost:3000/>
18+
- Add a loki data source <http://loki:3100>
19+
- Go to the 'Explore data' page and add Label filter: ``job="LokiWebApplication"``. Press the 'Run query' button
20+
- Log messages should be shown
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
version: '3.7'
2+
3+
volumes:
4+
grafana: ~
5+
loki: ~
6+
7+
services:
8+
loki:
9+
image: "grafana/loki:latest"
10+
hostname: logs
11+
command: -config.file=/etc/loki/local-config.yaml
12+
volumes:
13+
- loki:/loki
14+
ports:
15+
- "3100:3100" # http and Grafana data source
16+
- "9095:9095" # grpc
17+
grafana:
18+
image: grafana/grafana-oss:latest-ubuntu
19+
hostname: grafana
20+
ports:
21+
- "3000:3000" #UI
22+
volumes:
23+
- grafana:/var/lib/grafana
24+
environment:
25+
- GF_AUTH_ANONYMOUS_ENABLED=true
26+
- GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
27+
- GF_AUTH_DISABLE_LOGIN_FORM=true
28+
depends_on:
29+
- loki
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="LokiLoggingProvider" Version="1.0.0" />
11+
</ItemGroup>
12+
13+
</Project>

0 commit comments

Comments
 (0)