-
Notifications
You must be signed in to change notification settings - Fork 130
Support AuditTo in configuration #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
61a6603
Add more "end-to-end" tests for ConfigurationSettings (WIP)
3f64b11
Change the default namespace so namespaces match folders
815a796
Support for AuditTo in configuration
e863526
Add tests for different syntaxes of WriteTo
75b2036
Add Tests for static accessors
6e8f73a
Reference Serilog v2.6
190e1a9
Minor version bump -> 2.5.0
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
201 changes: 201 additions & 0 deletions
201
test/Serilog.Settings.Configuration.Tests/ConfigurationSettingsTests.cs
This file contains hidden or 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,201 @@ | ||
using System; | ||
using Microsoft.Extensions.Configuration; | ||
using Serilog.Events; | ||
using Serilog.Settings.Configuration.Tests.Support; | ||
using TestDummies; | ||
using TestDummies.Console; | ||
using TestDummies.Console.Themes; | ||
using Xunit; | ||
|
||
namespace Serilog.Settings.Configuration.Tests | ||
{ | ||
public class ConfigurationSettingsTests | ||
{ | ||
private static LoggerConfiguration ConfigFromJson(string jsonString) | ||
{ | ||
var config = new ConfigurationBuilder().AddJsonString(jsonString).Build(); | ||
return new LoggerConfiguration() | ||
.ReadFrom.Configuration(config); | ||
} | ||
|
||
[Fact] | ||
public void PropertyEnrichmentIsApplied() | ||
{ | ||
LogEvent evt = null; | ||
|
||
var json = @"{ | ||
""Serilog"": { | ||
""Properties"": { | ||
""App"": ""Test"" | ||
} | ||
} | ||
}"; | ||
|
||
var log = ConfigFromJson(json) | ||
.WriteTo.Sink(new DelegatingSink(e => evt = e)) | ||
.CreateLogger(); | ||
|
||
log.Information("Has a test property"); | ||
|
||
Assert.NotNull(evt); | ||
Assert.Equal("Test", evt.Properties["App"].LiteralValue()); | ||
} | ||
|
||
[Theory] | ||
[InlineData("extended syntax", | ||
@"{ | ||
""Serilog"": { | ||
""Using"": [""TestDummies""], | ||
""WriteTo"": [ | ||
{ ""Name"": ""DummyConsole""}, | ||
{ ""Name"": ""DummyWithLevelSwitch""}, | ||
] | ||
} | ||
}")] | ||
[InlineData("simplified syntax", | ||
@"{ | ||
""Serilog"": { | ||
""Using"": [""TestDummies""], | ||
""WriteTo"": [""DummyConsole"", ""DummyWithLevelSwitch"" ] | ||
} | ||
}")] | ||
public void ParameterlessSinksAreConfigured(string syntax, string json) | ||
{ | ||
var log = ConfigFromJson(json) | ||
.CreateLogger(); | ||
|
||
DummyConsoleSink.Emitted.Clear(); | ||
DummyWithLevelSwitchSink.Emitted.Clear(); | ||
|
||
log.Write(Some.InformationEvent()); | ||
|
||
Assert.Equal(1, DummyConsoleSink.Emitted.Count); | ||
Assert.Equal(1, DummyWithLevelSwitchSink.Emitted.Count); | ||
} | ||
|
||
[Fact] | ||
public void SinksAreConfigured() | ||
{ | ||
var json = @"{ | ||
""Serilog"": { | ||
""Using"": [""TestDummies""], | ||
""WriteTo"": [{ | ||
""Name"": ""DummyRollingFile"", | ||
""Args"": {""pathFormat"" : ""C:\\""} | ||
}] | ||
} | ||
}"; | ||
|
||
var log = ConfigFromJson(json) | ||
.CreateLogger(); | ||
|
||
DummyRollingFileSink.Emitted.Clear(); | ||
DummyRollingFileAuditSink.Emitted.Clear(); | ||
|
||
log.Write(Some.InformationEvent()); | ||
|
||
Assert.Equal(1, DummyRollingFileSink.Emitted.Count); | ||
Assert.Equal(0, DummyRollingFileAuditSink.Emitted.Count); | ||
} | ||
|
||
[Fact] | ||
public void AuditSinksAreConfigured() | ||
{ | ||
var json = @"{ | ||
""Serilog"": { | ||
""Using"": [""TestDummies""], | ||
""AuditTo"": [{ | ||
""Name"": ""DummyRollingFile"", | ||
""Args"": {""pathFormat"" : ""C:\\""} | ||
}] | ||
} | ||
}"; | ||
|
||
var log = ConfigFromJson(json) | ||
.CreateLogger(); | ||
|
||
DummyRollingFileSink.Emitted.Clear(); | ||
DummyRollingFileAuditSink.Emitted.Clear(); | ||
|
||
log.Write(Some.InformationEvent()); | ||
|
||
Assert.Equal(0, DummyRollingFileSink.Emitted.Count); | ||
Assert.Equal(1, DummyRollingFileAuditSink.Emitted.Count); | ||
} | ||
|
||
[Fact] | ||
public void TestMinimumLevelOverrides() | ||
{ | ||
var json = @"{ | ||
""Serilog"": { | ||
""MinimumLevel"" : { | ||
""Override"" : { | ||
""System"" : ""Warning"" | ||
} | ||
} | ||
} | ||
}"; | ||
|
||
LogEvent evt = null; | ||
|
||
var log = ConfigFromJson(json) | ||
.WriteTo.Sink(new DelegatingSink(e => evt = e)) | ||
.CreateLogger(); | ||
|
||
var systemLogger = log.ForContext<WeakReference>(); | ||
systemLogger.Write(Some.InformationEvent()); | ||
|
||
Assert.Null(evt); | ||
|
||
systemLogger.Warning("Bad things"); | ||
Assert.NotNull(evt); | ||
|
||
evt = null; | ||
log.Write(Some.InformationEvent()); | ||
Assert.NotNull(evt); | ||
} | ||
|
||
[Fact] | ||
public void SinksWithAbstractParamsAreConfiguredWithTypeName() | ||
{ | ||
var json = @"{ | ||
""Serilog"": { | ||
""Using"": [""TestDummies""], | ||
""WriteTo"": [{ | ||
""Name"": ""DummyConsole"", | ||
""Args"": {""theme"" : ""Serilog.Settings.Configuration.Tests.Support.CustomConsoleTheme, Serilog.Settings.Configuration.Tests""} | ||
}] | ||
} | ||
}"; | ||
|
||
DummyConsoleSink.Theme = null; | ||
|
||
ConfigFromJson(json) | ||
.CreateLogger(); | ||
|
||
Assert.NotNull(DummyConsoleSink.Theme); | ||
Assert.IsType<CustomConsoleTheme>(DummyConsoleSink.Theme); | ||
} | ||
|
||
[Fact] | ||
public void SinksAreConfiguredWithStaticMember() | ||
{ | ||
var json = @"{ | ||
""Serilog"": { | ||
""Using"": [""TestDummies""], | ||
""WriteTo"": [{ | ||
""Name"": ""DummyConsole"", | ||
""Args"": {""theme"" : ""TestDummies.Console.Themes.ConsoleThemes::Theme1, TestDummies""} | ||
}] | ||
} | ||
}"; | ||
|
||
DummyConsoleSink.Theme = null; | ||
|
||
ConfigFromJson(json) | ||
.CreateLogger(); | ||
|
||
Assert.Equal(ConsoleThemes.Theme1, DummyConsoleSink.Theme); | ||
} | ||
} | ||
} |
This file contains hidden or 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
12 changes: 12 additions & 0 deletions
12
test/Serilog.Settings.Configuration.Tests/Support/ConfigurationBuilderExtensions.cs
This file contains hidden or 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,12 @@ | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Serilog.Settings.Configuration.Tests.Support | ||
{ | ||
public static class ConfigurationBuilderExtensions | ||
{ | ||
public static IConfigurationBuilder AddJsonString(this IConfigurationBuilder builder, string json) | ||
{ | ||
return builder.Add(new JsonStringConfigSource(json)); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
test/Serilog.Settings.Configuration.Tests/Support/CustomConsoleTheme.cs
This file contains hidden or 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,8 @@ | ||
using TestDummies.Console.Themes; | ||
|
||
namespace Serilog.Settings.Configuration.Tests.Support | ||
{ | ||
class CustomConsoleTheme : ConsoleTheme | ||
{ | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
test/Serilog.Settings.Configuration.Tests/Support/DelegatingSink.cs
This file contains hidden or 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,34 @@ | ||
using System; | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
|
||
namespace Serilog.Settings.Configuration.Tests.Support | ||
{ | ||
public class DelegatingSink : ILogEventSink | ||
{ | ||
readonly Action<LogEvent> _write; | ||
|
||
public DelegatingSink(Action<LogEvent> write) | ||
{ | ||
if (write == null) throw new ArgumentNullException(nameof(write)); | ||
_write = write; | ||
} | ||
|
||
public void Emit(LogEvent logEvent) | ||
{ | ||
_write(logEvent); | ||
} | ||
|
||
public static LogEvent GetLogEvent(Action<ILogger> writeAction) | ||
{ | ||
LogEvent result = null; | ||
var l = new LoggerConfiguration() | ||
.MinimumLevel.Verbose() | ||
.WriteTo.Sink(new DelegatingSink(le => result = le)) | ||
.CreateLogger(); | ||
|
||
writeAction(l); | ||
return result; | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
test/Serilog.Settings.Configuration.Tests/Support/Extensions.cs
This file contains hidden or 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,12 @@ | ||
using Serilog.Events; | ||
|
||
namespace Serilog.Settings.Configuration.Tests.Support | ||
{ | ||
public static class Extensions | ||
{ | ||
public static object LiteralValue(this LogEventPropertyValue @this) | ||
{ | ||
return ((ScalarValue)@this).Value; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the other methods somehow used
found.Add(GetSurrogateConfigurationMethod<T>
, but I was not sure whether that was needed for Audit ..Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tsimbalar
GetSurrogateConfigurationMethod<T>
is for methods mimicking static methods (extensions) and used just to getMethodInfo
using expressions without reflection, but implementation I did is overcomplicated and just realized you can getMehtodInfo
right out of method group after casting to appropriate delegate type:((Func<LoggerFilterConfiguration, ILogEventFilter, LoggerConfiguration>)With).MethodInfo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh I see. I guess it might not be necessary for
AuditTo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd be needed for
AuditTo.Logger()
.. but this seems like a bit of a fringe case.