Skip to content

Commit

Permalink
Merge pull request #80 from serilog/dev
Browse files Browse the repository at this point in the history
Release 3.4.0
  • Loading branch information
nblumhardt authored Sep 18, 2017
2 parents c36567b + f0fdb9a commit 4550ffd
Show file tree
Hide file tree
Showing 18 changed files with 663 additions and 453 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,4 @@ UpgradeLog*.htm
# Microsoft Fakes
FakesAssemblies/
*.orig
/sample/Sample/logs/
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Log.CloseAndFlush();

The sink can take advantage of Seq's [API keys](http://docs.getseq.net/docs/api-keys) to authenticate clients and dynamically attach properties to events at the server-side. To use an API key, specify it in the `apiKey` parameter of `WriteTo.Seq()`.

### Configuring with XML
### XML `<appSettings>` configuration

To adjust the Seq server URL at deployment time, it's often convenient to configure it using XML `<appSettings>`, in the `App.config` or `Web.config` file.

Expand All @@ -64,6 +64,41 @@ The settings typically included are:

Serilog's XML configuration has several other capabilities that are described on the [Serilog wiki](https://github.com/serilog/serilog/wiki/AppSettings).

### JSON `appsettings.json` configuration

To use the Seq sink with _Microsoft.Extensions.Configuration_, for example with ASP.NET Core or .NET Core, use the [Serilog.Settings.Configuration](https://github.com/serilog/serilog-settings-configuration) package. First install that package if you have not already done so:

```powershell
Install-Package Serilog.Settings.Configuration
```

Instead of configuring the Seq sink directly in code, call `ReadFrom.Configuration()`:

```csharp
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();

var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
```

In your `appsettings.json` file, under the `Serilog` node, :

```json
{
"Serilog": {
"WriteTo": [
{ "Name": "Seq", "Args": { "serverUrl": "http://localhost:5341" } }
]
}
}
```

See the XML `<appSettings>` example above for a discussion of available `Args` options.


### Dynamic log level control

The Seq sink can dynamically adjust the logging level up or down based on the level associated with an API key in Seq. To use this feature, create a `LoggingLevelSwitch` to control the `MinimumLevel`, and pass this in the `controlLevelSwitch` parameter of `WriteTo.Seq()`:
Expand Down
3 changes: 1 addition & 2 deletions sample/Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ public static void Main(string[] args)

Log.Logger = new LoggerConfiguration()
.MinimumLevel.ControlledBy(levelSwitch)
.WriteTo.LiterateConsole()
.WriteTo.Console()
.WriteTo.Seq("http://localhost:5341",
apiKey: "o6nYf3WWnzF43Uu5PZWJ",
controlLevelSwitch: levelSwitch)
.CreateLogger();

Expand Down
15 changes: 10 additions & 5 deletions sample/Sample/Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
<PropertyGroup>
<Description>Sample Console Application</Description>
<Authors>nblumhardt</Authors>
<TargetFrameworks>netcoreapp1.0</TargetFrameworks>
<TargetFrameworks>netcoreapp2.0;net47</TargetFrameworks>
<AssemblyName>Sample</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>Sample</PackageId>
<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">$(PackageTargetFallback);dnxcore50;portable-net45+win8</PackageTargetFallback>
<RuntimeFrameworkVersion Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">1.0.4</RuntimeFrameworkVersion>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
Expand All @@ -19,8 +17,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.3.0" />
<PackageReference Include="Serilog.Sinks.Literate" Version="2.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">
<PackageReference Include="System.Net.Http" Version="4.3.2" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net47' ">
<Reference Include="System.Net.Http" />
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions src/Serilog.Sinks.Seq/SeqLoggerConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
using Serilog.Events;
using Serilog.Sinks.Seq;
using System.Net.Http;
using Serilog.Sinks.Seq.Audit;

#if DURABLE
using Serilog.Sinks.Seq.Durable;
#endif

namespace Serilog
{
Expand Down
11 changes: 6 additions & 5 deletions src/Serilog.Sinks.Seq/Serilog.Sinks.Seq.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

<PropertyGroup>
<Description>Serilog sink that writes to the Seq log server over HTTP/HTTPS.</Description>
<VersionPrefix>3.3.3</VersionPrefix>
<VersionPrefix>3.4.0</VersionPrefix>
<Authors>Serilog Contributors</Authors>
<Copyright>Copyright © Serilog Contributors 2013-2017</Copyright>
<TargetFrameworks>netstandard1.1;netstandard1.3;net45;net46</TargetFrameworks>
<TargetFrameworks>netstandard1.1;netstandard1.3;net45</TargetFrameworks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>Serilog.Sinks.Seq</AssemblyName>
<RootNamespace>Serilog</RootNamespace>
<AssemblyOriginatorKeyFile>../../assets/Serilog.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
Expand All @@ -24,7 +25,7 @@
<DefineConstants>$(DefineConstants);DURABLE;THREADING_TIMER</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net45' or '$(TargetFramework)' == 'net46' ">
<PropertyGroup Condition=" '$(TargetFramework)' == 'net45' ">
<DefineConstants>$(DefineConstants);DURABLE;THREADING_TIMER;HRESULTS</DefineConstants>
</PropertyGroup>

Expand All @@ -33,13 +34,13 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.3.0" />
<PackageReference Include="Serilog" Version="2.5.0" />
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="2.1.1" />
<PackageReference Include="Serilog.Formatting.Compact" Version="1.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' != 'net45' ">
<PackageReference Include="System.Net.Http" Version="4.3.1" />
<PackageReference Include="System.Net.Http" Version="4.3.2" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' != 'netstandard1.1' ">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
using Serilog.Formatting.Compact;
using Serilog.Formatting.Json;

namespace Serilog.Sinks.Seq
namespace Serilog.Sinks.Seq.Audit
{
sealed class SeqAuditSink : ILogEventSink, IDisposable
{
Expand Down
1 change: 0 additions & 1 deletion src/Serilog.Sinks.Seq/Sinks/Seq/ControlledLevelSwitch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using Serilog.Core;
using Serilog.Events;

Expand Down
72 changes: 72 additions & 0 deletions src/Serilog.Sinks.Seq/Sinks/Seq/Durable/BookmarkFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Serilog.Sinks.Seq Copyright 2017 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#if DURABLE

using System;
using System.IO;
using System.Text;

namespace Serilog.Sinks.Seq.Durable
{
sealed class BookmarkFile : IDisposable
{
readonly FileStream _bookmark;

public BookmarkFile(string bookmarkFilename)
{
_bookmark = System.IO.File.Open(bookmarkFilename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
}

public FileSetPosition TryReadBookmark()
{
if (_bookmark.Length != 0)
{
// Important not to dispose this StreamReader as the stream must remain open.
var reader = new StreamReader(_bookmark, Encoding.UTF8, false, 128);
var current = reader.ReadLine();

if (current != null)
{
_bookmark.Position = 0;
var parts = current.Split(new[] { ":::" }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 2)
{
return new FileSetPosition(long.Parse(parts[0]), parts[1]);
}
}
}

return FileSetPosition.None;
}

public void WriteBookmark(FileSetPosition bookmark)
{
if (bookmark.File == null)
return;

using (var writer = new StreamWriter(_bookmark))
{
writer.WriteLine("{0}:::{1}", bookmark.NextLineStart, bookmark.File);
}
}

public void Dispose()
{
_bookmark.Dispose();
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
using System.Net.Http;
using System.Text;

namespace Serilog.Sinks.Seq
namespace Serilog.Sinks.Seq.Durable
{
class DurableSeqSink : ILogEventSink, IDisposable
{
Expand Down
112 changes: 112 additions & 0 deletions src/Serilog.Sinks.Seq/Sinks/Seq/Durable/FileSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Serilog.Sinks.Seq Copyright 2017 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#if DURABLE

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using Serilog.Debugging;

namespace Serilog.Sinks.Seq.Durable
{
class FileSet
{
readonly string _bookmarkFilename;
readonly string _candidateSearchPath;
readonly string _logFolder;

const string InvalidPayloadFilePrefix = "invalid-";

public FileSet(string bufferBaseFilename)
{
if (bufferBaseFilename == null) throw new ArgumentNullException(nameof(bufferBaseFilename));

_bookmarkFilename = Path.GetFullPath(bufferBaseFilename + ".bookmark");
_logFolder = Path.GetDirectoryName(_bookmarkFilename);
_candidateSearchPath = Path.GetFileName(bufferBaseFilename) + "*.json";
}

public BookmarkFile OpenBookmarkFile()
{
return new BookmarkFile(_bookmarkFilename);
}

public string MakeInvalidPayloadFilename(HttpStatusCode statusCode)
{
var invalidPayloadFilename = $"{InvalidPayloadFilePrefix}{statusCode}-{Guid.NewGuid():n}.json";
return Path.Combine(_logFolder, invalidPayloadFilename);
}

public void CleanUpInvalidPayloadFiles(long maxNumberOfBytesToRetain)
{
try
{
var candiateFiles = Directory.EnumerateFiles(_logFolder, $"{InvalidPayloadFilePrefix}*.json");
DeleteOldFiles(maxNumberOfBytesToRetain, candiateFiles);
}
catch (Exception ex)
{
SelfLog.WriteLine("Exception thrown while trying to clean up invalid payload files: {0}", ex);
}
}

public string[] GetFiles()
{
return Directory.GetFiles(_logFolder, _candidateSearchPath)
.OrderBy(n => n)
.ToArray();
}

static void DeleteOldFiles(long maxNumberOfBytesToRetain, IEnumerable<string> files)
{
var orderedFileInfos = from candiateFile in files
let candiateFileInfo = new FileInfo(candiateFile)
orderby candiateFileInfo.LastAccessTimeUtc descending
select candiateFileInfo;

var invalidPayloadFilesToDelete = WhereCumulativeSizeGreaterThan(orderedFileInfos, maxNumberOfBytesToRetain);

foreach (var fileToDelete in invalidPayloadFilesToDelete)
{
try
{
fileToDelete.Delete();
}
catch (Exception ex)
{
SelfLog.WriteLine("Exception '{0}' thrown while trying to delete file {1}", ex.Message, fileToDelete.FullName);
}
}
}

static IEnumerable<FileInfo> WhereCumulativeSizeGreaterThan(IEnumerable<FileInfo> files, long maxCumulativeSize)
{
long cumulative = 0;
foreach (var file in files)
{
cumulative += file.Length;
if (cumulative > maxCumulativeSize)
{
yield return file;
}
}
}

}
}

#endif
Loading

0 comments on commit 4550ffd

Please sign in to comment.