corentinaltepe / nlog.loki

NLog target for Loki using an HTTP client
BSD 3-Clause "New" or "Revised" License
22 stars 7 forks source link

Implemented handling for sendLastFormatParameter option in Loki label… #88

Closed suntereo closed 1 year ago

suntereo commented 1 year ago

Enhancement of Loki Fields Rendering with sendLastFormatParameter and Custom Fields from Properties

This pull request introduces two significant features that align NLog.Loki with the behavior observed in NLog.Web.AspNetCore.Targets.Gelf:

  1. Implementation of the sendLastFormatParameter option: This feature enables the last parameter of a log message format to be sent to Loki as separate fields per property.

    Example:

    // using simple anonymous type object to create custom fields for a log entry
    log.Info($"Testing sendLastFormatParameter", new { publisher = "ACME Publisher", releaseDate = DateTime.Now });
  2. Reading custom fields from event properties: Like Gelf target, this feature reads custom fields from the log event properties.

    Example:

    // using additional properties to create custom fields for a log entry
    var eventInfo = new LogEventInfo
    {
       Message = "Testing additional properties inside LogEventInfo",
       Level = LogLevel.Info,
    };
    eventInfo.Properties.Add("publisher", "ACME Publisher");
    eventInfo.Properties.Add("releaseDate", DateTime.Now);
    log.Log(eventInfo);

These enhancements significantly streamline the migration process from Graylog to Loki. Prior to this update, programmers faced a time-consuming and error-prone task of identifying and modifying many lines of code that attempted to pass additional data. The difficulty of this process increased the risk that some data might be lost without notice, as the previous implementation didn't throw an error for such occurrences. This update alleviates these concerns, making the transition smoother and more reliable.

corentinaltepe commented 1 year ago

This is great! Thank you very much. I'll run the code, document the new option in the Readme, merge the PR and publish a new version of the package within the day.

suntereo commented 1 year ago

Thank you!

corentinaltepe commented 1 year ago

Sorry for the delay again.

I've the new feature in the template projet. It works well. image

Though it fails if the last parameter is a simple type such as a string. I'll try to warn users of this case in the readme. Thank you!