Browse files

Merge pull request #75 from jesperll/74

Fix the threading issue in #74
  • Loading branch information...
2 parents e95ce88 + 9ddbec5 commit 9397a23209bee3295f136c687b77099fcd6f8c54 @onovotny committed Feb 3, 2016
Showing with 47 additions and 2 deletions.
  1. +1 −1 MetroLog.Shared/Targets/FileTargetBase.cs
  2. +46 −1 MetroLog.Win81.Tests/FileStreamingTests.cs
View
2 MetroLog.Shared/Targets/FileTargetBase.cs
@@ -102,13 +102,13 @@ protected FileTargetBase(Layout layout)
protected sealed override async Task<LogWriteOperation> WriteAsyncCore(LogWriteContext context, LogEventInfo entry)
{
+ var contents = Layout.GetFormattedString(context, entry);
using (await _lock.LockAsync().ConfigureAwait(false))
{
await EnsureInitialized().ConfigureAwait(false);
await CheckCleanupAsync().ConfigureAwait(false);
var filename = FileNamingParameters.GetFilename(context, entry);
- var contents = Layout.GetFormattedString(context, entry);
var sw = await GetOrCreateStreamWriterForFile(filename).ConfigureAwait(false);
View
47 MetroLog.Win81.Tests/FileStreamingTests.cs
@@ -4,15 +4,17 @@
using System.IO;
using System.Linq;
using System.Text;
+using System.Text.RegularExpressions;
using System.Threading.Tasks;
using MetroLog.Internal;
using MetroLog.Targets;
+using MetroLog.Layouts;
using Windows.Storage;
using Xunit;
namespace MetroLog.NetCore.Tests
{
-
+
public class FileStreamingTargetTests
{
[Fact]
@@ -85,5 +87,48 @@ public class FileStreamingTargetTests
await Task.WhenAll(tasks);
}
+
+
+ [Fact]
+ public async Task TestCurrentManagedThreadIdMatchesCaller()
+ {
+ var loggingConfiguration = new LoggingConfiguration();
+ var target = new TestFileTarget(new CurrentManagedThreadIdLayout());
+ loggingConfiguration.AddTarget(LogLevel.Trace, LogLevel.Fatal, target);
+ LogManagerFactory.DefaultConfiguration = loggingConfiguration;
+ var log = (ILoggerAsync)LogManagerFactory.DefaultLogManager.GetLogger<FileStreamingTargetTests>();
+
+ for (int i = 0; i < 100; i++)
+ {
+ var expectedThreadId = Environment.CurrentManagedThreadId;
+ await log.TraceAsync("What thread am I?");
+ var output = target.Stream.ToArray();
+ target.Stream.Position = 0;
+ var loggedThreadId = int.Parse(Encoding.UTF8.GetString(output, 0, output.Length));
+ Assert.Equal(expectedThreadId, loggedThreadId);
+ }
+ }
+ }
+
+ class CurrentManagedThreadIdLayout : Layout
+ {
+ public override string GetFormattedString(LogWriteContext context, LogEventInfo info)
+ {
+ return Environment.CurrentManagedThreadId.ToString();
+ }
+ }
+
+ class TestFileTarget : StreamingFileTarget
+ {
+ public readonly MemoryStream Stream = new MemoryStream();
+
+ public TestFileTarget(Layout layout) : base(layout)
+ {
+ }
+
+ protected override Task<Stream> GetWritableStreamForFile(string fileName)
+ {
+ return Task.FromResult<Stream>(Stream);
+ }
}
}

0 comments on commit 9397a23

Please sign in to comment.