Skip to content

Support adding default tags as discrete values in LoggerConfiguration… #12

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

Merged
merged 2 commits into from
Apr 19, 2021
Merged
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 @@ -32,13 +32,14 @@ public static LoggerConfiguration Exceptionless(
if (apiKey == null)
throw new ArgumentNullException(nameof(apiKey));

return loggerConfiguration.Sink(new ExceptionlessSink(apiKey, null, additionalOperation, includeProperties), restrictedToMinimumLevel);
return loggerConfiguration.Sink(new ExceptionlessSink(apiKey, null, null, additionalOperation, includeProperties), restrictedToMinimumLevel);
}

/// <summary>Creates a new Exceptionless sink with the specified <paramref name="apiKey"/>.</summary>
/// <param name="loggerConfiguration">The logger configuration.</param>
/// <param name="apiKey">The API key that will be used when sending events to the server.</param>
/// <param name="serverUrl">Optional URL of the server events will be sent to.</param>
/// <param name="defaultTags">Default tags to be added to every log event.</param>
/// <param name="additionalOperation">Any additional operation to run against the build exceptions</param>
/// <param name="includeProperties">If false it suppressed sending the Serilog properties to Exceptionless</param>
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
Expand All @@ -48,6 +49,7 @@ public static LoggerConfiguration Exceptionless(
this LoggerSinkConfiguration loggerConfiguration,
string apiKey,
string serverUrl = null,
string[] defaultTags = null,
Func<EventBuilder, EventBuilder> additionalOperation = null,
bool includeProperties = true,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum
Expand All @@ -58,7 +60,7 @@ public static LoggerConfiguration Exceptionless(
if (apiKey == null)
throw new ArgumentNullException(nameof(apiKey));

return loggerConfiguration.Sink(new ExceptionlessSink(apiKey, serverUrl, additionalOperation, includeProperties), restrictedToMinimumLevel);
return loggerConfiguration.Sink(new ExceptionlessSink(apiKey, serverUrl, defaultTags, additionalOperation, includeProperties), restrictedToMinimumLevel);
}

/// <summary>Creates a new Exceptionless sink.</summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Exceptionless;
using Exceptionless.Dependency;
using Exceptionless.Logging;
Expand All @@ -13,6 +14,7 @@ namespace Serilog.Sinks.Exceptionless {
/// Exceptionless Sink
/// </summary>
public class ExceptionlessSink : ILogEventSink, IDisposable {
private readonly string[] _defaultTags;
private readonly Func<EventBuilder, EventBuilder> _additionalOperation;
private readonly bool _includeProperties;

Expand All @@ -27,6 +29,9 @@ public class ExceptionlessSink : ILogEventSink, IDisposable {
/// <param name="serverUrl">
/// Optional URL of the server events will be sent to.
/// </param>
/// <param name="defaultTags">
/// Default tags to be added to every log event.
/// </param>
/// <param name="additionalOperation">
/// Optional operation to run against the Error Builder before submitting to Exceptionless
/// </param>
Expand All @@ -36,6 +41,7 @@ public class ExceptionlessSink : ILogEventSink, IDisposable {
public ExceptionlessSink(
string apiKey,
string serverUrl = null,
string[] defaultTags = null,
Func<EventBuilder, EventBuilder> additionalOperation = null,
bool includeProperties = true
) {
Expand All @@ -53,6 +59,7 @@ public ExceptionlessSink(
config.UseLogger(new SelfLogLogger());
});

_defaultTags = defaultTags;
_additionalOperation = additionalOperation;
_includeProperties = includeProperties;
}
Expand Down Expand Up @@ -89,6 +96,10 @@ public void Emit(LogEvent logEvent) {

var builder = _client.CreateFromLogEvent(logEvent);

if (_defaultTags != null && _defaultTags.Any()) {
builder.AddTags(_defaultTags);
}

if (_includeProperties && logEvent.Properties != null) {
foreach (var prop in logEvent.Properties)
{
Expand Down