Skip to content

Commit

Permalink
Merge pull request serilog#758 from nblumhardt/f-selflog-action
Browse files Browse the repository at this point in the history
Enable(action) for SelfLog
  • Loading branch information
merbla committed May 30, 2016
2 parents e8449cc + 28e87c8 commit 3f6e640
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 8 deletions.
66 changes: 58 additions & 8 deletions src/Serilog/Debugging/SelfLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,63 @@ namespace Serilog.Debugging
/// </summary>
public static class SelfLog
{
static Action<string> _output;

/// <summary>
/// The output mechanism for self-log events.
/// The output mechanism for self-log messages.
/// </summary>
/// <example>
/// SelfLog.Out = Console.Error;
/// </example>
// ReSharper disable once MemberCanBePrivate.Global, UnusedAutoPropertyAccessor.Global
public static TextWriter Out { get; set; }
[Obsolete("Use SelfLog.Enable(value) and SelfLog.Disable() instead")]
public static TextWriter Out
{
set
{
if (value != null)
Enable(value);
else
Disable();
}
}

/// <summary>
/// Set the output mechanism for self-log messages.
/// </summary>
/// <param name="output">A synchronized <see cref="TextWriter"/> to which
/// self-log messages 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 messages.
/// </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>
/// Write a message to the self-log.
Expand All @@ -39,14 +88,15 @@ public static class SelfLog
/// <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 = Out;
if (o != null)
{
o.WriteLine(DateTime.Now.ToString("s") + " " + format, arg0, arg1, arg2);
o.Flush();
}
var o = _output;

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 3f6e640

Please sign in to comment.