serilog-contrib / serilog-sinks-grafana-loki

A Serilog sink sending log events to Grafana Loki
MIT License
198 stars 30 forks source link

Dear author: I’d like to know that how to use serilog.loki to establish a customizing client to make an index for writing logs as Elasticsearch. #15

Closed qiqistart closed 3 years ago

qiqistart commented 3 years ago

Dear author: I’d like to know that how to use serilog.loki to establish a customizing client to make an index for writing logs as Elasticsearch.

mishamyte commented 3 years ago

Hello, @huzitao!

Sorry, can't get about what index do u talk? Do u mean a search index, used for indexation of log entries?

qiqistart commented 3 years ago

yes

mishamyte commented 3 years ago

Main principles of Loki differs from a principles of Elastic Search.

Traditional systems like ELK parse log strings entirely, including fields with a lot of unique values (for example, UserId from log entry) and save parsed data into big indexes. It has advantage that queries could be done fast, cause all required data is in the index. But the disadvantage is an index grows and become very large (in most cases size of the index could be almost equal to a logs size).

Loki works in another way. It's approach requires only most critical data must be extracted into index (called labels). So u have a possibility to find data, using filtration by datetime & labels. And the u could do an additional filtering with LogQL (it's kinda a regex & substring-based language).

U could find more details in Loki documentation.

So, for that approach you should determine most important fields for indexing and setup the sync with filtrationMode Include and filtrationLabels list.

For example:

ILogger logger = new LoggerConfiguration()
    .WriteTo.GrafanaLoki(
          "http://localhost:3100",
           filtrationMode: LokiLabelFiltrationMode.Include,
           filtrationLabels: new [] {"RequestId", "ClientIp", "SomeImportantLogValue"})
    .CreateLogger();

More examples of filtering are here in tests

qiqistart commented 3 years ago

thank you very much

qiqistart commented 3 years ago

Thank you for your help.  Meanwhile, i have another question. Does the Loki client support custom labels to write logs?I need to write logs by custom labels at present.

mishamyte commented 3 years ago

@huzitao yes, you could use a global labels. They would append to every log entries from configured service.

ILogger logger = new LoggerConfiguration()
    .WriteTo.GrafanaLoki(
          "http://localhost:3100",
            labels: new [] { new LokiLabel { Key = "app", Value = "payment"}})
    .CreateLogger();

You could find all options in project wiki

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue.