Skip to content
This repository has been archived by the owner on Mar 1, 2022. It is now read-only.

Commit

Permalink
Enable programmatic configuration of Loggly c-sharp client
Browse files Browse the repository at this point in the history
  • Loading branch information
thviQit committed Nov 29, 2017
1 parent e66d19a commit 618d85f
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public static class LoggerConfigurationLogglyExtensions
/// The limit is soft in that it can be exceeded by any single error payload, but in that case only that single error
/// payload will be retained.</param>
/// <param name="retainedFileCountLimit">number of files to retain for the buffer. If defined, this also controls which records
/// <param name="logglyConfig">Used to configure underlying LogglyClient programmaticaly. Otherwise use app.Config.</param>
/// in the buffer get sent to the remote Loggly instance</param>
/// <returns>Logger configuration, allowing configuration to continue.</returns>
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
Expand All @@ -62,7 +63,8 @@ public static LoggerConfiguration Loggly(
long? eventBodyLimitBytes = 1024 * 1024,
LoggingLevelSwitch controlLevelSwitch = null,
long? retainedInvalidPayloadsLimitBytes = null,
int? retainedFileCountLimit = null)
int? retainedFileCountLimit = null,
LogglyConfiguration logglyConfig = null)
{
if (loggerConfiguration == null) throw new ArgumentNullException(nameof(loggerConfiguration));
if (bufferFileSizeLimitBytes.HasValue && bufferFileSizeLimitBytes < 0)
Expand All @@ -74,7 +76,7 @@ public static LoggerConfiguration Loggly(

if (bufferBaseFilename == null)
{
sink = new LogglySink(formatProvider, batchPostingLimit, defaultedPeriod);
sink = new LogglySink(formatProvider, batchPostingLimit, defaultedPeriod, logglyConfig);
}
else
{
Expand All @@ -87,7 +89,8 @@ public static LoggerConfiguration Loggly(
controlLevelSwitch,
retainedInvalidPayloadsLimitBytes,
retainedFileCountLimit,
formatProvider);
formatProvider,
logglyConfig);
}

return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
Expand Down
7 changes: 4 additions & 3 deletions src/Serilog.Sinks.Loggly/Sinks/Loggly/DurableLogglySink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public DurableLogglySink(
LoggingLevelSwitch levelControlSwitch,
long? retainedInvalidPayloadsLimitBytes,
int? retainedFileCountLimit = null,
IFormatProvider formatProvider = null
)
IFormatProvider formatProvider = null,
LogglyConfiguration logglyConfiguration = null)
{
if (bufferBaseFilename == null) throw new ArgumentNullException(nameof(bufferBaseFilename));

Expand All @@ -51,7 +51,8 @@ public DurableLogglySink(
levelControlSwitch,
retainedInvalidPayloadsLimitBytes,
encoding,
retainedFileCountLimit);
retainedFileCountLimit,
logglyConfiguration);

