-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from serilog/dev
3.2.0 Release
- Loading branch information
Showing
15 changed files
with
258 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
root=true | ||
root = true | ||
|
||
[*] | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 4 | ||
|
||
[*.{csproj,json,config,yml}] | ||
indent_size = 2 | ||
|
||
[*.sh] | ||
end_of_line = lf | ||
|
||
[*.{cmd, bat}] | ||
end_of_line = crlf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -234,3 +234,4 @@ _Pvt_Extensions | |
|
||
# FAKE - F# Make | ||
.fake/ | ||
example/Sample/log.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
language: csharp | ||
|
||
#dotnet cli require Ubuntu 14.04 | ||
sudo: required | ||
dist: trusty | ||
|
||
#dotnet cli require OSX 10.10 | ||
osx_image: xcode7.1 | ||
|
||
addons: | ||
apt: | ||
packages: | ||
- gettext | ||
- libcurl4-openssl-dev | ||
- libicu-dev | ||
- libssl-dev | ||
- libunwind8 | ||
- zlib1g | ||
|
||
os: | ||
- linux | ||
|
||
env: | ||
global: | ||
- DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true | ||
- TMP: /tmp | ||
|
||
matrix: | ||
- CLI_VERSION=1.0.0-preview2-003121 | ||
- CLI_VERSION=Latest | ||
|
||
matrix: | ||
allow_failures: | ||
- env: CLI_VERSION=Latest | ||
|
||
before_install: | ||
- if test "$TRAVIS_OS_NAME" == "osx"; then brew update; brew install openssl; brew link --force openssl; fi | ||
# Download script to install dotnet cli | ||
- if test "$CLI_OBTAIN_URL" == ""; then export CLI_OBTAIN_URL="https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0-preview2/scripts/obtain/dotnet-install.sh"; fi | ||
- curl -L --create-dirs $CLI_OBTAIN_URL -o ./scripts/obtain/install.sh | ||
- find ./scripts -name "*.sh" -exec chmod +x {} \; | ||
- export DOTNET_INSTALL_DIR="$PWD/.dotnetcli" | ||
# use bash to workaround bug https://github.com/dotnet/cli/issues/1725 | ||
- sudo bash ./scripts/obtain/install.sh --channel "preview" --version "$CLI_VERSION" --install-dir "$DOTNET_INSTALL_DIR" --no-path | ||
# add dotnet to PATH | ||
- export PATH="$DOTNET_INSTALL_DIR:$PATH" | ||
|
||
script: | ||
- ./build.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/bin/bash | ||
dotnet restore --no-cache | ||
for path in src/*/project.json; do | ||
dirname="$(dirname "${path}")" | ||
dotnet build ${dirname} -f netstandard1.3 -c Release | ||
done | ||
|
||
for path in test/*.Tests/project.json; do | ||
dirname="$(dirname "${path}")" | ||
dotnet build ${dirname} -f netcoreapp1.0 -c Release | ||
dotnet test ${dirname} -f netcoreapp1.0 -c Release | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
src/Serilog.Sinks.File/Sinks/File/SharedFileSink.OSMutex.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
// Copyright 2013-2016 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 OS_MUTEX | ||
|
||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
using Serilog.Formatting; | ||
using System.Threading; | ||
using Serilog.Debugging; | ||
|
||
namespace Serilog.Sinks.File | ||
{ | ||
/// <summary> | ||
/// Write log events to a disk file. | ||
/// </summary> | ||
public sealed class SharedFileSink : ILogEventSink, IFlushableFileSink, IDisposable | ||
{ | ||
readonly TextWriter _output; | ||
readonly FileStream _underlyingStream; | ||
readonly ITextFormatter _textFormatter; | ||
readonly long? _fileSizeLimitBytes; | ||
readonly object _syncRoot = new object(); | ||
|
||
const string MutexNameSuffix = ".serilog"; | ||
const int MutexWaitTimeout = 10000; | ||
readonly Mutex _mutex; | ||
|
||
/// <summary>Construct a <see cref="FileSink"/>.</summary> | ||
/// <param name="path">Path to the file.</param> | ||
/// <param name="textFormatter">Formatter used to convert log events to text.</param> | ||
/// <param name="fileSizeLimitBytes">The approximate maximum size, in bytes, to which a log file will be allowed to grow. | ||
/// For unrestricted growth, pass null. The default is 1 GB. To avoid writing partial events, the last event within the limit | ||
/// will be written in full even if it exceeds the limit.</param> | ||
/// <param name="encoding">Character encoding used to write the text file. The default is UTF-8 without BOM.</param> | ||
/// <returns>Configuration object allowing method chaining.</returns> | ||
/// <remarks>The file will be written using the UTF-8 character set.</remarks> | ||
/// <exception cref="IOException"></exception> | ||
public SharedFileSink(string path, ITextFormatter textFormatter, long? fileSizeLimitBytes, Encoding encoding = null) | ||
{ | ||
if (path == null) throw new ArgumentNullException(nameof(path)); | ||
if (textFormatter == null) throw new ArgumentNullException(nameof(textFormatter)); | ||
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 0) | ||
throw new ArgumentException("Negative value provided; file size limit must be non-negative"); | ||
|
||
_textFormatter = textFormatter; | ||
_fileSizeLimitBytes = fileSizeLimitBytes; | ||
|
||
var directory = Path.GetDirectoryName(path); | ||
if (!string.IsNullOrWhiteSpace(directory) && !Directory.Exists(directory)) | ||
{ | ||
Directory.CreateDirectory(directory); | ||
} | ||
|
||
var mutexName = Path.GetFullPath(path).Replace(Path.DirectorySeparatorChar, ':') + MutexNameSuffix; | ||
_mutex = new Mutex(false, mutexName); | ||
_underlyingStream = System.IO.File.Open(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); | ||
_output = new StreamWriter(_underlyingStream, encoding ?? new UTF8Encoding(encoderShouldEmitUTF8Identifier: false)); | ||
} | ||
|
||
/// <summary> | ||
/// Emit the provided log event to the sink. | ||
/// </summary> | ||
/// <param name="logEvent">The log event to write.</param> | ||
public void Emit(LogEvent logEvent) | ||
{ | ||
if (logEvent == null) throw new ArgumentNullException(nameof(logEvent)); | ||
|
||
lock (_syncRoot) | ||
{ | ||
if (!TryAcquireMutex()) | ||
return; | ||
|
||
try | ||
{ | ||
_underlyingStream.Seek(0, SeekOrigin.End); | ||
if (_fileSizeLimitBytes != null) | ||
{ | ||
if (_underlyingStream.Length >= _fileSizeLimitBytes.Value) | ||
return; | ||
} | ||
|
||
_textFormatter.Format(logEvent, _output); | ||
_output.Flush(); | ||
_underlyingStream.Flush(); | ||
} | ||
finally | ||
{ | ||
ReleaseMutex(); | ||
} | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void Dispose() | ||
{ | ||
lock (_syncRoot) | ||
{ | ||
_output.Dispose(); | ||
_mutex.Dispose(); | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void FlushToDisk() | ||
{ | ||
lock (_syncRoot) | ||
{ | ||
if (!TryAcquireMutex()) | ||
return; | ||
|
||
try | ||
{ | ||
_underlyingStream.Flush(true); | ||
} | ||
finally | ||
{ | ||
ReleaseMutex(); | ||
} | ||
} | ||
} | ||
|
||
bool TryAcquireMutex() | ||
{ | ||
try | ||
{ | ||
if (!_mutex.WaitOne(MutexWaitTimeout)) | ||
{ | ||
SelfLog.WriteLine("Shared file mutex could not be acquired within {0} ms", MutexWaitTimeout); | ||
return false; | ||
} | ||
} | ||
catch (AbandonedMutexException) | ||
{ | ||
SelfLog.WriteLine("Inherited shared file mutex after abandonment by another process"); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void ReleaseMutex() | ||
{ | ||
_mutex.ReleaseMutex(); | ||
} | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.