Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Elastic.Serilog.Sinks - Allow specifying a formatter for the message string #330

Open
am-zq opened this issue Aug 5, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@am-zq
Copy link

am-zq commented Aug 5, 2023

ECS integration/library project(s):
Elastic.Serilog.Sinks

Is your feature request related to a problem? Please describe.
DateOnly and DateTime objects are formatted in the log message using ambiguous formatting, and it does not seem possible to specify a custom formatter.

Consider the following:

_logger.LogInformation("DateOnly: {@date}", dateTest);
_logger.LogInformation("DateTime: {@datetime}", dateTimeTest);

This results in two documents in ElasticSearch with the following strings in the message property:

DateOnly: 01/03/2003
DateTime: 01/03/2003 23:59:59

This could be either dd/mm or mm/dd (and perhaps which one it is varies depending on the locale of the system where I am running my application). I would like dates to be formatted in ISO 8601 for consistency and to facilitate further analysis of log messages from ElasticSearch.

For comparison, with the Serilog Console logger I can pass in a custom formatter:

.WriteTo.Console(
        formatProvider: new ISO8601DateFormatter(),
        ...

The logs are rendered according to the formatter:

DateOnly: 2003-01-03
DateTime: 2003-01-03 23:59:59

Describe the solution you'd like
Allow passing in a formatProvider like the other Serilog sinks do

Describe alternatives you've considered

  • Expanding the documents in Kibana to look at the fields corresponding to the date/datetimes (the fields are rendered in ISO 8601), however this is very time consuming
  • Possibly switching to Serilog.Sinks.Elasticsearch since this appears to support the formatProvider
@am-zq am-zq added the enhancement New feature or request label Aug 5, 2023
@am-zq
Copy link
Author

am-zq commented Sep 17, 2023

I think this is the relevant line:

ecsEvent.Message = logEvent.RenderMessage();

RenderMessage is this method of Serilog LogEvent:
public string RenderMessage(IFormatProvider? formatProvider = null)

I suspect that passing in a IFormatProvider here (instead of null) would work. Perhaps include IFormatProvider in IEcsTextFormatterConfiguration? Then it could be set on ElasticsearchSinkOptions.TextFormatting during application start, e.g.

.WriteTo.Elasticsearch(
                new[] { new Uri(elasticUri) },
                opts =>
                {
                    opts.DataStream = new DataStreamName("logs", envName, "IBDataService");
                    opts.BootstrapMethod = BootstrapMethod.Failure;
                    opts.TextFormatting = new EcsTextFormatterConfiguration
                    {
                        FormatProvider = new ISO8601DateFormatter(),
                    };
                });

Does this approach seem reasonable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant