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

Remove WriteEvent usages #453

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
"Microsoft.Hosting.Lifetime": "Information",
"Elastic.Apm": "Error"
}
}
}
3 changes: 2 additions & 1 deletion examples/aspnetcore-with-serilog/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
"Microsoft.Hosting.Lifetime": "Information",
"Elastic.Apm": "Error"
}
},
"AllowedHosts": "*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ private IBufferedChannel<LogEvent> CreatIngestChannel(ElasticsearchLoggerOptions
{
IndexFormat = loggerOptions.Index.Format,
IndexOffset = loggerOptions.Index.IndexOffset,
WriteEvent = async (stream, ctx, logEvent) => await logEvent.SerializeAsync(stream, ctx).ConfigureAwait(false),
TimestampLookup = l => l.Timestamp,
};
SetupChannelOptions(_channelConfigurations, indexChannelOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ public EcsDataStreamChannel(DataStreamChannelOptions<TEcsDocument> options) : th
public EcsDataStreamChannel(
DataStreamChannelOptions<TEcsDocument> options,
ICollection<IChannelCallbacks<TEcsDocument, BulkResponse>>? callbackListeners
) : base(options, callbackListeners) =>
options.WriteEvent = async (stream, ctx, @event) =>
await JsonSerializer.SerializeAsync(stream, @event, typeof(TEcsDocument), EcsJsonConfiguration.SerializerOptions, ctx)
.ConfigureAwait(false);
) : base(options, callbackListeners) { }

/// <summary>
/// Bootstrap the target data stream. Will register the appropriate index and component templates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ public class EcsIndexChannel<TEcsDocument> : IndexChannel<TEcsDocument>
where TEcsDocument : EcsDocument
{
/// <inheritdoc cref="EcsIndexChannel{TEcsDocument}"/>
public EcsIndexChannel(IndexChannelOptions<TEcsDocument> options) : base(options) =>
options.WriteEvent = async (stream, ctx, @event) =>
await JsonSerializer.SerializeAsync(stream, @event, typeof(TEcsDocument), EcsJsonConfiguration.SerializerOptions, ctx)
.ConfigureAwait(false);
public EcsIndexChannel(IndexChannelOptions<TEcsDocument> options) : base(options) { }

/// <summary>
/// Bootstrap the target index. Will register the appropriate index and component templates
Expand Down
10 changes: 4 additions & 6 deletions src/Elastic.NLog.Targets/ElasticsearchTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class ElasticsearchTarget : TargetWithLayout

/// <summary>
/// Gets or sets the format string for the Elastic search index. The current <c>DateTimeOffset</c> is passed as parameter 0.
///
///
/// <para> Example: "dotnet-{0:yyyy.MM.dd}"</para>
/// <para> If no {0} parameter is defined the index name is effectively fixed</para>
/// </summary>
Expand Down Expand Up @@ -213,8 +213,7 @@ private EcsDataStreamChannel<NLogEcsDocument> CreateDataStreamChannel(Distribute
var dataStreamNamespace = DataStreamNamespace?.Render(LogEventInfo.CreateNullEvent()) ?? string.Empty;
var channelOptions = new DataStreamChannelOptions<NLogEcsDocument>(transport)
{
DataStream = new DataStreamName(dataStreamType, dataStreamSet, dataStreamNamespace),
WriteEvent = async (stream, ctx, logEvent) => await logEvent.SerializeAsync(stream, ctx).ConfigureAwait(false),
DataStream = new DataStreamName(dataStreamType, dataStreamSet, dataStreamNamespace)
};
SetupChannelOptions(channelOptions);
var channel = new EcsDataStreamChannel<NLogEcsDocument>(channelOptions, new[] { new InternalLoggerCallbackListener<NLogEcsDocument>() });
Expand All @@ -228,9 +227,8 @@ private EcsIndexChannel<NLogEcsDocument> CreateIndexChannel(DistributedTransport
{
IndexFormat = indexFormat,
IndexOffset = indexOffset,
WriteEvent = async (stream, ctx, logEvent) => await logEvent.SerializeAsync(stream, ctx).ConfigureAwait(false),
TimestampLookup = l => l.Timestamp,
OperationMode = indexOperation,
OperationMode = indexOperation
};

if (_hasIndexEventId)
Expand All @@ -251,7 +249,7 @@ protected override void CloseTarget()

/// <inheritdoc />
protected override void Write(LogEventInfo logEvent)
{
{
var ecsDoc = _layout.RenderEcsDocument(logEvent);
_channel?.TryWrite(ecsDoc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

<ItemGroup>
<None Remove="Lib\UAParser.regexes.yaml" />
<EmbeddedResource Include="Lib\UAParser.regexes.yaml" />
<EmbeddedResource Include="Lib\UAParser.regexes.yaml" LogicalName="UAParser.regexes.yaml" />
</ItemGroup>

</Project>
21 changes: 11 additions & 10 deletions src/Elastic.Serilog.Enrichers.Web/HttpContextEnricher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@ public HttpContextEnricher(IHttpContextAccessor httpContextAccessor) =>
/// <summary> Enrich the log event.</summary>
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
var r = new HttpContextEnrichments();
if (Adapter.HasContext)
{
r.Http = Adapter.Http;
r.Server = Adapter.Server;
r.Url = Adapter.Url;
r.UserAgent = Adapter.UserAgent;
r.Client = Adapter.Client;
r.User = Adapter.User;
}
if (!Adapter.HasContext)
return;

var r = new HttpContextEnrichments {
Http = Adapter.Http,
Server = Adapter.Server,
Url = Adapter.Url,
UserAgent = Adapter.UserAgent,
Client = Adapter.Client,
User = Adapter.User
};

logEvent.AddPropertyIfAbsent(new LogEventProperty(PropertyName, new ScalarValue(r)));
}
Expand Down
Loading