Skip to content

Commit

Permalink
BeginScope should return IDisposable where ToString returns ToString …
Browse files Browse the repository at this point in the history
…of input-state
  • Loading branch information
snakefoot committed Jul 27, 2018
1 parent 22234ea commit 8d4b7a3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ public void Dispose()
}
}
}

public override string ToString()
{
return (_properties?.Count > 0 ? _properties[_properties.Count - 1]?.ToString() : null) ?? base.ToString();
}
}
}
}
8 changes: 5 additions & 3 deletions test/CustomBeginScopeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ public void TestNonSerializableSayHi()
var runner = GetRunner<CustomBeginScopeTestRunner>();
var target = new NLog.Targets.MemoryTarget() { Layout = "${message} ${mdlc:World}. Welcome ${ndlc}" };
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target);
runner.SayHi().Wait();
var scopeString = runner.SayHi().Result;
Assert.Single(target.Logs);
Assert.Equal("Hi Earth. Welcome Earth People", target.Logs[0]);
Assert.Equal("Earth People", scopeString);
}

public class CustomBeginScopeTestRunner
Expand All @@ -60,12 +61,13 @@ public async Task SayHello()
}
}

public async Task SayHi()
public async Task<string> SayHi()
{
using (_logger.BeginScope("{World} People", "Earth"))
using (var scopeString = _logger.BeginScope("{World} People", "Earth"))
{
await Task.Yield();
_logger.LogInformation("Hi");
return scopeString.ToString();
}
}
}
Expand Down

0 comments on commit 8d4b7a3

Please sign in to comment.