Skip to content

Commit

Permalink
Support LoggingLevelSwitch and restrictedToMinimumLevel in Serilog Si…
Browse files Browse the repository at this point in the history
…nk (#328)
  • Loading branch information
Mpdreamz authored Aug 3, 2023
1 parent 97bbb34 commit 88f2bc8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 14 deletions.
14 changes: 14 additions & 0 deletions src/Elastic.Serilog.Sinks/ElasticsearchSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ public ElasticsearchSinkOptions() : this(new DefaultHttpTransport(TransportHelpe
/// </summary>
public string? IlmPolicy { get; set; }

/// <summary>
/// Provide an explicit minimum <see cref="LogEventLevel"/> for the Elasticsearch sink.
/// <para>This allows you to separately configure the sink to filter out messages.</para>
/// <para>E.g: Configure default logging at <see cref="LogEventLevel.Verbose"/> but only write <see cref="LogEventLevel.Error"/>
/// to Elasticsearch</para>
/// </summary>
public LogEventLevel? MinimumLevel { get; set; }

/// <summary>
/// A switch allowing the pass-through minimum level to be changed at runtime.
/// <para>Takes precedence over <see cref="MinimumLevel"/> if both are configured</para>
/// </summary>
public LoggingLevelSwitch? LevelSwitch { get; set; }

}

/// <summary>
Expand Down
51 changes: 37 additions & 14 deletions src/Elastic.Serilog.Sinks/ElasticsearchSinkExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Elastic.Transport;
using Serilog;
using Serilog.Configuration;
using Serilog.Core;
using Serilog.Events;

namespace Elastic.Serilog.Sinks
{
Expand All @@ -17,7 +19,11 @@ public static class ElasticsearchSinkExtensions
/// <para>Use <paramref name="loggerConfiguration"/> configure where and how data should be written</para>
/// </summary>
public static LoggerConfiguration Elasticsearch(this LoggerSinkConfiguration loggerConfiguration, ElasticsearchSinkOptions? options = null) =>
loggerConfiguration.Sink(new ElasticsearchSink(options ?? new ElasticsearchSinkOptions()));
loggerConfiguration.Sink(
new ElasticsearchSink(options ?? new ElasticsearchSinkOptions())
, restrictedToMinimumLevel: options?.MinimumLevel ?? LevelAlias.Minimum
, levelSwitch: options?.LevelSwitch
);

/// <summary>
/// Write logs directly to Elasticsearch.
Expand All @@ -26,7 +32,11 @@ public static LoggerConfiguration Elasticsearch(this LoggerSinkConfiguration log
/// </summary>
public static LoggerConfiguration Elasticsearch<TEcsDocument>(this LoggerSinkConfiguration loggerConfiguration, ElasticsearchSinkOptions<TEcsDocument>? options = null)
where TEcsDocument : EcsDocument, new() =>
loggerConfiguration.Sink(new ElasticsearchSink<TEcsDocument>(options ?? new ElasticsearchSinkOptions<TEcsDocument>()));
loggerConfiguration.Sink(
new ElasticsearchSink<TEcsDocument>(options ?? new ElasticsearchSinkOptions<TEcsDocument>())
, restrictedToMinimumLevel: options?.MinimumLevel ?? LevelAlias.Minimum
, levelSwitch: options?.LevelSwitch
);

/// <summary>
/// Write logs directly to Elasticsearch.
Expand All @@ -38,15 +48,18 @@ public static LoggerConfiguration Elasticsearch(
ICollection<Uri> nodes,
Action<ElasticsearchSinkOptions>? configureOptions = null,
Action<TransportConfiguration>? configureTransport = null,
bool useSniffing = true
bool useSniffing = true,
LoggingLevelSwitch? levelSwitch = null,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum
)
{
var transportConfig = useSniffing ? TransportHelper.Static(nodes) : TransportHelper.Sniffing(nodes);
configureTransport?.Invoke(transportConfig);

var sinkOptions = new ElasticsearchSinkOptions(new DefaultHttpTransport(transportConfig));
configureOptions?.Invoke(sinkOptions);

return loggerConfiguration.Sink(new ElasticsearchSink(sinkOptions));
return loggerConfiguration.Sink(new ElasticsearchSink(sinkOptions), restrictedToMinimumLevel, levelSwitch);
}

/// <summary>
Expand All @@ -60,15 +73,17 @@ public static LoggerConfiguration Elasticsearch<TEcsDocument>(
ICollection<Uri> nodes,
Action<ElasticsearchSinkOptions<TEcsDocument>>? configureOptions = null,
Action<TransportConfiguration>? configureTransport = null,
bool useSniffing = true
bool useSniffing = true,
LoggingLevelSwitch? levelSwitch = null,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum
) where TEcsDocument : EcsDocument, new()
{
var transportConfig = useSniffing ? TransportHelper.Static(nodes) : TransportHelper.Sniffing(nodes);
configureTransport?.Invoke(transportConfig);
var sinkOptions = new ElasticsearchSinkOptions<TEcsDocument>(new DefaultHttpTransport(transportConfig));
configureOptions?.Invoke(sinkOptions);

return loggerConfiguration.Sink(new ElasticsearchSink<TEcsDocument>(sinkOptions));
return loggerConfiguration.Sink(new ElasticsearchSink<TEcsDocument>(sinkOptions), restrictedToMinimumLevel, levelSwitch);
}

/// <summary>
Expand All @@ -82,15 +97,17 @@ public static LoggerConfiguration ElasticCloud(
string cloudId,
string apiKey,
Action<ElasticsearchSinkOptions>? configureOptions = null,
Action<TransportConfiguration>? configureTransport = null
Action<TransportConfiguration>? configureTransport = null,
LoggingLevelSwitch? levelSwitch = null,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum
)
{
var transportConfig = TransportHelper.Cloud(cloudId, apiKey);
configureTransport?.Invoke(transportConfig);
var sinkOptions = new ElasticsearchSinkOptions(new DefaultHttpTransport(transportConfig));
configureOptions?.Invoke(sinkOptions);

return loggerConfiguration.Sink(new ElasticsearchSink(sinkOptions));
return loggerConfiguration.Sink(new ElasticsearchSink(sinkOptions), restrictedToMinimumLevel, levelSwitch);
}

/// <summary>
Expand All @@ -105,15 +122,17 @@ public static LoggerConfiguration ElasticCloud<TEcsDocument>(
string cloudId,
string apiKey,
Action<ElasticsearchSinkOptions<TEcsDocument>>? configureOptions = null,
Action<TransportConfiguration>? configureTransport = null
Action<TransportConfiguration>? configureTransport = null,
LoggingLevelSwitch? levelSwitch = null,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum
) where TEcsDocument : EcsDocument, new()
{
var transportConfig = TransportHelper.Cloud(cloudId, apiKey);
configureTransport?.Invoke(transportConfig);
var sinkOptions = new ElasticsearchSinkOptions<TEcsDocument>(new DefaultHttpTransport(transportConfig));
configureOptions?.Invoke(sinkOptions);

return loggerConfiguration.Sink(new ElasticsearchSink<TEcsDocument>(sinkOptions));
return loggerConfiguration.Sink(new ElasticsearchSink<TEcsDocument>(sinkOptions), restrictedToMinimumLevel, levelSwitch);
}

/// <summary>
Expand All @@ -128,15 +147,17 @@ public static LoggerConfiguration ElasticCloud(
string username,
string password,
Action<ElasticsearchSinkOptions>? configureOptions = null,
Action<TransportConfiguration>? configureTransport = null
Action<TransportConfiguration>? configureTransport = null,
LoggingLevelSwitch? levelSwitch = null,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum
)
{
var transportConfig = TransportHelper.Cloud(cloudId, username, password);
configureTransport?.Invoke(transportConfig);
var sinkOptions = new ElasticsearchSinkOptions(new DefaultHttpTransport(transportConfig));
configureOptions?.Invoke(sinkOptions);

return loggerConfiguration.Sink(new ElasticsearchSink(sinkOptions));
return loggerConfiguration.Sink(new ElasticsearchSink(sinkOptions), restrictedToMinimumLevel, levelSwitch);
}

/// <summary>
Expand All @@ -152,15 +173,17 @@ public static LoggerConfiguration ElasticCloud<TEcsDocument>(
string username,
string password,
Action<ElasticsearchSinkOptions<TEcsDocument>>? configureOptions = null,
Action<TransportConfiguration>? configureTransport = null
Action<TransportConfiguration>? configureTransport = null,
LoggingLevelSwitch? levelSwitch = null,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum
) where TEcsDocument : EcsDocument, new()
{
var transportConfig = TransportHelper.Cloud(cloudId, username, password);
configureTransport?.Invoke(transportConfig);
var sinkOptions = new ElasticsearchSinkOptions<TEcsDocument>(new DefaultHttpTransport(transportConfig));
configureOptions?.Invoke(sinkOptions);

return loggerConfiguration.Sink(new ElasticsearchSink<TEcsDocument>(sinkOptions));
return loggerConfiguration.Sink(new ElasticsearchSink<TEcsDocument>(sinkOptions), restrictedToMinimumLevel, levelSwitch);
}
}
}

0 comments on commit 88f2bc8

Please sign in to comment.