{"id":13451189,"url":"https://github.com/JosephWoodward/Serilog-Sinks-Loki","last_synced_at":"2025-03-23T18:31:55.645Z","repository":{"id":38807273,"uuid":"164086573","full_name":"josephwoodward/Serilog-Sinks-Loki","owner":"josephwoodward","description":"A Serilog Sink for Loki, Grafana's new Prometheus inspired log aggregator","archived":false,"fork":false,"pushed_at":"2022-12-08T09:01:59.000Z","size":2103,"stargazers_count":133,"open_issues_count":21,"forks_count":36,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-05-28T03:44:53.369Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/josephwoodward.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-01-04T09:25:11.000Z","updated_at":"2024-04-25T20:41:04.000Z","dependencies_parsed_at":"2023-01-24T21:45:45.187Z","dependency_job_id":null,"html_url":"https://github.com/josephwoodward/Serilog-Sinks-Loki","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josephwoodward%2FSerilog-Sinks-Loki","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josephwoodward%2FSerilog-Sinks-Loki/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josephwoodward%2FSerilog-Sinks-Loki/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josephwoodward%2FSerilog-Sinks-Loki/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/josephwoodward","download_url":"https://codeload.github.com/josephwoodward/Serilog-Sinks-Loki/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245149424,"owners_count":20568905,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T07:00:49.507Z","updated_at":"2025-03-23T18:31:55.599Z","avatar_url":"https://github.com/josephwoodward.png","language":"C#","funding_links":[],"categories":["C# #"],"sub_categories":[],"readme":"# Serilog.Sinks.Loki\n\n![.NET Core](https://github.com/JosephWoodward/Serilog-Sinks-Loki/workflows/.NET%20Core/badge.svg?branch=master)\n\nThis is a Serilog Sink for Grafana's new [Loki Log Aggregator](https://grafana.com/loki).\n\nWhat is Loki?\n\n\u003e Loki is a horizontally-scalable, highly-available, multi-tenant log aggregation system inspired by Prometheus. It is designed to be very cost effective and easy to operate, as it does not index the contents of the logs, but rather a set of labels for each log stream.\n\nYou can find more information about what Loki is over on [Grafana's website here](https://grafana.com/loki).\n\n![Loki Screenshot](https://raw.githubusercontent.com/JosephWoodward/Serilog-Sinks-Loki/master/assets/screenshot2.png)\n\n## Current Features:\n\n- Formats and batches log entries to Loki via HTTP\n- Ability to provide global Loki log labels\n- Comes baked with an HTTP client, but your own can be provided\n- Provides contextual log labels\n\nComing soon:\n\n- Write logs to disk in the correct format to send via Promtail\n- Send logs to Loki via HTTP using Snappy compression\n\n## Installation\n\nThe Serilog.Sinks.Loki NuGet [package can be found here](https://www.nuget.org/packages/Serilog.Sinks.Loki/). Alternatively you can install it via one of the following commands below:\n\nNuGet command:\n```bash\nInstall-Package Serilog.Sinks.Loki\n```\n.NET Core CLI:\n```bash\ndotnet add package Serilog.Sinks.Loki\n```\n\n## Basic Example:\n\n```csharp\n// var credentials = new BasicAuthCredentials(\"http://localhost:3100\", \"\u003cusername\u003e\", \"\u003cpassword\u003e\");\nvar credentials = new NoAuthCredentials(\"http://localhost:3100\"); // Address to local or remote Loki server\n\nLog.Logger = new LoggerConfiguration()\n        .MinimumLevel.Information()\n        .Enrich.FromLogContext()\n        .WriteTo.LokiHttp(credentials)\n        .CreateLogger();\n\nvar exception = new {Message = ex.Message, StackTrace = ex.StackTrace};\nLog.Error(exception);\n\nvar position = new { Latitude = 25, Longitude = 134 };\nvar elapsedMs = 34;\nLog.Information(\"Message processed {@Position} in {Elapsed:000} ms.\", position, elapsedMs);\n\nLog.CloseAndFlush();\n```\n\n### Adding global labels\n\nLoki indexes and groups log streams using labels, in Serilog.Sinks.Loki you can attach labels to all log entries by passing an implementation `ILogLabelProvider` to the `WriteTo.LokiHttp(..)` configuration method. This is ideal for labels such as instance IDs, environments and application names.\n\n__See [Serilog.Sinks.Loki.Example/LogLabelProvider.cs](https://github.com/JosephWoodward/Serilog-Sinks-Loki/blob/master/src/Serilog.Sinks.Loki.Example/LogLabelProvider.cs)__ for a basic `LogLabelProvider` implementation.\n\n```csharp\nvar credentials = new BasicAuthCredentials(\"http://localhost:3100\", \"\u003cusername\u003e\", \"\u003cpassword\u003e\");\nvar log = new LoggerConfiguration()\n        .MinimumLevel.Verbose()\n        .Enrich.FromLogContext()\n        .WriteTo.LokiHttp(credentials, new LogLabelProvider())\n        .CreateLogger();\n```\n\n### Local, contextual labels\n\nIn some occasions you'll want to add context to your log stream within a particular class or method, this can be achieved using contextual labels:\n\n```csharp\nusing (LogContext.PushProperty(\"A\", 1))\n{\n    log.Warning(\"Warning with Property A\");\n    log.Fatal(\"Fatal with Property A\");\n}\n```\n\n### Custom HTTP Client\n\nSerilog.Loki.Sink is built on top of the popular [Serilog.Sinks.Http](https://github.com/FantasticFiasco/serilog-sinks-http) library to post log entries to Loki. With this in mind you can you can extend the default HttpClient (`LokiHttpClient`), or replace it entirely by implementing `IHttpClient`.\n\n```csharp\n// ExampleHttpClient.cs\n\npublic class ExampleHttpClient : LokiHttpClient\n{\n    public override Task\u003cHttpResponseMessage\u003e PostAsync(string requestUri, HttpContent content)\n    {\n        return base.PostAsync(requestUri, content);\n    }\n}\n```\n```csharp\n// Usage\n\nvar credentials = new BasicAuthCredentials(\"http://localhost:3100\", \"\u003cusername\u003e\", \"\u003cpassword\u003e\");\nvar log = new LoggerConfiguration()\n        .MinimumLevel.Verbose()\n        .Enrich.FromLogContext()\n        .WriteTo.LokiHttp(credentials, new LogLabelProvider(), new ExampleHttpClient())\n        .CreateLogger();\n```\n\n### Configure using appsettings.json\n`Serilog-Sinks-Loki` can be configured with `appsettings.json` using `Serilog.Settings.Configuration`.  \nIt support the following arguments `serverUrl`, `username`, `password`, `credentials`, `labelProvider`, `httpClient`, `outputTemplate` and `formatProvider`.  \nNot all fields can be used in combination look in [LokiSinkExtensions.cs](src/Serilog.Sinks.Loki/LokiSinkExtensions.cs) for the supported combinations.  \n`credentials`, `labelProvider`, `httpClient`, and `formatProvider` are classes and must be specified using the `Namespace.ClassName, Assembly` syntax.\n```json\n\"Serilog\": {\n  \"Using\": [ \"Serilog.Sinks.Loki\" ],\n  \"MinimumLevel\": {\n    \"Default\": \"Verbose\"\n  },\n  \"Enrich\": [ \"FromLogContext\" ],\n  \"WriteTo\": [      \n    {\n      \"Name\": \"LokiHttp\",\n      \"Args\": {\n        \"serverUrl\": \"https://loki:3000\",\n        \"labelProvider\": \"Namespace.ClassName, Assembly\"\n      }\n    }\n  ]\n}\n```\n\n### Missing a feature or want to contribute?\nThis package is still in its infancy so if there's anything missing then please feel free to raise a feature request, either that or pull requests are most welcome!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJosephWoodward%2FSerilog-Sinks-Loki","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FJosephWoodward%2FSerilog-Sinks-Loki","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJosephWoodward%2FSerilog-Sinks-Loki/lists"}