//writes events to the file to support connection recovery
_sink = new RollingFileSink(
Expand Down
12 changes: 10 additions & 2 deletions src/Serilog.Sinks.Loggly/Sinks/Loggly/HttpLogShipper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class HttpLogShipper : IDisposable
readonly IFileSystemAdapter _fileSystemAdapter = new FileSystemAdapter();
readonly FileBufferDataProvider _bufferDataProvider;
readonly InvalidPayloadLogger _invalidPayloadLogger;

readonly LogglyConfigAdapter _adapter;

public HttpLogShipper(
string bufferBaseFilename,
int batchPostingLimit,
Expand All @@ -52,14 +53,21 @@ public HttpLogShipper(
LoggingLevelSwitch levelControlSwitch,
long? retainedInvalidPayloadsLimitBytes,
Encoding encoding,
int? retainedFileCountLimit)
int? retainedFileCountLimit,
LogglyConfiguration logglyConfiguration)
{
_batchPostingLimit = batchPostingLimit;
_retainedFileCountLimit = retainedFileCountLimit;

_controlledSwitch = new ControlledLevelSwitch(levelControlSwitch);
_connectionSchedule = new ExponentialBackoffConnectionSchedule(period);

if (logglyConfiguration != null)
{
_adapter = new LogglyConfigAdapter();
_adapter.ConfigureLogglyClient(logglyConfiguration);
}

_logglyClient = new LogglyClient(); //we'll use the loggly client instead of HTTP directly

//create necessary path elements
Expand Down
41 changes: 41 additions & 0 deletions src/Serilog.Sinks.Loggly/Sinks/Loggly/LogglyConfigAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using Loggly.Config;

namespace Serilog.Sinks.Loggly
{
class LogglyConfigAdapter
{
public void ConfigureLogglyClient(LogglyConfiguration logglyConfiguration)
{
var config = LogglyConfig.Instance;

if (!string.IsNullOrWhiteSpace(logglyConfiguration.ApplicationName))
config.ApplicationName = logglyConfiguration.ApplicationName;

if (string.IsNullOrWhiteSpace(logglyConfiguration.CustomerToken))
throw new ArgumentNullException("CustomerToken", "CustomerToken is required");

config.CustomerToken = logglyConfiguration.CustomerToken;
config.IsEnabled = logglyConfiguration.IsEnabled;

foreach (var tag in logglyConfiguration.Tags)
{
config.TagConfig.Tags.Add(tag);
}

config.ThrowExceptions = logglyConfiguration.ThrowExceptions;

if (logglyConfiguration.LogTransport != TransportProtocol.Https)
config.Transport.LogTransport = (LogTransport)Enum.Parse(typeof(LogTransport), logglyConfiguration.LogTransport.ToString());

if (!string.IsNullOrWhiteSpace(logglyConfiguration.EndpointHostName))
config.Transport.EndpointHostname = logglyConfiguration.EndpointHostName;

if (logglyConfiguration.EndpointPort > 0 && logglyConfiguration.EndpointPort < ushort.MaxValue)
config.Transport.EndpointPort = logglyConfiguration.EndpointPort;

config.Transport.IsOmitTimestamp = logglyConfiguration.OmitTimestamp;
config.Transport = config.Transport.GetCoercedToValidConfig();
}
}
}
57 changes: 57 additions & 0 deletions src/Serilog.Sinks.Loggly/Sinks/Loggly/LogglyConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Collections.Generic;

namespace Serilog.Sinks.Loggly
{
public class LogglyConfiguration
{
public string ApplicationName { get; set; }
public string CustomerToken { get; set; }
public List<string> Tags { get; set; }
public bool IsEnabled { get; set; } = true;
public bool ThrowExceptions { get; set; }

/// <summary>
/// Defaults to Https
/// </summary>
public TransportProtocol LogTransport { get; set; }

/// <summary>
/// Defaults to logs-01.loggly.com
/// </summary>
public string EndpointHostName { get; set; }

/// <summary>
/// Defaults to default port for selected LogTransport.
/// E.g. https is 443, SyslogTcp/-Udp is 514 and SyslogSecure is 6514.
/// </summary>
public int EndpointPort { get; set; }

/// <summary>
/// Defines if timestamp should automatically be added to the json body when using Https
/// </summary>
public bool OmitTimestamp { get; set; }
}

public enum TransportProtocol
{
/// <summary>
/// Https.
/// </summary>
Https,

/// <summary>
/// SyslogSecure.
/// </summary>
SyslogSecure,

/// <summary>
/// SyslogUdp.
/// </summary>
SyslogUdp,

/// <summary>
/// SyslogTcp.
/// </summary>
SyslogTcp,
}
}
9 changes: 8 additions & 1 deletion src/Serilog.Sinks.Loggly/Sinks/Loggly/LogglySink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class LogglySink : PeriodicBatchingSink
{
readonly LogEventConverter _converter;
readonly LogglyClient _client;
readonly LogglyConfigAdapter _adapter;

/// <summary>
/// A reasonable default for the number of events posted in
Expand All @@ -47,9 +48,15 @@ public class LogglySink : PeriodicBatchingSink
/// <param name="batchSizeLimit">The maximum number of events to post in a single batch.</param>
/// <param name="period">The time to wait between checking for event batches.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
public LogglySink(IFormatProvider formatProvider, int batchSizeLimit, TimeSpan period)
/// <param name="logglyConfig">Used to configure underlying LogglyClient programmaticaly. Otherwise use app.Config.</param>
public LogglySink(IFormatProvider formatProvider, int batchSizeLimit, TimeSpan period, LogglyConfiguration logglyConfig)
: base (batchSizeLimit, period)
{
if (logglyConfig != null)
{
_adapter = new LogglyConfigAdapter();
_adapter.ConfigureLogglyClient(logglyConfig);
}
_client = new LogglyClient();
_converter = new LogEventConverter(formatProvider);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="4.19.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0-preview-20170923-02" />
<PackageReference Include="NSubstitute" Version="2.0.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="xunit" Version="2.3.0-beta5-build3769" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="NSubstitute" Version="3.1.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="xunit" Version="2.3.1" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
Expand Down

0 comments on commit 618d85f

Please sign in to comment.