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

Commit

Permalink
Add some extra parameters to allow configuration from appsettings.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Darryl Shpak authored and MiguelAlho committed Jul 18, 2018
1 parent a727318 commit 200650d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var log = new LoggerConfiguration()

Properties will be sent along to Loggly. The level is sent as a category.

To use a durable logger (that will save messages localy if the connectio nto the server is unavailable, and resend once the connection has recovered), set the `bufferBaseFilename` argument in the `Loggly()` extension method.
To use a durable logger (that will save messages locally if the connection to the server is unavailable, and resend once the connection has recovered), set the `bufferBaseFilename` argument in the `Loggly()` extension method.

```csharp
var log = new LoggerConfiguration()
Expand All @@ -27,3 +27,24 @@ var log = new LoggerConfiguration()
This will write unsent messages to a `buffer-{Date}.json` file in the specified folder (`C:\test\` in the example).

The method also takes a `retainedFileCountLimit` parameter that will allow you to control how much info to store / ship when back online. By default, the value is `null` with the intent is to send all persisted data, no matter how old. If you specify a value, only the data in the last N buffer files will be shipped back, preventing stale data to be indexed (if that info is no longer usefull).

The sink can also be configured from `appsettings.json` for .NET Standard / .NET Core applications that do not support XML configuration:

```json
{
"Serilog": {
"WriteTo": [
{
"Name": "Loggly",
"Args": {
"customerToken": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"tags": "foo,bar"
}
}
],
"Properties": { "Application": "SampleApp" }
}
}
```

The `customerToken` argument is required, if you use this form of configuration. The `tags` argument is comma-delimited. The `Application` property will also be sent to Loggly and should be set appropriately.
27 changes: 24 additions & 3 deletions src/Serilog.Sinks.Loggly/LoggerConfigurationLogglyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// limitations under the License.

using System;
using System.Collections.Generic;
using System.Linq;
using Serilog.Configuration;
using Serilog.Core;
using Serilog.Events;
Expand Down Expand Up @@ -48,9 +50,12 @@ 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
/// in the buffer get sent to the remote Loggly instance</param>
/// <param name="customerToken">Token used to identify the Loggly account to use</param>
/// <param name="tags">Comma-delimited list of tags to submit to Loggly</param>
/// <param name="endpointHostName">Hostname to send logs to. Defaults to logs-01.loggly.com.</param>
/// <param name="logglyConfig">Used to configure underlying LogglyClient programmaticaly. Otherwise use app.Config.</param>
/// <param name="includes">Decides if the sink should include specific properties in the log message</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>
public static LoggerConfiguration Loggly(
Expand All @@ -65,6 +70,9 @@ public static LoggerConfiguration Loggly(
LoggingLevelSwitch controlLevelSwitch = null,
long? retainedInvalidPayloadsLimitBytes = null,
int? retainedFileCountLimit = null,
string customerToken = null,
string tags = null,
string endpointHostName = null,
LogglyConfiguration logglyConfig = null,
LogIncludes includes = null)
{
Expand All @@ -76,9 +84,22 @@ public static LoggerConfiguration Loggly(

ILogEventSink sink;

LogglyConfiguration constructedLogglyConfig = null;
if (customerToken != null)
{
constructedLogglyConfig = new LogglyConfiguration
{
CustomerToken = customerToken,
Tags = tags?.Split(',').ToList() ?? new List<string>(),
EndpointHostName = endpointHostName
};
}

var activeLogglyConfig = logglyConfig ?? constructedLogglyConfig;

if (bufferBaseFilename == null)
{
sink = new LogglySink(formatProvider, batchPostingLimit, defaultedPeriod, logglyConfig, includes);
sink = new LogglySink(formatProvider, batchPostingLimit, defaultedPeriod, activeLogglyConfig, includes);
}
else
{
Expand All @@ -92,7 +113,7 @@ public static LoggerConfiguration Loggly(
retainedInvalidPayloadsLimitBytes,
retainedFileCountLimit,
formatProvider,
logglyConfig,
activeLogglyConfig,
includes);
}

Expand Down

0 comments on commit 200650d

Please sign in to comment.