Skip to content

Commit

Permalink
Merge branch 'main' into nlog-metadata-protection
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored Oct 22, 2022
2 parents 7a2bd5a + 3f371c8 commit 4863992
Show file tree
Hide file tree
Showing 17 changed files with 422 additions and 182 deletions.
8 changes: 4 additions & 4 deletions examples/aspnetcore-with-serilog/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public static IWebHost BuildWebHost(string[] args) =>

// Ensure HttpContextAccessor is accessible
var httpAccessor = ctx.Configuration.Get<HttpContextAccessor>();

// Create a formatter configuration to se this accessor
var formatterConfig = new EcsTextFormatterConfiguration();
formatterConfig.MapHttpContext(httpAccessor);
formatterConfig.MapHttpAdapter = new HttpAdapter(httpAccessor);

// Write events to the console using this configration
var formatter = new EcsTextFormatter(formatterConfig);

Expand Down Expand Up @@ -61,4 +61,4 @@ public static void Main(string[] args)
}
}
}
}
}
1 change: 1 addition & 0 deletions src/Elastic.CommonSchema.Generator/FileGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static void Generate(CommonSchemaTypesProjection commonSchemaTypesProject
{
{ m => Generate(m, "EcsDocument"), "Base ECS Document" },
{ m => Generate(m, "EcsDocumentJsonConverter"), "Base ECS Document Json Converter" },
{ m => Generate(m, "EcsJsonContext"), "Ecs System Text Json Source Generators" },
{ m => Generate(m, "FieldSets"), "Field Sets" },
{ m => Generate(m, "Entities"), "Entities" },
{ m => Generate(m, "InlineObjects"), "Inline Objects" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace Elastic.CommonSchema.Serialization
@foreach (var entity in Model.EntityClasses)
{
var entityName = entity.BaseFieldSet.FieldSet.Name;
<text> "@entityName" => ReadProp<@(entity.Name)>(ref reader, "@entityName", ecsEvent, (b, v) => b.@(entity.Name) = v),
<text> "@entityName" => ReadProp<@(entity.Name)>(ref reader, "@entityName", EcsJsonContext.Default.@(entity.Name), ecsEvent, (b, v) => b.@(entity.Name) = v),
</text>}
_ =>
typeof(@(Model.Base.Name)) == ecsEvent.GetType()
Expand Down Expand Up @@ -102,7 +102,7 @@ namespace Elastic.CommonSchema.Serialization
@foreach (var entity in Model.EntityClasses)
{
var entityName = entity.BaseFieldSet.FieldSet.Name;
<text> WriteProp(writer, "@(entityName)", value.@(entity.Name));
<text> WriteProp(writer, "@(entityName)", value.@(entity.Name), EcsJsonContext.Default.@(entity.Name));
</text>
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@using System
@using Generator
@inherits Elastic.CommonSchema.Generator.Views.CodeTemplatePage<Elastic.CommonSchema.Generator.Projection.CommonSchemaTypesProjection>
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information


/*
IMPORTANT NOTE
==============
This file has been generated.
If you wish to submit a PR please modify the original csharp file and submit the PR with that change. Thanks!
*/

using System.Text.Json.Serialization;

namespace Elastic.CommonSchema;

@foreach (var property in Model.Base.EntityProperties)
{
<text>[JsonSerializable(typeof(@property.Entity.Name))]
</text>
}
@foreach (var entity in Model.EntityClasses)
{
<text>[JsonSerializable(typeof(@entity.Name))]
</text>
}
[JsonSourceGenerationOptions(DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
internal partial class EcsJsonContext : JsonSerializerContext { }
21 changes: 15 additions & 6 deletions src/Elastic.CommonSchema.Serilog/EcsTextFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,28 @@ namespace Elastic.CommonSchema.Serilog
/// <summary>
/// A serilog formatter that writes log events using the Elasticsearch Common Schema format
/// </summary>
public class EcsTextFormatter : ITextFormatter
public class EcsTextFormatter<TEcsDocument> : ITextFormatter
where TEcsDocument : EcsDocument, new()
{
private readonly EcsTextFormatterConfiguration _configuration;

public EcsTextFormatter() : this(new EcsTextFormatterConfiguration()) { }
protected EcsTextFormatterConfiguration<TEcsDocument> Configuration { get; }

public EcsTextFormatter(EcsTextFormatterConfiguration configuration) =>
_configuration = configuration ?? new EcsTextFormatterConfiguration();
public EcsTextFormatter() : this(new EcsTextFormatterConfiguration<TEcsDocument>()) { }

public EcsTextFormatter(EcsTextFormatterConfiguration<TEcsDocument> configuration) =>
Configuration = configuration ?? new EcsTextFormatterConfiguration<TEcsDocument>();

public virtual void Format(LogEvent logEvent, TextWriter output)
{
var ecsEvent = LogEventConverter.ConvertToEcs(logEvent, _configuration);
var ecsEvent = LogEventConverter.ConvertToEcs<TEcsDocument>(logEvent, Configuration);
output.WriteLine(ecsEvent.Serialize());
}
}

public class EcsTextFormatter : EcsTextFormatter<EcsDocument>
{
public EcsTextFormatter() : base() {}
public EcsTextFormatter(EcsTextFormatterConfiguration<EcsDocument> configuration) : base(configuration) {}
public EcsTextFormatter(EcsTextFormatterConfiguration configuration) : base(configuration) {}
}
}
45 changes: 15 additions & 30 deletions src/Elastic.CommonSchema.Serilog/EcsTextFormatterConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,29 @@ namespace Elastic.CommonSchema.Serilog
public interface IEcsTextFormatterConfiguration
{
bool MapCurrentThread { get; set; }
Func<EcsDocument, LogEvent, EcsDocument> MapCustom { get; set; }
bool MapExceptions { get; set; }
IHttpAdapter MapHttpAdapter { get; set; }
ISet<string> LogEventPropertiesToFilter { get;set; }
}

public class EcsTextFormatterConfiguration : IEcsTextFormatterConfiguration
public interface IEcsTextFormatterConfiguration<TEcsDocument> : IEcsTextFormatterConfiguration
where TEcsDocument : EcsDocument, new()
{
bool IEcsTextFormatterConfiguration.MapExceptions { get; set; } = true;
bool IEcsTextFormatterConfiguration.MapCurrentThread { get; set; } = true;

IHttpAdapter IEcsTextFormatterConfiguration.MapHttpAdapter { get; set; }
ISet<string> IEcsTextFormatterConfiguration.LogEventPropertiesToFilter { get; set; }

Func<EcsDocument, LogEvent, EcsDocument> IEcsTextFormatterConfiguration.MapCustom { get; set; } = (b, e) => b;

#if NETSTANDARD
public EcsTextFormatterConfiguration MapHttpContext(IHttpContextAccessor contextAccessor) => Assign(this, contextAccessor, (o, v) => o.MapHttpAdapter
= new HttpAdapter(v));
#else
public EcsTextFormatterConfiguration MapHttpContext(HttpContext httpContext) =>
Assign(this, httpContext, (o, v) => o.MapHttpAdapter = new HttpAdapter(v));
#endif
public EcsTextFormatterConfiguration MapExceptions(bool value) => Assign(this, value, (o, v) => o.MapExceptions = v);

public EcsTextFormatterConfiguration MapCurrentThread(bool value) => Assign(this, value, (o, v) => o.MapCurrentThread = v);
Func<TEcsDocument, LogEvent, TEcsDocument> MapCustom { get; set; }
}

public EcsTextFormatterConfiguration MapCustom(Func<EcsDocument, LogEvent, EcsDocument> value) => Assign(this, value, (o, v) => o.MapCustom = v);
public class EcsTextFormatterConfiguration<TEcsDocument> : IEcsTextFormatterConfiguration<TEcsDocument>
where TEcsDocument : EcsDocument, new()
{
public bool MapCurrentThread { get; set; } = true;
public bool MapExceptions { get; set; } = true;
public IHttpAdapter MapHttpAdapter { get; set; }
public ISet<string> LogEventPropertiesToFilter { get; set; }
public Func<TEcsDocument, LogEvent, TEcsDocument> MapCustom { get; set; }
}

public EcsTextFormatterConfiguration LogEventPropertiesToFilter(ISet<string> value) => Assign(this, value, (o, v) => o.LogEventPropertiesToFilter = v);
public class EcsTextFormatterConfiguration : EcsTextFormatterConfiguration<EcsDocument>
{

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static EcsTextFormatterConfiguration Assign<TValue>(
EcsTextFormatterConfiguration self, TValue value, Action<IEcsTextFormatterConfiguration, TValue> assign
)
{
assign(self, value);
return self;
}
}
}
8 changes: 6 additions & 2 deletions src/Elastic.CommonSchema.Serilog/LogEventConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ private static class SpecialKeys
public const string RequestId = nameof(RequestId);
}

public static EcsDocument ConvertToEcs(LogEvent logEvent, IEcsTextFormatterConfiguration configuration)
public static TEcsDocument ConvertToEcs<TEcsDocument>(LogEvent logEvent, IEcsTextFormatterConfiguration<TEcsDocument> configuration)
where TEcsDocument : EcsDocument, new()
{
var exceptions = logEvent.Exception != null
? new List<Exception> { logEvent.Exception }
Expand All @@ -58,7 +59,7 @@ public static EcsDocument ConvertToEcs(LogEvent logEvent, IEcsTextFormatterConfi
if (configuration.MapHttpAdapter != null)
exceptions.AddRange(configuration.MapHttpAdapter.Exceptions);

var ecsEvent = new EcsDocument
var ecsEvent = new TEcsDocument
{
Timestamp = logEvent.Timestamp,
Message = logEvent.RenderMessage(),
Expand Down Expand Up @@ -90,6 +91,9 @@ public static EcsDocument ConvertToEcs(LogEvent logEvent, IEcsTextFormatterConfi
return ecsEvent;
}

public static EcsDocument ConvertToEcs(LogEvent logEvent, IEcsTextFormatterConfiguration<EcsDocument> configuration) =>
ConvertToEcs<EcsDocument>(logEvent, configuration);

private static Service GetService(LogEvent logEvent)
{
if (!logEvent.TryGetScalarPropertyValue("ElasticApmServiceName", out var serviceName))
Expand Down
Loading

0 comments on commit 4863992

Please sign in to comment.