diff --git a/Build.ps1 b/Build.ps1 index 05746a5..0515652 100644 --- a/Build.ps1 +++ b/Build.ps1 @@ -2,20 +2,28 @@ Push-Location $PSScriptRoot if(Test-Path .\artifacts) { Remove-Item .\artifacts -Force -Recurse } -& dotnet restore +& dotnet restore --no-cache -$revision = @{ $true = $env:APPVEYOR_BUILD_NUMBER; $false = 1 }[$env:APPVEYOR_BUILD_NUMBER -ne $NULL]; +$branch = @{ $true = $env:APPVEYOR_REPO_BRANCH; $false = $(git symbolic-ref --short -q HEAD) }[$env:APPVEYOR_REPO_BRANCH -ne $NULL]; +$revision = @{ $true = "{0:00000}" -f [convert]::ToInt32("0" + $env:APPVEYOR_BUILD_NUMBER, 10); $false = "local" }[$env:APPVEYOR_BUILD_NUMBER -ne $NULL]; +$suffix = @{ $true = ""; $false = "$branch-$revision"}[$branch -eq "master" -and $revision -ne "local"] -Push-Location src/Serilog.Sinks.File +foreach ($src in ls src/Serilog.*) { + Push-Location $src -& dotnet pack -c Release -o ..\..\.\artifacts --version-suffix=$revision -if($LASTEXITCODE -ne 0) { exit 1 } + & dotnet pack -c Release -o ..\..\.\artifacts --version-suffix=$suffix + if($LASTEXITCODE -ne 0) { exit 1 } -Pop-Location -Push-Location test/Serilog.Sinks.File.Tests + Pop-Location +} -& dotnet test -c Release -if($LASTEXITCODE -ne 0) { exit 2 } +foreach ($test in ls test/Serilog.*.Tests) { + Push-Location $test + + & dotnet test -c Release + if($LASTEXITCODE -ne 0) { exit 2 } + + Pop-Location +} -Pop-Location Pop-Location diff --git a/CHANGES.md b/CHANGES.md index 424dd8e..82f849c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,6 @@ +2.1.0 + - Support alternative `ITextFormatter`s through the configuration API (#4) + 2.0.0 -- Moved to new project -- DotNet Core support \ No newline at end of file + - Moved to new project + - DotNet Core support \ No newline at end of file diff --git a/README.md b/README.md index 5ca6152..5f5b17e 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,6 @@ -# Serilog.Sinks.File +# Serilog.Sinks.File [![Build status](https://ci.appveyor.com/api/projects/status/hh9gymy0n6tne46j?svg=true)](https://ci.appveyor.com/project/serilog/serilog-sinks-file) [![NuGet Version](http://img.shields.io/nuget/v/Serilog.Sinks.File.svg?style=flat)](https://www.nuget.org/packages/Serilog.Sinks.File/) [![Documentation](https://img.shields.io/badge/docs-wiki-yellow.svg)](https://github.com/serilog/serilog/wiki) [![Join the chat at https://gitter.im/serilog/serilog](https://img.shields.io/gitter/room/serilog/serilog.svg)](https://gitter.im/serilog/serilog) -The file sink for Serilog. - -[![Build status](https://ci.appveyor.com/api/projects/status/hh9gymy0n6tne46j?svg=true)](https://ci.appveyor.com/project/serilog/serilog-sinks-file) [![NuGet Version](http://img.shields.io/nuget/v/Serilog.Sinks.File.svg?style=flat)](https://www.nuget.org/packages/Serilog.Sinks.File/) - - -Writes log events to a text file. +Writes [Serilog](https://serilog.net) events to a text file. ```csharp var log = new LoggerConfiguration() @@ -13,20 +8,31 @@ var log = new LoggerConfiguration() .CreateLogger(); ``` -To avoid sinking apps with runaway disk usage the file sink **limits file size to 1GB by default**. The limit can be increased or removed using the `fileSizeLimitBytes` parameter. +To avoid bringing down apps with runaway disk usage the file sink **limits file size to 1GB by default**. The limit can be increased or removed using the `fileSizeLimitBytes` parameter. ```csharp .WriteTo.File("log.txt", fileSizeLimitBytes: null) ``` -Or in XML [app-settings format](https://github.com/serilog/serilog/wiki/AppSettings): +> **Important:** Only one process may write to a log file at a given time. For multi-process scenarios, either use separate files or one of the non-file-based sinks. + +### `` configuration + +The sink can be configured in XML [app-settings format](https://github.com/serilog/serilog/wiki/AppSettings) if the _Serilog.Settings.AppSettings_ package is in use: ```xml + ``` -> **Important:** Only one process may write to a log file at a given time. For multi-process scenarios, either use separate files or one of the non-file-based sinks. +### JSON formatting + +To emit JSON, rather than plain text, a formatter can be specified: + +```csharp + .WriteTo.File(new JsonFormatter(), "log.txt") +``` -* [Documentation](https://github.com/serilog/serilog/wiki) +To configure an alternative formatter in XML ``, specify the formatter's assembly-qualified type name as the setting `value`. -Copyright © 2016 Serilog Contributors - Provided under the [Apache License, Version 2.0](http://apache.org/licenses/LICENSE-2.0.html). +_Copyright © 2016 Serilog Contributors - Provided under the [Apache License, Version 2.0](http://apache.org/licenses/LICENSE-2.0.html)._ diff --git a/appveyor.yml b/appveyor.yml index 6a39959..19d0d28 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,4 +1,5 @@ version: '{build}' +skip_tags: true image: Visual Studio 2015 configuration: Release install: @@ -18,5 +19,11 @@ deploy: secure: nvZ/z+pMS91b3kG4DgfES5AcmwwGoBYQxr9kp4XiJHj25SAlgdIxFx++1N0lFH2x skip_symbols: true on: - branch: /^(dev|master)$/ - + branch: /^(master|dev)$/ +- provider: GitHub + auth_token: + secure: p4LpVhBKxGS5WqucHxFQ5c7C8cP74kbNB0Z8k9Oxx/PMaDQ1+ibmoexNqVU5ZlmX + artifact: /Serilog.*\.nupkg/ + tag: v$(appveyor_build_version) + on: + branch: master diff --git a/src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs b/src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs index 42e6d01..d31acb4 100644 --- a/src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs +++ b/src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs @@ -17,7 +17,9 @@ using Serilog.Core; using Serilog.Debugging; using Serilog.Events; +using Serilog.Formatting; using Serilog.Formatting.Display; +using Serilog.Formatting.Json; using Serilog.Sinks.File; namespace Serilog @@ -57,8 +59,45 @@ public static LoggerConfiguration File( bool buffered = false) { if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration)); + if (path == null) throw new ArgumentNullException(nameof(path)); if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate)); + var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider); + return File(sinkConfiguration, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes, levelSwitch, buffered); + } + + /// + /// Write log events to the specified file. + /// + /// Logger sink configuration. + /// A formatter, such as , to convert the log events into + /// text for the file. If control of regular text formatting is required, use the other + /// overload of + /// and specify the outputTemplate parameter instead. + /// + /// Path to the file. + /// The minimum level for + /// events passed through the sink. Ignored when is specified. + /// A switch allowing the pass-through minimum level + /// to be changed at runtime. + /// The maximum size, in bytes, to which a log file will be allowed to grow. + /// For unrestricted growth, pass null. The default is 1 GB. + /// Indicates if flushing to the output file can be buffered or not. The default + /// is false. + /// Configuration object allowing method chaining. + /// The file will be written using the UTF-8 character set. + public static LoggerConfiguration File( + this LoggerSinkConfiguration sinkConfiguration, + ITextFormatter formatter, + string path, + LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, + long? fileSizeLimitBytes = DefaultFileSizeLimitBytes, + LoggingLevelSwitch levelSwitch = null, + bool buffered = false) + { + if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration)); + if (formatter == null) throw new ArgumentNullException(nameof(formatter)); + if (path == null) throw new ArgumentNullException(nameof(path)); FileSink sink; try diff --git a/src/Serilog.Sinks.File/Properties/AssemblyInfo.cs b/src/Serilog.Sinks.File/Properties/AssemblyInfo.cs index 1237c0e..0d5d620 100644 --- a/src/Serilog.Sinks.File/Properties/AssemblyInfo.cs +++ b/src/Serilog.Sinks.File/Properties/AssemblyInfo.cs @@ -6,7 +6,7 @@ [assembly: CLSCompliant(true)] -[assembly: InternalsVisibleTo("Serilog.Tests, PublicKey=" + +[assembly: InternalsVisibleTo("Serilog.Sinks.File.Tests, PublicKey=" + "0024000004800000940000000602000000240000525341310004000001000100fb8d13fd344a1c" + "6fe0fe83ef33c1080bf30690765bc6eb0df26ebfdf8f21670c64265b30db09f73a0dea5b3db4c9" + "d18dbf6d5a25af5ce9016f281014d79dc3b4201ac646c451830fc7e61a2dfd633d34c39f87b818" + diff --git a/src/Serilog.Sinks.File/Sinks/File/FileSink.cs b/src/Serilog.Sinks.File/Sinks/File/FileSink.cs index 5c92ff0..cbfc37f 100644 --- a/src/Serilog.Sinks.File/Sinks/File/FileSink.cs +++ b/src/Serilog.Sinks.File/Sinks/File/FileSink.cs @@ -44,8 +44,7 @@ public sealed class FileSink : ILogEventSink, IDisposable /// Configuration object allowing method chaining. /// The file will be written using the UTF-8 character set. /// - public FileSink(string path, ITextFormatter textFormatter, long? fileSizeLimitBytes, Encoding encoding = null, - bool buffered = false) + public FileSink(string path, ITextFormatter textFormatter, long? fileSizeLimitBytes, Encoding encoding = null, bool buffered = false) { if (path == null) throw new ArgumentNullException(nameof(path)); if (textFormatter == null) throw new ArgumentNullException(nameof(textFormatter)); diff --git a/src/Serilog.Sinks.File/project.json b/src/Serilog.Sinks.File/project.json index a2a9d53..cd35902 100644 --- a/src/Serilog.Sinks.File/project.json +++ b/src/Serilog.Sinks.File/project.json @@ -1,6 +1,6 @@ { - "version": "2.0.0", - "description": "The file sink for Serilog", + "version": "2.1.0-*", + "description": "Write Serilog events to a text file in plain or JSON format.", "authors": [ "Serilog Contributors" ], "packOptions": { "tags": [ "serilog", "file", "io" ], diff --git a/test/Serilog.Sinks.File.Tests/Sinks/File/FileSinkTests.cs b/test/Serilog.Sinks.File.Tests/Sinks/File/FileSinkTests.cs new file mode 100644 index 0000000..e71cac3 --- /dev/null +++ b/test/Serilog.Sinks.File.Tests/Sinks/File/FileSinkTests.cs @@ -0,0 +1,75 @@ +using System.IO; +using Xunit; +using Serilog.Formatting.Json; +using Serilog.Sinks.File.Tests.Support; +using Serilog.Tests.Support; + +namespace Serilog.Sinks.File.Tests +{ + public class FileSinkTests + { + [Fact] + public void FileIsWrittenIfNonexistent() + { + using (var tmp = TempFolder.ForCaller()) + { + var nonexistent = tmp.AllocateFilename("txt"); + var evt = Some.LogEvent("Hello, world!"); + + using (var sink = new FileSink(nonexistent, new JsonFormatter(), null)) + { + sink.Emit(evt); + } + + var lines = System.IO.File.ReadAllLines(nonexistent); + Assert.Contains("Hello, world!", lines[0]); + } + } + + [Fact] + public void FileIsAppendedToWhenAlreadyCreated() + { + using (var tmp = TempFolder.ForCaller()) + { + var path = tmp.AllocateFilename("txt"); + var evt = Some.LogEvent("Hello, world!"); + + using (var sink = new FileSink(path, new JsonFormatter(), null)) + { + sink.Emit(evt); + } + + using (var sink = new FileSink(path, new JsonFormatter(), null)) + { + sink.Emit(evt); + } + + var lines = System.IO.File.ReadAllLines(path); + Assert.Contains("Hello, world!", lines[0]); + Assert.Contains("Hello, world!", lines[1]); + } + } + + [Fact] + public void WhenLimitIsSpecifiedFileSizeIsRestricted() + { + const int maxBytes = 100; + + using (var tmp = TempFolder.ForCaller()) + { + var path = tmp.AllocateFilename("txt"); + var evt = Some.LogEvent(new string('n', maxBytes + 1)); + + using (var sink = new FileSink(path, new JsonFormatter(), maxBytes)) + { + sink.Emit(evt); + } + + var size = new FileInfo(path).Length; + Assert.True(size > 0); + Assert.True(size < maxBytes); + } + } + } +} + diff --git a/test/Serilog.Sinks.File.Tests/Sinks/IOFile/FileSinkTests.cs b/test/Serilog.Sinks.File.Tests/Sinks/IOFile/FileSinkTests.cs deleted file mode 100644 index df4d49e..0000000 --- a/test/Serilog.Sinks.File.Tests/Sinks/IOFile/FileSinkTests.cs +++ /dev/null @@ -1,91 +0,0 @@ -#if FILE_IO - -using System; -using System.IO; -using System.Linq; -using Xunit; -using Serilog.Events; -using Serilog.Tests.Support; - -namespace Serilog.Tests.Sinks.IOFile -{ - public class FileSinkTests - { - [Fact] - public void FileIsWrittenIfNonexistent() - { - var path = Some.NonexistentTempFilePath(); - TestLoggingAndDelete(path); - } - - [Fact] - public void FileIsAppendedToWhenAlreadyCreated() - { - var path = Some.TempFilePath(); - TestLoggingAndDelete(path); - } - - [Fact] - public void WhenLimitIsSpecifiedFileSizeIsRestricted() - { - const int maxBytes = 100; - var path = Some.NonexistentTempFilePath(); - ExecuteAndCleanUpFile( - path, - p => new LoggerConfiguration() - .WriteTo.File(p, fileSizeLimitBytes: maxBytes) - .CreateLogger(), - log => - { - log.Information(new string('n', maxBytes + 1)); - var size = new FileInfo(path).Length; - Assert.True(size > 0); - Assert.True(size < maxBytes); - }); - } - - static void TestLoggingAndDelete(string path) - { - ExecuteAndCleanUpFile( - path, - p => new LoggerConfiguration() - .WriteTo.File(p) - .CreateLogger(), - log => - { - var message = Some.MessageTemplate(); - - log.Write(new LogEvent( - DateTimeOffset.Now, - LogEventLevel.Information, - null, - message, - Enumerable.Empty())); - - var refile = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - var content = new StreamReader(refile).ReadToEnd(); - refile.Dispose(); - - Assert.True(content.Contains(message.Text)); - }); - } - - static void ExecuteAndCleanUpFile(string path, Func configure, Action test) - { - ILogger log = null; - - try - { - log = configure(path); - test(log); - } - finally - { - var disposable = (IDisposable) log; - if (disposable != null) disposable.Dispose(); - File.Delete(path); - } - } - } -} -#endif diff --git a/test/Serilog.Sinks.File.Tests/Support/CollectingSink.cs b/test/Serilog.Sinks.File.Tests/Support/CollectingSink.cs deleted file mode 100644 index 4ff4a7b..0000000 --- a/test/Serilog.Sinks.File.Tests/Support/CollectingSink.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using Serilog.Core; -using Serilog.Events; - -namespace Serilog.Tests.Support -{ - class CollectingSink : ILogEventSink - { - readonly List _events = new List(); - - public List Events { get { return _events; } } - - public LogEvent SingleEvent { get { return _events.Single(); } } - - public void Emit(LogEvent logEvent) - { - _events.Add(logEvent); - } - } -} diff --git a/test/Serilog.Sinks.File.Tests/Support/DelegateDisposable.cs b/test/Serilog.Sinks.File.Tests/Support/DelegateDisposable.cs deleted file mode 100644 index fbafd38..0000000 --- a/test/Serilog.Sinks.File.Tests/Support/DelegateDisposable.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; - -namespace Serilog.Tests.Support -{ - public class DelegateDisposable : IDisposable - { - private readonly Action _disposeAction; - private bool _disposed; - - public DelegateDisposable(Action disposeAction) - { - _disposeAction = disposeAction; - } - - public void Dispose() - { - if (_disposed) - return; - - _disposeAction(); - _disposed = true; - } - } -} \ No newline at end of file diff --git a/test/Serilog.Sinks.File.Tests/Support/DelegatingEnricher.cs b/test/Serilog.Sinks.File.Tests/Support/DelegatingEnricher.cs deleted file mode 100644 index 1d49f31..0000000 --- a/test/Serilog.Sinks.File.Tests/Support/DelegatingEnricher.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System; -using Serilog.Core; -using Serilog.Events; - -namespace Serilog.Tests.Support -{ - class DelegatingEnricher : ILogEventEnricher - { - readonly Action _enrich; - - public DelegatingEnricher(Action enrich) - { - if (enrich == null) throw new ArgumentNullException(nameof(enrich)); - _enrich = enrich; - } - - public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) - { - _enrich(logEvent, propertyFactory); - } - } -} diff --git a/test/Serilog.Sinks.File.Tests/Support/DelegatingSink.cs b/test/Serilog.Sinks.File.Tests/Support/DelegatingSink.cs deleted file mode 100644 index e19ee78..0000000 --- a/test/Serilog.Sinks.File.Tests/Support/DelegatingSink.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using Serilog.Core; -using Serilog.Events; - -namespace Serilog.Tests.Support -{ - public class DelegatingSink : ILogEventSink - { - readonly Action _write; - - public DelegatingSink(Action write) - { - if (write == null) throw new ArgumentNullException(nameof(write)); - _write = write; - } - - public void Emit(LogEvent logEvent) - { - _write(logEvent); - } - - public static LogEvent GetLogEvent(Action writeAction) - { - LogEvent result = null; - var l = new LoggerConfiguration() - .WriteTo.Sink(new DelegatingSink(le => result = le)) - .CreateLogger(); - - writeAction(l); - return result; - } - } -} diff --git a/test/Serilog.Sinks.File.Tests/Support/DisposableLogger.cs b/test/Serilog.Sinks.File.Tests/Support/DisposableLogger.cs deleted file mode 100644 index ccff568..0000000 --- a/test/Serilog.Sinks.File.Tests/Support/DisposableLogger.cs +++ /dev/null @@ -1,422 +0,0 @@ -using System; -using System.Collections.Generic; -using Serilog.Core; -using Serilog.Events; - -namespace Serilog.Tests.Support -{ - public class DisposableLogger : ILogger, IDisposable - { - public bool Disposed { get; set; } - - public void Dispose() - { - Disposed = true; - } - - public ILogger ForContext(ILogEventEnricher enricher) - { - throw new NotImplementedException(); - } - - public ILogger ForContext(IEnumerable enrichers) - { - throw new NotImplementedException(); - } - - public ILogger ForContext(string propertyName, object value, bool destructureObjects = false) - { - throw new NotImplementedException(); - } - - public ILogger ForContext() - { - throw new NotImplementedException(); - } - - public ILogger ForContext(Type source) - { - throw new NotImplementedException(); - } - - public void Write(LogEvent logEvent) - { - throw new NotImplementedException(); - } - - public void Write(LogEventLevel level, string messageTemplate) - { - throw new NotImplementedException(); - } - - public void Write(LogEventLevel level, string messageTemplate, T propertyValue) - { - throw new NotImplementedException(); - } - - public void Write(LogEventLevel level, string messageTemplate, T0 propertyValue0, T1 propertyValue1) - { - throw new NotImplementedException(); - } - - public void Write(LogEventLevel level, string messageTemplate, T0 propertyValue0, T1 propertyValue1, - T2 propertyValue2) - { - throw new NotImplementedException(); - } - - public void Write(LogEventLevel level, string messageTemplate, params object[] propertyValues) - { - throw new NotImplementedException(); - } - - public void Write(LogEventLevel level, Exception exception, string messageTemplate) - { - throw new NotImplementedException(); - } - - public void Write(LogEventLevel level, Exception exception, string messageTemplate, T propertyValue) - { - throw new NotImplementedException(); - } - - public void Write(LogEventLevel level, Exception exception, string messageTemplate, T0 propertyValue0, - T1 propertyValue1) - { - throw new NotImplementedException(); - } - - public void Write(LogEventLevel level, Exception exception, string messageTemplate, T0 propertyValue0, - T1 propertyValue1, T2 propertyValue2) - { - throw new NotImplementedException(); - } - - public void Write(LogEventLevel level, Exception exception, string messageTemplate, params object[] propertyValues) - { - throw new NotImplementedException(); - } - - public bool IsEnabled(LogEventLevel level) - { - throw new NotImplementedException(); - } - - public void Verbose(string messageTemplate) - { - throw new NotImplementedException(); - } - - public void Verbose(string messageTemplate, T propertyValue) - { - throw new NotImplementedException(); - } - - public void Verbose(string messageTemplate, T0 propertyValue0, T1 propertyValue1) - { - throw new NotImplementedException(); - } - - public void Verbose(string messageTemplate, T0 propertyValue0, T1 propertyValue1, T2 propertyValue2) - { - throw new NotImplementedException(); - } - - public void Verbose(string messageTemplate, params object[] propertyValues) - { - throw new NotImplementedException(); - } - - public void Verbose(Exception exception, string messageTemplate) - { - throw new NotImplementedException(); - } - - public void Verbose(Exception exception, string messageTemplate, T propertyValue) - { - throw new NotImplementedException(); - } - - public void Verbose(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) - { - throw new NotImplementedException(); - } - - public void Verbose(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, - T2 propertyValue2) - { - throw new NotImplementedException(); - } - - public void Verbose(Exception exception, string messageTemplate, params object[] propertyValues) - { - throw new NotImplementedException(); - } - - public void Debug(string messageTemplate) - { - throw new NotImplementedException(); - } - - public void Debug(string messageTemplate, T propertyValue) - { - throw new NotImplementedException(); - } - - public void Debug(string messageTemplate, T0 propertyValue0, T1 propertyValue1) - { - throw new NotImplementedException(); - } - - public void Debug(string messageTemplate, T0 propertyValue0, T1 propertyValue1, T2 propertyValue2) - { - throw new NotImplementedException(); - } - - public void Debug(string messageTemplate, params object[] propertyValues) - { - throw new NotImplementedException(); - } - - public void Debug(Exception exception, string messageTemplate) - { - throw new NotImplementedException(); - } - - public void Debug(Exception exception, string messageTemplate, T propertyValue) - { - throw new NotImplementedException(); - } - - public void Debug(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) - { - throw new NotImplementedException(); - } - - public void Debug(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, - T2 propertyValue2) - { - throw new NotImplementedException(); - } - - public void Debug(Exception exception, string messageTemplate, params object[] propertyValues) - { - throw new NotImplementedException(); - } - - public void Information(string messageTemplate) - { - throw new NotImplementedException(); - } - - public void Information(string messageTemplate, T propertyValue) - { - throw new NotImplementedException(); - } - - public void Information(string messageTemplate, T0 propertyValue0, T1 propertyValue1) - { - throw new NotImplementedException(); - } - - public void Information(string messageTemplate, T0 propertyValue0, T1 propertyValue1, T2 propertyValue2) - { - throw new NotImplementedException(); - } - - public void Information(string messageTemplate, params object[] propertyValues) - { - throw new NotImplementedException(); - } - - public void Information(Exception exception, string messageTemplate) - { - throw new NotImplementedException(); - } - - public void Information(Exception exception, string messageTemplate, T propertyValue) - { - throw new NotImplementedException(); - } - - public void Information(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) - { - throw new NotImplementedException(); - } - - public void Information(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, - T2 propertyValue2) - { - throw new NotImplementedException(); - } - - public void Information(Exception exception, string messageTemplate, params object[] propertyValues) - { - throw new NotImplementedException(); - } - - public void Warning(string messageTemplate) - { - throw new NotImplementedException(); - } - - public void Warning(string messageTemplate, T propertyValue) - { - throw new NotImplementedException(); - } - - public void Warning(string messageTemplate, T0 propertyValue0, T1 propertyValue1) - { - throw new NotImplementedException(); - } - - public void Warning(string messageTemplate, T0 propertyValue0, T1 propertyValue1, T2 propertyValue2) - { - throw new NotImplementedException(); - } - - public void Warning(string messageTemplate, params object[] propertyValues) - { - throw new NotImplementedException(); - } - - public void Warning(Exception exception, string messageTemplate) - { - throw new NotImplementedException(); - } - - public void Warning(Exception exception, string messageTemplate, T propertyValue) - { - throw new NotImplementedException(); - } - - public void Warning(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) - { - throw new NotImplementedException(); - } - - public void Warning(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, - T2 propertyValue2) - { - throw new NotImplementedException(); - } - - public void Warning(Exception exception, string messageTemplate, params object[] propertyValues) - { - throw new NotImplementedException(); - } - - public void Error(string messageTemplate) - { - throw new NotImplementedException(); - } - - public void Error(string messageTemplate, T propertyValue) - { - throw new NotImplementedException(); - } - - public void Error(string messageTemplate, T0 propertyValue0, T1 propertyValue1) - { - throw new NotImplementedException(); - } - - public void Error(string messageTemplate, T0 propertyValue0, T1 propertyValue1, T2 propertyValue2) - { - throw new NotImplementedException(); - } - - public void Error(string messageTemplate, params object[] propertyValues) - { - throw new NotImplementedException(); - } - - public void Error(Exception exception, string messageTemplate) - { - throw new NotImplementedException(); - } - - public void Error(Exception exception, string messageTemplate, T propertyValue) - { - throw new NotImplementedException(); - } - - public void Error(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) - { - throw new NotImplementedException(); - } - - public void Error(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, - T2 propertyValue2) - { - throw new NotImplementedException(); - } - - public void Error(Exception exception, string messageTemplate, params object[] propertyValues) - { - throw new NotImplementedException(); - } - - public void Fatal(string messageTemplate) - { - throw new NotImplementedException(); - } - - public void Fatal(string messageTemplate, T propertyValue) - { - throw new NotImplementedException(); - } - - public void Fatal(string messageTemplate, T0 propertyValue0, T1 propertyValue1) - { - throw new NotImplementedException(); - } - - public void Fatal(string messageTemplate, T0 propertyValue0, T1 propertyValue1, T2 propertyValue2) - { - throw new NotImplementedException(); - } - - public void Fatal(string messageTemplate, params object[] propertyValues) - { - throw new NotImplementedException(); - } - - public void Fatal(Exception exception, string messageTemplate) - { - throw new NotImplementedException(); - } - - public void Fatal(Exception exception, string messageTemplate, T propertyValue) - { - throw new NotImplementedException(); - } - - public void Fatal(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) - { - throw new NotImplementedException(); - } - - public void Fatal(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, - T2 propertyValue2) - { - throw new NotImplementedException(); - } - - public void Fatal(Exception exception, string messageTemplate, params object[] propertyValues) - { - throw new NotImplementedException(); - } - - public bool BindMessageTemplate(string messageTemplate, object[] propertyValues, out MessageTemplate parsedTemplate, - out IEnumerable boundProperties) - { - throw new NotImplementedException(); - } - - public bool BindProperty(string propertyName, object value, bool destructureObjects, out LogEventProperty property) - { - throw new NotImplementedException(); - } - } -} \ No newline at end of file diff --git a/test/Serilog.Sinks.File.Tests/Support/DisposeTrackingSink.cs b/test/Serilog.Sinks.File.Tests/Support/DisposeTrackingSink.cs deleted file mode 100644 index 8092418..0000000 --- a/test/Serilog.Sinks.File.Tests/Support/DisposeTrackingSink.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using Serilog.Core; -using Serilog.Events; - -namespace Serilog.Tests.Support -{ - class DisposeTrackingSink : ILogEventSink, IDisposable - { - public bool IsDisposed { get; set; } - - public void Emit(LogEvent logEvent) - { - } - - public void Dispose() - { - IsDisposed = true; - } - } -} \ No newline at end of file diff --git a/test/Serilog.Sinks.File.Tests/Support/Extensions.cs b/test/Serilog.Sinks.File.Tests/Support/Extensions.cs deleted file mode 100644 index b4fa640..0000000 --- a/test/Serilog.Sinks.File.Tests/Support/Extensions.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Serilog.Events; - -namespace Serilog.Tests.Support -{ - public static class Extensions - { - public static object LiteralValue(this LogEventPropertyValue @this) - { - return ((ScalarValue)@this).Value; - } - } -} diff --git a/test/Serilog.Sinks.File.Tests/Support/Some.cs b/test/Serilog.Sinks.File.Tests/Support/Some.cs index 0df2968..a831492 100644 --- a/test/Serilog.Sinks.File.Tests/Support/Some.cs +++ b/test/Serilog.Sinks.File.Tests/Support/Some.cs @@ -1,87 +1,24 @@ using System; -using System.IO; -using System.Linq; -using System.Threading; +using System.Collections.Generic; using Serilog.Events; -using Serilog.Parsing; +using Xunit.Sdk; namespace Serilog.Tests.Support { static class Some { - static int Counter; - - public static int Int() - { - return Interlocked.Increment(ref Counter); - } - - public static decimal Decimal() - { - return Int() + 0.123m; - } - - public static string String(string tag = null) - { - return (tag ?? "") + "__" + Int(); - } - - public static TimeSpan TimeSpan() - { - return System.TimeSpan.FromMinutes(Int()); - } - - public static DateTime Instant() - { - return new DateTime(2012, 10, 28) + TimeSpan(); - } - - public static DateTimeOffset OffsetInstant() - { - return new DateTimeOffset(Instant()); - } - - public static LogEvent LogEvent(DateTimeOffset? timestamp = null, LogEventLevel level = LogEventLevel.Information) - { - return new LogEvent(timestamp ?? OffsetInstant(), level, - null, MessageTemplate(), Enumerable.Empty()); - } - - public static LogEvent InformationEvent(DateTimeOffset? timestamp = null) - { - return LogEvent(timestamp, LogEventLevel.Information); - } - - public static LogEvent DebugEvent(DateTimeOffset? timestamp = null) - { - return LogEvent(timestamp, LogEventLevel.Debug); - } - - public static LogEventProperty LogEventProperty() - { - return new LogEventProperty(String(), new ScalarValue(Int())); - } - - public static string NonexistentTempFilePath() - { - return Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".txt"); - } - - public static string TempFilePath() - { - return Path.GetTempFileName(); - } - - public static string TempFolderPath() - { - var dir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(dir); - return dir; - } - - public static MessageTemplate MessageTemplate() - { - return new MessageTemplateParser().Parse(String()); + public static LogEvent LogEvent(string messageTemplate, params object[] propertyValues) + { + var log = new LoggerConfiguration().CreateLogger(); + MessageTemplate template; + IEnumerable properties; +#pragma warning disable Serilog004 // Constant MessageTemplate verifier + if (!log.BindMessageTemplate(messageTemplate, propertyValues, out template, out properties)) +#pragma warning restore Serilog004 // Constant MessageTemplate verifier + { + throw new XunitException("Template could not be bound."); + } + return new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, template, properties); } } } diff --git a/test/Serilog.Sinks.File.Tests/Support/TempFolder.cs b/test/Serilog.Sinks.File.Tests/Support/TempFolder.cs new file mode 100644 index 0000000..a57d71f --- /dev/null +++ b/test/Serilog.Sinks.File.Tests/Support/TempFolder.cs @@ -0,0 +1,51 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Runtime.CompilerServices; + +namespace Serilog.Sinks.File.Tests.Support +{ + class TempFolder : IDisposable + { + static readonly Guid Session = Guid.NewGuid(); + + readonly string _tempFolder; + + public TempFolder(string name) + { + _tempFolder = System.IO.Path.Combine( + Environment.GetEnvironmentVariable("TMP"), + "Serilog.Sinks.File.Tests", + Session.ToString("n"), + name); + + Directory.CreateDirectory(_tempFolder); + } + + public string Path => _tempFolder; + + public void Dispose() + { + try + { + if (Directory.Exists(_tempFolder)) + Directory.Delete(_tempFolder, true); + } + catch (Exception ex) + { + Debug.WriteLine(ex); + } + } + + public static TempFolder ForCaller([CallerMemberName] string caller = null) + { + if (caller == null) throw new ArgumentNullException(nameof(caller)); + return new TempFolder(caller); + } + + public string AllocateFilename(string ext = null) + { + return System.IO.Path.Combine(Path, Guid.NewGuid().ToString("n") + "." + (ext ?? "tmp")); + } + } +} diff --git a/test/Serilog.Sinks.File.Tests/project.json b/test/Serilog.Sinks.File.Tests/project.json index 28225cc..8b7feb8 100644 --- a/test/Serilog.Sinks.File.Tests/project.json +++ b/test/Serilog.Sinks.File.Tests/project.json @@ -1,5 +1,4 @@ { - "version": "2.0.0", "testRunner": "xunit", "dependencies": { "Serilog.Sinks.File": { "target": "project" },