-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-introduced WriteTo method accepting api key and log id as strings …
…to support configuration through appsettings.json and similar
- Loading branch information
1 parent
2481426
commit 7821736
Showing
3 changed files
with
115 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
test/Serilog.Sinks.ElmahIo.Tests/LoggerConfigurationElmahIoExtensionsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using NUnit.Framework; | ||
using Serilog.Core; | ||
using System; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace Serilog.Sinks.ElmahIo.Tests | ||
{ | ||
public class LoggerConfigurationElmahIoExtensionsTest | ||
{ | ||
[Test] | ||
public void CanConfigureFromOptions() | ||
{ | ||
var log = new LoggerConfiguration() | ||
.WriteTo.ElmahIo(new ElmahIoSinkOptions("apiKey", Guid.NewGuid())) | ||
.CreateLogger(); | ||
|
||
AssertInstalledSink(log); | ||
} | ||
|
||
[Test] | ||
public void CanConfigureFromStringAndGuid() | ||
{ | ||
#pragma warning disable CS0618 // Type or member is obsolete | ||
var log = new LoggerConfiguration() | ||
.WriteTo.ElmahIo("apiKey", Guid.NewGuid().ToString()) | ||
.CreateLogger(); | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
|
||
AssertInstalledSink(log); | ||
} | ||
|
||
[Test] | ||
public void CanConfigureFromAppsettings() | ||
{ | ||
var configuration = new ConfigurationBuilder() | ||
.AddObject(new | ||
{ | ||
Serilog = new | ||
{ | ||
Using = new[] { "Serilog.Sinks.ElmahIo" }, | ||
MinimumLevel = new | ||
{ | ||
Default = "Information" | ||
}, | ||
WriteTo = new[] | ||
{ | ||
new | ||
{ | ||
Name = "ElmahIo", | ||
Args = new | ||
{ | ||
apiKey = "API_KEY", | ||
logId = Guid.NewGuid().ToString(), | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
.Build(); | ||
|
||
var log = new LoggerConfiguration() | ||
.ReadFrom.Configuration(configuration) | ||
.CreateLogger(); | ||
|
||
AssertInstalledSink(log); | ||
} | ||
|
||
private static void AssertInstalledSink(Logger log) | ||
{ | ||
var aggregateSinkFieldInfo = log | ||
.GetType() | ||
.GetField("_sink", BindingFlags.Instance | BindingFlags.NonPublic); | ||
var aggregateSink = (ILogEventSink)aggregateSinkFieldInfo?.GetValue(log); | ||
var sinkEnumerableFieldInfo = aggregateSink? | ||
.GetType() | ||
.GetField("_sinks", BindingFlags.Instance | BindingFlags.NonPublic); | ||
var sinks = (ILogEventSink[])sinkEnumerableFieldInfo?.GetValue(aggregateSink); | ||
Assert.That(sinks.Count, Is.EqualTo(1)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters