Created
October 27, 2015 01:23
-
-
Save augustoproiete/95144d6252aaed477c15 to your computer and use it in GitHub Desktop.
Example of using custom prefixes with Serilog when using AppSettings
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
<?xml version="1.0" encoding="utf-8" ?> | |
<configuration> | |
<appSettings> | |
<add key="custom1:serilog:minimum-level" value="Warning" /> | |
<add key="custom2:serilog:minimum-level" value="Error" /> | |
</appSettings> | |
</configuration> |
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
using System; | |
using System.Configuration; | |
using System.Linq; | |
using Serilog; | |
using Serilog.Configuration; | |
namespace Serilog | |
{ | |
internal class AppSettingsSettings : ILoggerSettings | |
{ | |
readonly string SettingPrefix; | |
public AppSettingsSettings(string settingPrefix) | |
{ | |
SettingPrefix = string.Format("{0}:", settingPrefix); | |
} | |
public void Configure(LoggerConfiguration loggerConfiguration) | |
{ | |
if (loggerConfiguration == null) | |
{ | |
throw new ArgumentNullException("loggerConfiguration"); | |
} | |
var settings = ConfigurationManager.AppSettings; | |
var pairs = settings.AllKeys | |
.Where(k => k.StartsWith(SettingPrefix)) | |
.ToDictionary(k => k.Substring(SettingPrefix.Length), k => Environment.ExpandEnvironmentVariables(settings[k])); | |
// Add the FullNetFx assembly by default so that all built-in Serilog sinks are available without "using" | |
pairs.Add("using:_ImpliedSerilogFullNetFx", typeof(AppSettingsSettings).Assembly.FullName); | |
loggerConfiguration.ReadFrom.KeyValuePairs(pairs); | |
} | |
} | |
} |
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
using System; | |
using Serilog; | |
using Serilog.Configuration; | |
namespace Serilog | |
{ | |
public static class SerilogExtensions | |
{ | |
/// <summary> | |
/// Reads the <appSettings> element of App.config or Web.config, searching for for keys | |
/// that look like: <code>serilog:*</code>, which are used to configure | |
/// the logger. To add a sink, use a key like <code>serilog:write-to:File.path</code> for | |
/// each parameter to the sink's configuration method. To add an additional assembly | |
/// containing sinks, use <code>serilog:using</code>. To set the level use | |
/// <code>serilog:minimum-level</code>. | |
/// </summary> | |
/// <param name="settingConfiguration">Logger setting configuration</param> | |
/// <param name="settingPrefix">Prefix to use when reading keys in appSettings</param> | |
/// <returns>An object allowing configuration to continue.</returns> | |
public static LoggerConfiguration AppSettingsWithPrefix( | |
this LoggerSettingsConfiguration settingConfiguration, string settingPrefix = "serilog") | |
{ | |
if (settingConfiguration == null) | |
{ | |
throw new ArgumentNullException("settingConfiguration"); | |
} | |
return settingConfiguration.Settings(new AppSettingsSettings(settingPrefix)); | |
} | |
} | |
} |
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
using System.Configuration; | |
using NUnit.Framework; | |
using Serilog; | |
using Serilog.Events; | |
namespace Serilog.UnitTests | |
{ | |
[TestFixture] | |
public class SerilogExtensionsTests | |
{ | |
[Test] | |
public void CanUseCustomPrefixToConfigureSettings() | |
{ | |
const string prefix1 = "custom1:serilog"; | |
const string prefix2 = "custom2:serilog"; | |
// Make sure we have the expected keys in the App.config | |
Assert.AreEqual("Warning", ConfigurationManager.AppSettings[prefix1 + ":minimum-level"]); | |
Assert.AreEqual("Error", ConfigurationManager.AppSettings[prefix2 + ":minimum-level"]); | |
var log1 = new LoggerConfiguration() | |
.ReadFrom.AppSettingsWithPrefix(prefix1) | |
.CreateLogger(); | |
var log2 = new LoggerConfiguration() | |
.ReadFrom.AppSettingsWithPrefix(prefix2) | |
.CreateLogger(); | |
Assert.IsFalse(log1.IsEnabled(LogEventLevel.Information)); | |
Assert.IsTrue(log1.IsEnabled(LogEventLevel.Warning)); | |
Assert.IsFalse(log2.IsEnabled(LogEventLevel.Warning)); | |
Assert.IsTrue(log2.IsEnabled(LogEventLevel.Error)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment