Skip to content
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

General code refresh #132

Merged
merged 6 commits into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,7 @@ UpgradeLog*.htm

# Microsoft Fakes
FakesAssemblies/

*.orig
/sample/Sample/logs/
.idea
1 change: 0 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
version: '{build}'
skip_tags: true
image: Visual Studio 2019
configuration: Release
install:
- ps: mkdir -Force ".\build\" | Out-Null
build_script:
Expand Down
2 changes: 1 addition & 1 deletion sample/Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Sample
{
public class Program
{
public static void Main(string[] args)
public static void Main()
{
// By sharing between the Seq sink and logger itself,
// Seq API keys can be used to control the level of the whole logging pipeline.
Expand Down
23 changes: 0 additions & 23 deletions sample/Sample/Properties/AssemblyInfo.cs

This file was deleted.

8 changes: 2 additions & 6 deletions sample/Sample/Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Description>Sample Console Application</Description>
<Authors>nblumhardt</Authors>
<TargetFrameworks>netcoreapp2.0;net47</TargetFrameworks>
<TargetFrameworks>netcoreapp3.1;net47</TargetFrameworks>
<AssemblyName>Sample</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>Sample</PackageId>
Expand All @@ -20,14 +20,10 @@
<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>
<ProjectReference Include="..\..\src\Serilog.Sinks.Seq\Serilog.Sinks.Seq.csproj" />
</ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion serilog-sinks-seq.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{037440DE-440
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{E9D1B5E1-DEB9-4A04-8BAB-24EC7240ADAF}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
appveyor.yml = appveyor.yml
Build.ps1 = Build.ps1
README.md = README.md
Expand Down
7 changes: 7 additions & 0 deletions serilog-sinks-seq.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Backoff/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=delim/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=fdelim/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=filenames/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Formattable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=ptoken/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=rdelim/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Seq_0027s/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Serilog/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
28 changes: 19 additions & 9 deletions src/Serilog.Sinks.Seq/SeqLoggerConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Serilog.Events;
using Serilog.Sinks.Seq;
using System.Net.Http;
using Serilog.Sinks.PeriodicBatching;
using Serilog.Sinks.Seq.Audit;

#if DURABLE
Expand Down Expand Up @@ -89,21 +90,28 @@ public static LoggerConfiguration Seq(
throw new ArgumentOutOfRangeException(nameof(queueSizeLimit), "Queue size limit must be non-zero.");

var defaultedPeriod = period ?? SeqSink.DefaultPeriod;
var controlledSwitch = new ControlledLevelSwitch(controlLevelSwitch);

ILogEventSink sink;

if (bufferBaseFilename == null)
{
sink = new SeqSink(
var batchedSink = new SeqSink(
serverUrl,
apiKey,
batchPostingLimit,
defaultedPeriod,
eventBodyLimitBytes,
controlLevelSwitch,
controlledSwitch,
messageHandler,
compact,
queueSizeLimit);
compact);

var options = new PeriodicBatchingSinkOptions
{
BatchSizeLimit = batchPostingLimit,
Period = defaultedPeriod,
QueueLimit = queueSizeLimit
};

sink = new PeriodicBatchingSink(batchedSink, options);
}
else
{
Expand All @@ -116,7 +124,7 @@ public static LoggerConfiguration Seq(
defaultedPeriod,
bufferSizeLimitBytes,
eventBodyLimitBytes,
controlLevelSwitch,
controlledSwitch,
messageHandler,
retainedInvalidPayloadsLimitBytes);
#else
Expand All @@ -125,7 +133,9 @@ public static LoggerConfiguration Seq(
#endif
}

return loggerSinkConfiguration.Sink(sink, restrictedToMinimumLevel);
return loggerSinkConfiguration.Conditional(
controlledSwitch.IsIncluded,
wt => wt.Sink(sink, restrictedToMinimumLevel));
}

/// <summary>
Expand Down
20 changes: 11 additions & 9 deletions src/Serilog.Sinks.Seq/Serilog.Sinks.Seq.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@

<PropertyGroup>
<Description>Serilog sink that writes to the Seq log server over HTTP/HTTPS.</Description>
<VersionPrefix>4.1.0</VersionPrefix>
<VersionPrefix>5.0.0</VersionPrefix>
<Authors>Serilog Contributors</Authors>
<Copyright>Copyright © Serilog Contributors 2013-2019</Copyright>
<TargetFrameworks>netstandard1.1;netstandard1.3;net45;netstandard2.0</TargetFrameworks>
<Copyright>Copyright © Serilog Contributors 2013-2020</Copyright>
<TargetFrameworks>netstandard1.1;netstandard1.3;net45;netstandard2.0;netcoreapp3.1</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>
<PackageId>Serilog.Sinks.Seq</PackageId>
<PackageTags>serilog;seq</PackageTags>
<PackageIconUrl>https://serilog.net/images/serilog-sink-seq-nuget.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/serilog/serilog-sinks-seq</PackageProjectUrl>
<PackageLicenseUrl>http://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<RepositoryUrl>https://github.com/serilog/serilog-sinks-seq</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<GenerateAssemblyVersionAttribute>true</GenerateAssemblyVersionAttribute>
Expand All @@ -26,11 +24,15 @@
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<DefineConstants>$(DefineConstants);DURABLE;THREADING_TIMER</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<DefineConstants>$(DefineConstants);DURABLE;THREADING_TIMER</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' ">
<DefineConstants>$(DefineConstants);DURABLE;THREADING_TIMER</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net45' ">
<DefineConstants>$(DefineConstants);DURABLE;THREADING_TIMER;HRESULTS</DefineConstants>
</PropertyGroup>
Expand All @@ -41,8 +43,8 @@

<ItemGroup>
<PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="2.2.0" />
<PackageReference Include="Serilog.Formatting.Compact" Version="1.0.0" />
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="2.3.0-dev-00760 " />
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.1' ">
Expand Down
4 changes: 2 additions & 2 deletions src/Serilog.Sinks.Seq/Sinks/Seq/Durable/DurableSeqSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public DurableSeqSink(
TimeSpan period,
long? bufferSizeLimitBytes,
long? eventBodyLimitBytes,
LoggingLevelSwitch levelControlSwitch,
ControlledLevelSwitch controlledSwitch,
HttpMessageHandler messageHandler,
long? retainedInvalidPayloadsLimitBytes)
{
Expand All @@ -50,7 +50,7 @@ public DurableSeqSink(
batchPostingLimit,
period,
eventBodyLimitBytes,
levelControlSwitch,
controlledSwitch,
messageHandler,
retainedInvalidPayloadsLimitBytes,
bufferSizeLimitBytes);
Expand Down
4 changes: 2 additions & 2 deletions src/Serilog.Sinks.Seq/Sinks/Seq/Durable/HttpLogShipper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ public HttpLogShipper(
int batchPostingLimit,
TimeSpan period,
long? eventBodyLimitBytes,
LoggingLevelSwitch levelControlSwitch,
ControlledLevelSwitch controlledSwitch,
HttpMessageHandler messageHandler,
long? retainedInvalidPayloadsLimitBytes,
long? bufferSizeLimitBytes)
{
_apiKey = apiKey;
_batchPostingLimit = batchPostingLimit;
_eventBodyLimitBytes = eventBodyLimitBytes;
_controlledSwitch = new ControlledLevelSwitch(levelControlSwitch);
_controlledSwitch = controlledSwitch;
_connectionSchedule = new ExponentialBackoffConnectionSchedule(period);
_retainedInvalidPayloadsLimitBytes = retainedInvalidPayloadsLimitBytes;
_bufferSizeLimitBytes = bufferSizeLimitBytes;
Expand Down
5 changes: 2 additions & 3 deletions src/Serilog.Sinks.Seq/Sinks/Seq/SeqApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace Serilog.Sinks.Seq
{
class SeqApi
static class SeqApi
{
public const string BulkUploadResource = "api/events/raw";
public const string ApiKeyHeaderName = "X-Seq-ApiKey";
Expand Down Expand Up @@ -49,8 +49,7 @@ class SeqApi
return null;

var value = eventInputResult.Substring(startValue, endValue - startValue);
LogEventLevel minimumLevel;
if (!Enum.TryParse(value, out minimumLevel))
if (!Enum.TryParse(value, out LogEventLevel minimumLevel))
return null;

return minimumLevel;
Expand Down
113 changes: 113 additions & 0 deletions src/Serilog.Sinks.Seq/Sinks/Seq/SeqPayloadFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Serilog.Sinks.Seq Copyright 2014-2019 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.

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Serilog.Debugging;
using Serilog.Events;
using Serilog.Formatting.Compact;
using Serilog.Formatting.Json;

namespace Serilog.Sinks.Seq
{
static class SeqPayloadFormatter
{
static readonly JsonValueFormatter JsonValueFormatter = new JsonValueFormatter();

public static string FormatCompactPayload(IEnumerable<LogEvent> events, long? eventBodyLimitBytes)
{
var payload = new StringWriter();

foreach (var logEvent in events)
{
var buffer = new StringWriter();

try
{
CompactJsonFormatter.FormatEvent(logEvent, buffer, JsonValueFormatter);
}
catch (Exception ex)
{
LogNonFormattableEvent(logEvent, ex);
continue;
}

var json = buffer.ToString();
if (CheckEventBodySize(json, eventBodyLimitBytes))
{
payload.WriteLine(json);
}
}

return payload.ToString();
}

public static string FormatRawPayload(IEnumerable<LogEvent> events, long? eventBodyLimitBytes)
{
var payload = new StringWriter();
payload.Write("{\"Events\":[");

var delimStart = "";
foreach (var logEvent in events)
{
var buffer = new StringWriter();

try
{
RawJsonFormatter.FormatContent(logEvent, buffer);
}
catch (Exception ex)
{
LogNonFormattableEvent(logEvent, ex);
continue;
}

var json = buffer.ToString();
if (CheckEventBodySize(json, eventBodyLimitBytes))
{
payload.Write(delimStart);
payload.Write(json);
delimStart = ",";
}
}

payload.Write("]}");
return payload.ToString();
}

static void LogNonFormattableEvent(LogEvent logEvent, Exception ex)
{
SelfLog.WriteLine(
"Event at {0} with message template {1} could not be formatted into JSON for Seq and will be dropped: {2}",
logEvent.Timestamp.ToString("o"), logEvent.MessageTemplate.Text, ex);
}

static bool CheckEventBodySize(string json, long? eventBodyLimitBytes)
{
if (eventBodyLimitBytes.HasValue &&
Encoding.UTF8.GetByteCount(json) > eventBodyLimitBytes.Value)
{
SelfLog.WriteLine(
"Event JSON representation exceeds the byte size limit of {0} set for this Seq sink and will be dropped; data: {1}",
eventBodyLimitBytes, json);
return false;
}

return true;
}

}
}
Loading