Skip to content

Commit e5d1247

Browse files
authored
Merge pull request #77 from tjaart/add_apply_theme_on_output_redirection_option
Add apply theme on output redirection option
2 parents e83f2f3 + 0ca100a commit e5d1247

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

src/Serilog.Sinks.Console/ConsoleLoggerConfigurationExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public static class ConsoleLoggerConfigurationExtensions
4848
/// <param name="standardErrorFromLevel">Specifies the level at which events will be written to standard error.</param>
4949
/// <param name="theme">The theme to apply to the styled output. If not specified,
5050
/// uses <see cref="SystemConsoleTheme.Literate"/>.</param>
51+
/// <param name="applyThemeToRedirectedOutput">Applies the selected or default theme even when output redirection is detected.</param>
5152
/// <returns>Configuration object allowing method chaining.</returns>
5253
public static LoggerConfiguration Console(
5354
this LoggerSinkConfiguration sinkConfiguration,
@@ -56,13 +57,14 @@ public static LoggerConfiguration Console(
5657
IFormatProvider formatProvider = null,
5758
LoggingLevelSwitch levelSwitch = null,
5859
LogEventLevel? standardErrorFromLevel = null,
59-
ConsoleTheme theme = null,
60+
ConsoleTheme theme = null,
61+
bool applyThemeToRedirectedOutput = false,
6062
object syncRoot = null)
6163
{
6264
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
6365
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
6466

65-
var appliedTheme = System.Console.IsOutputRedirected || System.Console.IsErrorRedirected ?
67+
var appliedTheme = !applyThemeToRedirectedOutput && (System.Console.IsOutputRedirected || System.Console.IsErrorRedirected) ?
6668
ConsoleTheme.None :
6769
theme ?? SystemConsoleThemes.Literate;
6870

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using Xunit;
5+
using Serilog.Sinks.SystemConsole.Themes;
6+
7+
namespace Serilog.Sinks.Console.Tests.Configuration
8+
{
9+
public class ConsoleLoggerConfigurationExtensionsTests
10+
{
11+
[Fact]
12+
public void OutputFormattingIsIgnored()
13+
{
14+
using (var stream = new MemoryStream())
15+
{
16+
var sw = new StreamWriter(stream);
17+
18+
System.Console.SetOut(sw);
19+
var config = new LoggerConfiguration()
20+
.WriteTo.Console(theme: AnsiConsoleTheme.Literate,
21+
applyThemeToRedirectedOutput: false);
22+
23+
var logger = config.CreateLogger();
24+
25+
logger.Error("test");
26+
stream.Position = 0;
27+
28+
using (var streamReader = new StreamReader(stream))
29+
{
30+
var result = streamReader.ReadToEnd();
31+
var controlCharacterCount = result.Count(c => Char.IsControl(c) && !Char.IsWhiteSpace(c));
32+
Assert.Equal(0, controlCharacterCount);
33+
}
34+
}
35+
}
36+
37+
[Fact]
38+
public void OutputFormattingIsPresent()
39+
{
40+
using (var stream = new MemoryStream())
41+
{
42+
var sw = new StreamWriter(stream);
43+
44+
System.Console.SetOut(sw);
45+
var config = new LoggerConfiguration()
46+
.WriteTo.Console(theme: AnsiConsoleTheme.Literate,
47+
applyThemeToRedirectedOutput: true);
48+
49+
var logger = config.CreateLogger();
50+
51+
logger.Error("test");
52+
stream.Position = 0;
53+
54+
using (var streamReader = new StreamReader(stream))
55+
{
56+
var result = streamReader.ReadToEnd();
57+
var controlCharacterCount = result.Count(c => Char.IsControl(c) && !Char.IsWhiteSpace(c));
58+
Assert.NotEqual(0, controlCharacterCount);
59+
}
60+
}
61+
}
62+
}
63+
}

test/Serilog.Sinks.Console.Tests/Output/OutputTemplateRendererTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public void DefaultLevelLengthIsFullText()
143143
}
144144

145145
[Fact]
146-
public void AligmentAndWidthCanBeCombined()
146+
public void AlignmentAndWidthCanBeCombined()
147147
{
148148
var formatter = new OutputTemplateRenderer(ConsoleTheme.None, "{Level,5:w3}", CultureInfo.InvariantCulture);
149149
var evt = DelegatingSink.GetLogEvent(l => l.Information("Hello"));

0 commit comments

Comments
 (0)