Skip to content

fix/test fallback serilog (#523) #524

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

Open
wants to merge 2 commits into
base: 8.18
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -10,7 +10,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Elastic.Ingest.Elasticsearch" Version="0.11.3" />
<PackageReference Include="Elastic.Ingest.Elasticsearch" Version="0.12.1" />
</ItemGroup>

</Project>
13 changes: 8 additions & 5 deletions src/Elastic.Serilog.Sinks/ElasticsearchSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ public interface IElasticsearchSinkOptions
IEcsTextFormatterConfiguration EcsTextFormatterConfiguration { get; }

/// <inheritdoc cref="DataStreamName"/>
public DataStreamName DataStream { get; }
DataStreamName DataStream { get; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we intend to remove these from the public API?

Copy link
Contributor

@snakefoot snakefoot Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stevejgordon This is a property on the IElasticsearchSinkOptions-interface, and all interface-members are public by default. This is just removal of clutter.


/// <summary>
/// The ILM Policy to apply, see the following for more details:
/// <para>https://www.elastic.co/guide/en/elasticsearch/reference/current/index-lifecycle-management.html</para>
/// Defaults to `logs` which is shipped by default with Elasticsearch
/// </summary>
public string? IlmPolicy { get; }
string? IlmPolicy { get; }

}

Expand Down Expand Up @@ -136,8 +136,11 @@ public ElasticsearchSink(ElasticsearchSinkOptions<TEcsDocument> options)
var channelOptions = new DataStreamChannelOptions<TEcsDocument>(options.Transport)
{
DataStream = options.DataStream,
ExportMaxRetriesCallback = EmitExportFailures
ExportMaxRetriesCallback = EmitExportFailures,
ExportExceptionCallback = _ =>
{

}
};
options.ConfigureChannel?.Invoke(channelOptions);
_channel = new EcsDataStreamChannel<TEcsDocument>(channelOptions, new [] { new SelfLogCallbackListener<TEcsDocument>(options)});
Expand All @@ -153,7 +156,7 @@ private void EmitExportFailures(IReadOnlyCollection<TEcsDocument> documents)
.ToArray();
_failureListener.OnLoggingFailed(
this,
LoggingFailureKind.Temporary,
LoggingFailureKind.Permanent,
"Failure to export events over to Elasticsearch.",
logs,
exception: null
Expand All @@ -169,7 +172,7 @@ public void Emit(LogEvent logEvent)
{
_failureListener.OnLoggingFailed(
this,
LoggingFailureKind.Temporary,
LoggingFailureKind.Permanent,
"Failure to push event over the channel.",
[logEvent],
exception: null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<ItemGroup>
<PackageReference Include="Elastic.Clients.Elasticsearch" Version="8.16.2" />
<PackageReference Include="Elastic.Elasticsearch.Xunit" Version="0.6.0" />
<PackageReference Include="Elastic.Ingest.Elasticsearch" Version="0.11.3" />
<PackageReference Include="Elastic.Ingest.Elasticsearch" Version="0.12.1" />

</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Serilog.Sinks.ColoredConsole" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.TestCorrelator" Version="4.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1"/>
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.2"/>
Expand Down
54 changes: 54 additions & 0 deletions tests/Elastic.Serilog.Sinks.Tests/SerilogFailureOutputTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Elastic.Channels;
using Elastic.Channels.Diagnostics;
using Elastic.Transport;
using FluentAssertions;
using Serilog;
using Serilog.Sinks.TestCorrelator;
using Xunit;
using DataStreamName = Elastic.Ingest.Elasticsearch.DataStreams.DataStreamName;

namespace Elastic.Serilog.Sinks.Tests
{
public class SerilogFailureOutputTests
{
private readonly CountdownEvent _waitHandle;
private IChannelDiagnosticsListener? _listener;
private ElasticsearchSinkOptions SinkOptions { get; }

public SerilogFailureOutputTests()
{
_waitHandle = new CountdownEvent(1);
SinkOptions = new ElasticsearchSinkOptions(new DistributedTransport(new TransportConfiguration()))
{
DataStream = new DataStreamName("logs", "serilog", "tests"),
ConfigureChannel = c =>
{
c.BufferOptions = new BufferOptions
{
ExportMaxRetries = 0,
WaitHandle = _waitHandle,
OutboundBufferMaxSize = 1
};
},
ChannelDiagnosticsCallback = (l) => _listener = l
};
}

[Fact] public void AssertLogs()
{
var loggerConfig = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.FallbackChain(
fc => fc.Elasticsearch(SinkOptions),
fc => fc.Console()
);

using var logger = loggerConfig.CreateLogger();
logger.Information("Hello world");

if (!_waitHandle.WaitHandle.WaitOne(TimeSpan.FromSeconds(10)))
throw new Exception($"No flush occurred in 10 seconds: {_listener}", _listener?.ObservedException);

}
}
}
Loading