Skip to content

Commit

Permalink
Merge pull request #16 from jonas-merkle/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
jonas-merkle authored Apr 24, 2020
2 parents 2766210 + d31a0df commit a0e3697
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public static void Main(string[] args)
var slackUserIcon = string.IsNullOrEmpty(input) ? input : ":monkey_face:";
Console.WriteLine();

var levelSwitch = new LoggingLevelSwitch(initialMinimumLevel: LogEventLevel.Verbose);

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Slack(
Expand All @@ -51,7 +53,7 @@ public static void Main(string[] args)

sinkRestrictedToMinimumLevel: LogEventLevel.Verbose,
sinkOutputTemplate: "{Message}",
sinkLevelSwitch:new LoggingLevelSwitch(initialMinimumLevel: LogEventLevel.Verbose)
sinkLevelSwitch: levelSwitch
)
.CreateLogger();

Expand All @@ -71,6 +73,10 @@ public static void Main(string[] args)
Log.Write(LogEventLevel.Fatal, e, "Fatal Logging Message with exception");
}

levelSwitch.MinimumLevel = LogEventLevel.Fatal;

Log.Write(LogEventLevel.Error, "This Message shouldn't be send to Slack!");

System.Threading.Thread.Sleep(10000);

Console.WriteLine("Press any key to finish...");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using NUnit.Framework;

namespace Serilog.Sinks.SlackWebHook.Tests
{
[TestFixture]
public class SlackSinkOptionsTests
{
#region setup

[SetUp]
public void SetUp()
{

}

#endregion

#region init tests

[Test]
public void DefaultConstructorTest()
{
var options = new SlackSinkOptions();

Assert.IsTrue(options.SlackAttachmentColors.Count == 6);
Assert.IsTrue(options.SlackAttachmentFooterIcon.Count == 6);

Assert.IsTrue(options.PeriodicBatchingSinkOptionsBatchSizeLimit >= 0);
Assert.IsTrue(!options.PeriodicBatchingSinkOptionsPeriod.Equals(TimeSpan.Zero));
Assert.IsTrue(options.PeriodicBatchingSinkOptionsQueueLimit >= 0);
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
<NeutralLanguage></NeutralLanguage>
<SignAssembly>false</SignAssembly>
<PackageId>Serilog.Sinks.SlackWebHook</PackageId>
<AssemblyVersion>1.0.2.0</AssemblyVersion>
<FileVersion>1.0.2.0</FileVersion>
<Version>1.0.2</Version>
<AssemblyVersion>1.0.3.0</AssemblyVersion>
<FileVersion>1.0.3.0</FileVersion>
<Version>1.0.3</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,13 @@ private static LoggerConfiguration Slack(
if (periodicBatchingSinkOptionsPeriod != null) slackSinkOptions.PeriodicBatchingSinkOptionsPeriod = (TimeSpan)periodicBatchingSinkOptionsPeriod;
if (periodicBatchingSinkOptionsQueueLimit != null) slackSinkOptions.PeriodicBatchingSinkOptionsQueueLimit = (int)periodicBatchingSinkOptionsQueueLimit;

if (sinkRestrictedToMinimumLevel != null) slackSinkOptions.SinkRestrictedToMinimumLevel = (LogEventLevel)sinkRestrictedToMinimumLevel;
if (sinkOutputTemplate != null) slackSinkOptions.SinkOutputTemplate = sinkOutputTemplate;

return loggerSinkConfiguration.Sink(new SlackSink(slackSinkOptions, sinkFormatProvider, sinkLevelSwitch, statusSwitch, slackHttpClient,
generateSlackMessageText, generateSlackMessageAttachments, generateSlackMessageBlocks));
return loggerSinkConfiguration.Sink(
logEventSink: new SlackSink(slackSinkOptions, sinkFormatProvider, statusSwitch, slackHttpClient,
generateSlackMessageText, generateSlackMessageAttachments, generateSlackMessageBlocks),
restrictedToMinimumLevel: sinkRestrictedToMinimumLevel ?? LevelAlias.Minimum,
levelSwitch: sinkLevelSwitch);
}

#endregion
Expand Down
13 changes: 0 additions & 13 deletions Serilog.Sinks.SlackWebHook/Serilog.Sinks.SlackWebHook/SlackSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Serilog.Core;
using Serilog.Events;
using Serilog.Sinks.PeriodicBatching;
using Slack.Webhooks;
Expand Down Expand Up @@ -32,11 +31,6 @@ public class SlackSink : PeriodicBatchingSink
/// </summary>
private readonly SlackSinkOptions _slackSinkOptions;

/// <summary>
/// <see cref="LoggingLevelSwitch"/> to change the minimum LogEventLevel of this sink.
/// </summary>
private readonly LoggingLevelSwitch _sinkLevelSwitch;

/// <summary>
/// <see cref="SlackSinkActivationSwitch"/> to change the activation status of the sink on the fly.
/// </summary>
Expand Down Expand Up @@ -71,7 +65,6 @@ public class SlackSink : PeriodicBatchingSink
/// </summary>
/// <param name="slackSinkOptions">Slack Sink Options object.</param>
/// <param name="formatProvider">FormatProvider object.</param>
/// <param name="sinkLevelSwitch">LoggingLevelSwitch object.</param>
/// <param name="slackHttpClient">HttpClient instance.</param>
/// <param name="generateSlackMessageText">GenerateSlackMessageText function.</param>
/// <param name="generateSlackMessageAttachments">GenerateSlackMessageAttachments function.</param>
Expand All @@ -80,7 +73,6 @@ public class SlackSink : PeriodicBatchingSink
public SlackSink(
SlackSinkOptions slackSinkOptions,
IFormatProvider formatProvider,
LoggingLevelSwitch sinkLevelSwitch = null,
SlackSinkActivationSwitch statusSwitch = null,
HttpClient slackHttpClient = null,
Func<LogEvent, IFormatProvider, object, string> generateSlackMessageText = null,
Expand All @@ -94,7 +86,6 @@ public SlackSink(
{
_slackSinkOptions = slackSinkOptions;
_formatProvider = formatProvider;
_sinkLevelSwitch = sinkLevelSwitch ?? new LoggingLevelSwitch(LevelAlias.Minimum);
_statusSwitch = statusSwitch ?? new SlackSinkActivationSwitch();
_slackHttpClient = slackHttpClient ?? new HttpClient();

Expand Down Expand Up @@ -134,10 +125,6 @@ protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events)

foreach (var logEvent in events)
{
// check log level
if (logEvent.Level < _slackSinkOptions.SinkRestrictedToMinimumLevel) continue;
if (logEvent.Level < _sinkLevelSwitch.MinimumLevel) continue;

// create new slack message
var msg = new SlackMessage
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,6 @@ public class SlackSinkOptions

#region general sink options

/// <summary>
/// OPTIONAL: The absolute minimum <see cref="LogEventLevel"/> a log message must have to be send to Slack.
/// </summary>
public LogEventLevel SinkRestrictedToMinimumLevel { get; set; } = LevelAlias.Minimum;

/// <summary>
/// OPTIONAL: The template for the output format of the log messages.
/// </summary>
Expand Down

0 comments on commit a0e3697

Please sign in to comment.