Skip to content

Commit

Permalink
Fixes serilog#731 - evented SelfLog
Browse files Browse the repository at this point in the history
  • Loading branch information
nblumhardt committed May 30, 2016
1 parent 834eae6 commit 58b9064
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 18 deletions.
63 changes: 45 additions & 18 deletions src/Serilog/Debugging/SelfLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Serilog.Debugging
/// </summary>
public static class SelfLog
{
static readonly Action<string> _output;
static Action<string> _output;

/// <summary>
/// The output mechanism for self-log events.
Expand All @@ -32,28 +32,53 @@ public static class SelfLog
/// SelfLog.Out = Console.Error;
/// </example>
// ReSharper disable once MemberCanBePrivate.Global, UnusedAutoPropertyAccessor.Global
[Obsolete("Use SetOutput() instead")]
[Obsolete("Use SelfLog.Enable(value) and SelfLog.Disable() instead")]
public static TextWriter Out
{
set
{
if (value == null)
{
SetOutput(null);
}
if (value != null)
Enable(value);
else
{
SetOutput(m =>
{
value.WriteLine(DateTime.UtcNow.ToString("o") + " " + m);
value.Flush();
});
}
Disable();
}
}

public static void SetOutput(Action<string> output)
/// <summary>
/// Set the output mechanism for self-log events.
/// </summary>
/// <param name="output">A synchronized <see cref="TextWriter"/> to which
/// self-log events will be written.</param>
// ReSharper disable once MemberCanBePrivate.Global
public static void Enable(TextWriter output)
{
if (output == null) throw new ArgumentNullException(nameof(output));

Enable(m =>
{
output.WriteLine(m);
output.Flush();
});
}

/// <summary>
/// Set the output mechanism for self-log events.
/// </summary>
/// <param name="output">An action to invoke with self-log messages.</param>
/// // ReSharper disable once MemberCanBePrivate.Global
public static void Enable(Action<string> output)
{
if (output == null) throw new ArgumentNullException(nameof(output));
_output = output;
}

/// <summary>
/// Clear the output mechanism and disable self-log events.
/// </summary>
/// // ReSharper disable once MemberCanBePrivate.Global
public static void Disable()
{
_output = null;
}

/// <summary>
Expand All @@ -63,13 +88,15 @@ public static void SetOutput(Action<string> output)
/// <param name="arg0">First argument, if supplied.</param>
/// <param name="arg1">Second argument, if supplied.</param>
/// <param name="arg2">Third argument, if supplied.</param>
/// <remarks>
/// The name is historical; because this is used from third-party sink packages, removing the "Line"
/// suffix as would seem sensible isn't worth the breakage.
/// </remarks>
public static void WriteLine(string format, object arg0 = null, object arg1 = null, object arg2 = null)
{
var o = _output;
if (o != null)
{
o(string.Format(format, arg0, arg1, arg2));
}

o?.Invoke(string.Format(DateTime.UtcNow.ToString("o") + " " + format, arg0, arg1, arg2));
}
}
}
35 changes: 35 additions & 0 deletions test/Serilog.Tests/Debugging/SelfLogTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Serilog.Debugging;
using Xunit;

namespace Serilog.Tests.Debugging
{
public class SelfLogTests
{
[ThreadStatic]
static List<string> Messages;

[Fact]
public void MessagesAreWrittenWhenOutputIsSet()
{
Messages = new List<string>();
SelfLog.Enable(m =>
{
Messages = Messages ?? new List<string>();
Messages.Add(m);
});

SelfLog.WriteLine("Hello {0} {1} {2}", 0, 1, 2);
Assert.True(Messages.Any(m => m.EndsWith("Hello 0 1 2")));

// Better to do this here than in another test, since at this point
// we've confirmed there's actually something to disable.
var count = Messages.Count;
SelfLog.Disable();
SelfLog.WriteLine("Unwritten");
Assert.Equal(Messages.Count, count);
}
}
}

0 comments on commit 58b9064

Please sign in to comment.