-
Notifications
You must be signed in to change notification settings - Fork 65
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] Allow custom base type #167
Comments
Hi @cwuethrich, I have the same problem. I want to extend the Above code |
Hi @changemyminds I tried to make an example with Contoso again. Custom public class ContosoBase : Base
{
[DataMember(Name = "contoso")]
public Contoso Contoso { get; set; }
protected override void WriteAdditionalProperties(Action<string, object> write)
{
base.WriteAdditionalProperties(write);
write("contoso", Contoso);
}
protected override bool ReceiveProperty(string propertyName, object value)
{
if ("contoso".Equals(propertyName))
{
Contoso = value as Contoso;
return true;
}
return base.ReceiveProperty(propertyName, value);
}
protected override bool TryRead(string propertyName, out Type type)
{
if ("contoso".Equals(propertyName))
{
type = typeof(Contoso);
return true;
}
return base.TryRead(propertyName, out type);
}
}
public class Contoso
{
[DataMember(Name = "company")]
public string CompanyName { get; set; }
} Custom public class ContosoEcsTextFormatter : EcsTextFormatter
{
public ContosoEcsTextFormatter() : base(GetConfiguration()) { }
private static EcsTextFormatterConfiguration GetConfiguration()
{
var configuration = new EcsTextFormatterConfiguration();
configuration.MapCustom(MapCustom);
configuration.LogEventPropertiesToFilter(new HashSet<string>
{
"CompanyName",
});
return configuration;
}
private static Base MapCustom(Base ecsBase, LogEvent logEvent)
{
var contosoBase = CreateContosoBase(ecsBase);
void Assign<T>(T obj, string key, Action<T, object> assign)
where T : class, new()
{
if (!logEvent.TryGetScalarPropertyValue(key, out var value))
return;
assign(obj, value.Value);
}
contosoBase.Contoso = new Contoso();
Assign(contosoBase.Contoso, "CompanyName", (o, v) => o.CompanyName = v?.ToString());
return contosoBase;
}
private static ContosoBase CreateContosoBase(Base ecsBase)
{
return new ContosoBase
{
Metadata = ecsBase.Metadata,
Agent = ecsBase.Agent,
Client = ecsBase.Client,
Cloud = ecsBase.Cloud,
Container = ecsBase.Container,
Destination = ecsBase.Destination,
Dll = ecsBase.Dll,
Dns = ecsBase.Dns,
Ecs = ecsBase.Ecs,
Error = ecsBase.Error,
Event = ecsBase.Event,
File = ecsBase.File,
Group = ecsBase.Group,
Host = ecsBase.Host,
Http = ecsBase.Http,
Log = ecsBase.Log,
Observer = ecsBase.Observer,
Organization = ecsBase.Organization,
Package = ecsBase.Package,
Process = ecsBase.Process,
Registry = ecsBase.Registry,
Related = ecsBase.Related,
Rule = ecsBase.Rule,
Server = ecsBase.Server,
Service = ecsBase.Service,
Source = ecsBase.Source,
Threat = ecsBase.Threat,
Tls = ecsBase.Tls,
Url = ecsBase.Url,
User = ecsBase.User,
UserAgent = ecsBase.UserAgent,
Vulnerability = ecsBase.Vulnerability,
Timestamp = ecsBase.Timestamp,
Labels = ecsBase.Labels,
Message = ecsBase.Message,
Tags = ecsBase.Tags,
//Span = ecsBase.Span,
Trace = ecsBase.Trace,
Transaction = ecsBase.Transaction,
};
}
} |
This way users can implement and use their own EcsDocument subclasses as ITextFormatter Fixes #167
Thanks for raising this! Agreed this support for this usecase would be great. I've opened: #217 to implement this natively in the library. |
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 theBase
class.The Method
ConvertToEcs
is implemented with theBase
class. There is no other way to use the code in there with an other Type.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.With this change we can use a own base class and easily add new fields.
Describe alternatives you've considered
A problem here is the property
MapCustom
in the configuration of the formatter.If we accept breaking changes, we could even change this and return the custom base type in the converter.
Additional context
What do you think about this change? Is it worth it to create a pull request?
The text was updated successfully, but these errors were encountered: