Skip to content

Commit ce5b339

Browse files
committed
Removed awkward namespace clashes; added simple in memory sink to enable phone/Win8 testing.
1 parent ef6d092 commit ce5b339

File tree

10 files changed

+104
-12
lines changed

10 files changed

+104
-12
lines changed

src/Serilog.FullNetFx/LoggerConfigurationFullNetFxExtensions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
using Serilog.Configuration;
1717
using Serilog.Events;
1818
using Serilog.Formatting.Display;
19+
using Serilog.Sinks.DiagnosticTrace;
1920
using Serilog.Sinks.DumpFile;
20-
using Serilog.Sinks.File;
21+
using Serilog.Sinks.IOFile;
2122
using Serilog.Sinks.RollingFile;
2223
using Serilog.Sinks.SystemConsole;
23-
using Serilog.Sinks.Trace;
2424

2525
namespace Serilog
2626
{

src/Serilog.FullNetFx/Serilog.FullNetFx.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@
4848
<Compile Include="LoggerConfigurationFullNetFxExtensions.cs" />
4949
<Compile Include="Properties\AssemblyInfo.cs" />
5050
<Compile Include="Sinks\DumpFile\DumpFileSink.cs" />
51-
<Compile Include="Sinks\File\FileSink.cs" />
51+
<Compile Include="Sinks\IOFile\FileSink.cs" />
5252
<Compile Include="Sinks\RollingFile\RollingFileSink.cs" />
5353
<Compile Include="Sinks\SystemConsole\ConsoleSink.cs" />
54-
<Compile Include="Sinks\Trace\DiagnosticTraceSink.cs" />
54+
<Compile Include="Sinks\DiagnosticTrace\DiagnosticTraceSink.cs" />
5555
</ItemGroup>
5656
<ItemGroup>
5757
<None Include="..\..\assets\Serilog.snk">

src/Serilog.FullNetFx/Sinks/Trace/DiagnosticTraceSink.cs src/Serilog.FullNetFx/Sinks/DiagnosticTrace/DiagnosticTraceSink.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
// limitations under the License.
1414

1515
using System;
16+
using System.Diagnostics;
1617
using System.IO;
1718
using Serilog.Core;
1819
using Serilog.Events;
1920
using Serilog.Formatting;
2021

21-
namespace Serilog.Sinks.Trace
22+
namespace Serilog.Sinks.DiagnosticTrace
2223
{
2324
class DiagnosticTraceSink : ILogEventSink
2425
{
@@ -35,7 +36,7 @@ public void Emit(LogEvent logEvent)
3536
if (logEvent == null) throw new ArgumentNullException("logEvent");
3637
var sr = new StringWriter();
3738
_textFormatter.Format(logEvent, sr);
38-
System.Diagnostics.Trace.Write(sr.ToString());
39+
Trace.Write(sr.ToString());
3940
}
4041
}
4142
}

src/Serilog.FullNetFx/Sinks/File/FileSink.cs src/Serilog.FullNetFx/Sinks/IOFile/FileSink.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
using Serilog.Events;
1919
using Serilog.Formatting;
2020

21-
namespace Serilog.Sinks.File
21+
namespace Serilog.Sinks.IOFile
2222
{
2323
sealed class FileSink : ILogEventSink, IDisposable
2424
{
@@ -31,7 +31,7 @@ public FileSink(string path, ITextFormatter textFormatter)
3131
if (path == null) throw new ArgumentNullException("path");
3232
if (textFormatter == null) throw new ArgumentNullException("textFormatter");
3333
_textFormatter = textFormatter;
34-
_output = new StreamWriter(System.IO.File.Open(path, FileMode.Append, FileAccess.Write, FileShare.Read));
34+
_output = new StreamWriter(File.Open(path, FileMode.Append, FileAccess.Write, FileShare.Read));
3535
}
3636

3737
public void Emit(LogEvent logEvent)

src/Serilog.FullNetFx/Sinks/RollingFile/RollingFileSink.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
using Serilog.Core;
1717
using Serilog.Events;
1818
using Serilog.Formatting;
19-
using Serilog.Sinks.File;
19+
using Serilog.Sinks.IOFile;
2020

2121
namespace Serilog.Sinks.RollingFile
2222
{

src/Serilog/Configuration/LoggerSinkConfiguration.cs

+20
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
// limitations under the License.
1414

1515
using System;
16+
using System.IO;
1617
using Serilog.Core;
1718
using Serilog.Events;
19+
using Serilog.Formatting.Display;
20+
using Serilog.Sinks.IOTextWriter;
1821

1922
namespace Serilog.Configuration
2023
{
@@ -52,5 +55,22 @@ public LoggerConfiguration Sink(ILogEventSink logEventSink, LogEventLevel restri
5255
_addSink(sink);
5356
return _loggerConfiguration;
5457
}
58+
59+
/// <summary>
60+
/// Write log events to the provided <see cref="TextWriter"/>.
61+
/// </summary>
62+
/// <param name="textWriter">The text writer to write log events to.</param>
63+
/// <param name="outputTemplate">Message template describing the output format.</param>
64+
/// <returns>Configuration object allowing method chaining.</returns>
65+
/// <exception cref="ArgumentNullException"></exception>
66+
public LoggerConfiguration TextWriter(TextWriter textWriter, string outputTemplate = DefaultOutputTemplate)
67+
{
68+
if (textWriter == null) throw new ArgumentNullException("textWriter");
69+
if (outputTemplate == null) throw new ArgumentNullException("outputTemplate");
70+
71+
var formatter = new MessageTemplateTextFormatter(outputTemplate);
72+
_addSink(new TextWriterSink(textWriter, formatter));
73+
return _loggerConfiguration;
74+
}
5575
}
5676
}

src/Serilog/Serilog.csproj

+2-3
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,15 @@
9494
<Compile Include="Parsing\MessageTemplateParser.cs" />
9595
<Compile Include="Parsing\TextToken.cs" />
9696
<Compile Include="Properties\AssemblyInfo.cs" />
97+
<Compile Include="Sinks\IOTextWriter\TextWriterSink.cs" />
9798
</ItemGroup>
9899
<ItemGroup>
99100
<None Include="..\..\assets\Serilog.snk">
100101
<Link>Serilog.snk</Link>
101102
</None>
102103
<None Include="Serilog.nuspec" />
103104
</ItemGroup>
104-
<ItemGroup>
105-
<Folder Include="Sinks\" />
106-
</ItemGroup>
105+
<ItemGroup />
107106
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
108107
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
109108
Other similar extension points exist, see Microsoft.Common.targets.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2013 Nicholas Blumhardt
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
using System.IO;
17+
using Serilog.Core;
18+
using Serilog.Events;
19+
using Serilog.Formatting;
20+
21+
namespace Serilog.Sinks.IOTextWriter
22+
{
23+
class TextWriterSink : ILogEventSink
24+
{
25+
readonly TextWriter _textWriter;
26+
readonly ITextFormatter _textFormatter;
27+
readonly object _syncRoot = new object();
28+
29+
public TextWriterSink(TextWriter textWriter, ITextFormatter textFormatter)
30+
{
31+
if (textFormatter == null) throw new ArgumentNullException("textFormatter");
32+
_textWriter = textWriter;
33+
_textFormatter = textFormatter;
34+
}
35+
36+
public void Emit(LogEvent logEvent)
37+
{
38+
if (logEvent == null) throw new ArgumentNullException("logEvent");
39+
lock (_syncRoot)
40+
{
41+
_textFormatter.Format(logEvent, _textWriter);
42+
}
43+
}
44+
}
45+
}

test/Serilog.Tests/Serilog.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<Compile Include="Properties\AssemblyInfo.cs" />
7373
<Compile Include="Sinks\File\FileSinkTests.cs" />
7474
<Compile Include="Sinks\RollingFile\RollingFileSinkTests.cs" />
75+
<Compile Include="Sinks\TextWriter\TextWriterSinkTests.cs" />
7576
<Compile Include="Support\DelegatingEnricher.cs" />
7677
<Compile Include="Support\DelegatingSink.cs" />
7778
<Compile Include="Support\Extensions.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.IO;
2+
using NUnit.Framework;
3+
using Serilog.Tests.Support;
4+
5+
namespace Serilog.Tests.Sinks.TextWriter
6+
{
7+
[TestFixture]
8+
public class TextWriterSinkTests
9+
{
10+
[Test]
11+
public void EventsAreWrittenToTheTextWriter()
12+
{
13+
var sw = new StringWriter();
14+
15+
var log = new LoggerConfiguration()
16+
.WriteTo.TextWriter(sw)
17+
.CreateLogger();
18+
19+
var mt = Some.String();
20+
log.Information(mt);
21+
22+
var s = sw.ToString();
23+
Assert.That(s.Contains(mt));
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)