Description
ECS integration/library project(s) (e.g. Elastic.CommonSchema.Serilog):
Elastic.CommonSchema.Serilog
Is your feature request related to a problem? Please describe.
We are extending the ECS with our own fields. Let's use the Microsoft example company Contoso. We are extending the ECS with a new group named contoso
. The current Serilog Converter doesn't support any Extension to the Base
class.
The Method ConvertToEcs
is implemented with the Base
class. There is no other way to use the code in there with an other Type.
public static Base ConvertToEcs(LogEvent logEvent, IEcsTextFormatterConfiguration configuration)
{
...
var ecsEvent = new Base();
...
return ecsEvent;
}
Describe the solution you'd like
Without breaking changes we could at least change this to a method where it is possible to extend the Base
class and add the own fields with casting in a other base type.
public static Base ConvertToEcs(LogEvent logEvent, IEcsTextFormatterConfiguration configuration)
=> ConvertToEcs<Base>(logEvent, configuration);
public static Base ConvertToEcs<T>(LogEvent logEvent, IEcsTextFormatterConfiguration configuration)
where T : Base, new()
{
...
var ecsEvent = new T();
...
return ecsEvent;
}
With this change we can use a own base class and easily add new fields.
public class ContosoBase : Base
{
[DataMember(Name = "contoso")]
public Contoso Contoso { get; set; }
}
public class Contoso
{
[DataMember(Name = "company_name")]
public string CompanyName { get; set; }
}
public class ContosoEcsTextFormatter : EcsTextFormatter
{
public override void Format(LogEvent logEvent, TextWriter output)
{
var ecsEvent = LogEventConverter.ConvertToEcs<ContosoBase>(logEvent, _configuration);
if (ecsEvent is ContosoBase contosoEcsEvent)
{
contosoEcsEvent.Contoso = new Contoso
{
CompanyName = "Contoso",
};
}
output.WriteLine(ecsEvent.Serialize());
}
}
Describe alternatives you've considered
A problem here is the property MapCustom
in the configuration of the formatter.
public interface IEcsTextFormatterConfiguration
{
Func<Base, LogEvent, Base> MapCustom { get; set; }
...
}
If we accept breaking changes, we could even change this and return the custom base type in the converter.
public static T ConvertToEcs<T>(LogEvent logEvent, IEcsTextFormatterConfiguration configuration)
where T : Base, new()
{
...
}
Additional context
What do you think about this change? Is it worth it to create a pull request?