Skip to content

Commit

Permalink
Moved AppSettings support to Serilog.Settings.AppSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
nblumhardt committed Feb 18, 2016
1 parent 505f6c8 commit e87b999
Show file tree
Hide file tree
Showing 29 changed files with 299 additions and 283 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2013-2016 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using Serilog.Configuration;
using Serilog.Settings.AppSettings;

namespace Serilog
{
/// <summary>
/// Extends <see cref="LoggerConfiguration"/> with support for System.Configuration appSettings elements.
/// </summary>
public static class AppSettingsLoggerConfigurationExtensions
{
/// <summary>
/// Reads the &lt;appSettings&gt; 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. If specified the value
/// will be prepended to the setting keys and followed by :, for example "myapp" will use "myapp:serilog:minumum-level. If null
/// no prefix is applied.</param>
/// <returns>An object allowing configuration to continue.</returns>
public static LoggerConfiguration AppSettings(
this LoggerSettingsConfiguration settingConfiguration, string settingPrefix = null)
{
if (settingConfiguration == null) throw new ArgumentNullException(nameof(settingConfiguration));
if (settingPrefix != null)
{
if (settingPrefix.Contains(":")) throw new ArgumentException("Custom setting prefixes cannot contain the colon (:) character.");
if (settingPrefix == "serilog") throw new ArgumentException("The value \"serilog\" is not a permitted setting prefix. To use the default, do not specify a custom prefix at all.");
if (string.IsNullOrWhiteSpace(settingPrefix)) throw new ArgumentException("To use the default setting prefix, do not supply the settingPrefix parameter, instead pass the default null.");
}

return settingConfiguration.Settings(new AppSettingsSettings(settingPrefix));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>0d9f37e3-2cb4-4c0d-a307-32bb71defdd4</ProjectGuid>
<RootNamespace>Serilog</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<ProduceOutputsOnBuild>True</ProduceOutputsOnBuild>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if APPSETTINGS
using System;
using System.Configuration;
using System.Linq;
Expand Down Expand Up @@ -40,9 +39,7 @@ public void Configure(LoggerConfiguration loggerConfiguration)
.Where(k => k.StartsWith(_settingPrefix))
.ToDictionary(k => k.Substring(_settingPrefix.Length), k => Environment.ExpandEnvironmentVariables(settings[k]));

var keyValuePairSettings = new KeyValuePairSettings(pairs);
keyValuePairSettings.Configure(loggerConfiguration);
loggerConfiguration.ReadFrom.KeyValuePairs(pairs);
}
}
}
#endif
22 changes: 22 additions & 0 deletions src/Serilog.Settings.AppSettings/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"version": "2.0.0-beta-*",
"description": "XML configuration (System.Configuration <appSettings>) support for Serilog.",
"authors": [ "Serilog Contributors" ],
"tags": [ "serilog", "xml" ],
"projectUrl": "http://serilog.net",
"licenseUrl": "http://www.apache.org/licenses/LICENSE-2.0",
"iconUrl": "http://serilog.net/images/serilog-configuration-nuget.png",
"dependencies": {
"Serilog": { "target": "project" }
},
"compilationOptions": {
"keyFile": "../../assets/Serilog.snk"
},
"frameworks": {
"net45": {
"frameworkAssemblies": {
"System.Configuration": ""
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,7 @@ public void Emit(LogEvent logEvent)
{
if (logEvent == null) throw new ArgumentNullException(nameof(logEvent));

#if NET40
Thread.MemoryBarrier();
#else
Interlocked.MemoryBarrier();
#endif

IList<Exception> exceptions = null;

Expand Down
11 changes: 11 additions & 0 deletions src/Serilog.Sinks.PeriodicBatching/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Reflection;
using System.Runtime.CompilerServices;

[assembly: AssemblyVersion("2.0.0.0")]

[assembly: InternalsVisibleTo("Serilog.Tests, PublicKey=" +
"0024000004800000940000000602000000240000525341310004000001000100fb8d13fd344a1c" +
"6fe0fe83ef33c1080bf30690765bc6eb0df26ebfdf8f21670c64265b30db09f73a0dea5b3db4c9" +
"d18dbf6d5a25af5ce9016f281014d79dc3b4201ac646c451830fc7e61a2dfd633d34c39f87b818" +
"94191652df5ac63cc40c77f3542f702bda692e6e8a9158353df189007a49da0f3cfd55eb250066" +
"b19485ec")]
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public abstract class PeriodicBatchingSink : ILogEventSink, IDisposable
readonly Queue<LogEvent> _waitingBatch = new Queue<LogEvent>();

readonly object _stateLock = new object();
#if NO_TIMER
readonly PortableTimer _timer;
#else
#if WAITABLE_TIMER
readonly Timer _timer;
#else
readonly PortableTimer _timer;
#endif
bool _unloading;
bool _started;
Expand All @@ -61,20 +61,20 @@ protected PeriodicBatchingSink(int batchSizeLimit, TimeSpan period)
_queue = new ConcurrentQueue<LogEvent>();
_status = new BatchedConnectionStatus(period);

#if NO_TIMER
_timer = new PortableTimer(cancel => OnTick());
#else
#if WAITABLE_TIMER
_timer = new Timer(s => OnTick(), null, -1, -1);
#else
_timer = new PortableTimer(cancel => OnTick());
#endif

#if !NO_APPDOMAIN
#if APPDOMAIN
AppDomain.CurrentDomain.DomainUnload += OnAppDomainUnloading;
AppDomain.CurrentDomain.ProcessExit += OnAppDomainUnloading;
AppDomain.CurrentDomain.UnhandledException += OnAppDomainUnloading;
#endif
}

#if !NO_APPDOMAIN
#if APPDOMAIN
void OnAppDomainUnloading(object sender, EventArgs args)
{
var eventArgs = args as UnhandledExceptionEventArgs;
Expand All @@ -95,18 +95,18 @@ void CloseAndFlush()
_unloading = true;
}

#if !NO_APPDOMAIN
#if APPDOMAIN
AppDomain.CurrentDomain.DomainUnload -= OnAppDomainUnloading;
AppDomain.CurrentDomain.ProcessExit -= OnAppDomainUnloading;
AppDomain.CurrentDomain.UnhandledException -= OnAppDomainUnloading;
#endif

#if NO_TIMER
_timer.Dispose();
#else
#if WAITABLE_TIMER
var wh = new ManualResetEvent(false);
if (_timer.Dispose(wh))
wh.WaitOne();
#else
_timer.Dispose();
#endif

OnTick();
Expand Down Expand Up @@ -221,16 +221,12 @@ void OnTick()

void SetTimer(TimeSpan interval)
{
#if NO_TIMER
_timer.Start(interval);
#if WAITABLE_TIMER
_timer.Change(interval, Timeout.InfiniteTimeSpan);
#else
_timer.Change(interval,
#if NET40
TimeSpan.FromMilliseconds(-1));
#else
Timeout.InfiniteTimeSpan);
#endif
_timer.Start(interval);
#endif

}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if NO_TIMER
#if !WAITABLE_TIMER

using Serilog.Debugging;
using System;
Expand Down
13 changes: 5 additions & 8 deletions src/Serilog.Sinks.PeriodicBatching/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,26 @@
"version": "2.0.0-beta-*",
"description": "The periodic batching sink for Serilog",
"authors": [ "Serilog Contributors" ],
"tags": [ "serilog", "batching", "timer"],
"tags": [ "serilog", "batching", "timer" ],
"projectUrl": "http://serilog.net",
"licenseUrl": "http://www.apache.org/licenses/LICENSE-2.0",
"iconUrl": "http://serilog.net/images/serilog-sink-nuget.png",
"dependencies": {
"Serilog": { "target": "project" },
"Serilog": { "target": "project" },
},
"compilationOptions": {
"keyFile": "../../assets/Serilog.snk"
},
"frameworks": {
"net45": {
"frameworkAssemblies": {
"System.Configuration": ""
"compilationOptions": {
"define": [ "APPDOMAIN", "WAITABLE_TIMER" ]
}
},
"dotnet5.2": {
"compilationOptions": {
"define": [ "NO_TIMER", "NO_APPDOMAIN" ]
},
"dependencies": {
"System.Collections.Concurrent": "4.0.11-beta-23516"
}
}
}
}
}
11 changes: 11 additions & 0 deletions src/Serilog.Sinks.RollingFile/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Reflection;
using System.Runtime.CompilerServices;

[assembly: AssemblyVersion("2.0.0.0")]

[assembly: InternalsVisibleTo("Serilog.Tests, PublicKey=" +
"0024000004800000940000000602000000240000525341310004000001000100fb8d13fd344a1c" +
"6fe0fe83ef33c1080bf30690765bc6eb0df26ebfdf8f21670c64265b30db09f73a0dea5b3db4c9" +
"d18dbf6d5a25af5ce9016f281014d79dc3b4201ac646c451830fc7e61a2dfd633d34c39f87b818" +
"94191652df5ac63cc40c77f3542f702bda692e6e8a9158353df189007a49da0f3cfd55eb250066" +
"b19485ec")]
11 changes: 11 additions & 0 deletions src/Serilog.Sinks.TextWriter/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Reflection;
using System.Runtime.CompilerServices;

[assembly: AssemblyVersion("2.0.0.0")]

[assembly: InternalsVisibleTo("Serilog.Tests, PublicKey=" +
"0024000004800000940000000602000000240000525341310004000001000100fb8d13fd344a1c" +
"6fe0fe83ef33c1080bf30690765bc6eb0df26ebfdf8f21670c64265b30db09f73a0dea5b3db4c9" +
"d18dbf6d5a25af5ce9016f281014d79dc3b4201ac646c451830fc7e61a2dfd633d34c39f87b818" +
"94191652df5ac63cc40c77f3542f702bda692e6e8a9158353df189007a49da0f3cfd55eb250066" +
"b19485ec")]
11 changes: 11 additions & 0 deletions src/Serilog.Sinks.Trace/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Reflection;
using System.Runtime.CompilerServices;

[assembly: AssemblyVersion("2.0.0.0")]

[assembly: InternalsVisibleTo("Serilog.Tests, PublicKey=" +
"0024000004800000940000000602000000240000525341310004000001000100fb8d13fd344a1c" +
"6fe0fe83ef33c1080bf30690765bc6eb0df26ebfdf8f21670c64265b30db09f73a0dea5b3db4c9" +
"d18dbf6d5a25af5ce9016f281014d79dc3b4201ac646c451830fc7e61a2dfd633d34c39f87b818" +
"94191652df5ac63cc40c77f3542f702bda692e6e8a9158353df189007a49da0f3cfd55eb250066" +
"b19485ec")]
34 changes: 0 additions & 34 deletions src/Serilog/LoggerConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@
using System.Diagnostics;
#endif


#if APPSETTINGS
using Serilog.Settings.AppSettings;
#endif

namespace Serilog
{
/// <summary>
Expand Down Expand Up @@ -105,34 +100,5 @@ public static LoggerConfiguration WithEnvironmentUserName(
return enrichmentConfiguration.With<EnvironmentUserNameEnricher>();
}
#endif

#if APPSETTINGS
/// <summary>
/// Reads the &lt;appSettings&gt; 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. If specified the value
/// will be prepended to the setting keys and followed by :, for example "myapp" will use "myapp:serilog:minumum-level. If null
/// no prefix is applied.</param>
/// <returns>An object allowing configuration to continue.</returns>
public static LoggerConfiguration AppSettings(
this LoggerSettingsConfiguration settingConfiguration, string settingPrefix = null)
{
if (settingConfiguration == null) throw new ArgumentNullException(nameof(settingConfiguration));
if (settingPrefix != null)
{
if (settingPrefix.Contains(":")) throw new ArgumentException("Custom setting prefixes cannot contain the colon (:) character.");
if (settingPrefix == "serilog") throw new ArgumentException("The value \"serilog\" is not a permitted setting prefix. To use the default, do not specify a custom prefix at all.");
if (string.IsNullOrWhiteSpace(settingPrefix)) throw new ArgumentException("To use the default setting prefix, do not supply the settingPrefix parameter, instead pass the default null.");
}

return settingConfiguration.Settings(new AppSettingsSettings(settingPrefix));
}
#endif
}
}
11 changes: 4 additions & 7 deletions src/Serilog/Parsing/MessageTemplateToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,9 @@
// limitations under the License.

using System;
using System.Collections.Generic;
using System.IO;

#if NET40
using IPropertyDictionary = System.Collections.Generic.IDictionary<string, Serilog.Events.LogEventPropertyValue>;
#else
using IPropertyDictionary = System.Collections.Generic.IReadOnlyDictionary<string, Serilog.Events.LogEventPropertyValue>;
#endif
using Serilog.Events;

namespace Serilog.Parsing
{
Expand All @@ -40,6 +36,7 @@ protected MessageTemplateToken(int startIndex)
/// <summary>
/// The token's start index in the template.
/// </summary>
// ReSharper disable once UnusedAutoPropertyAccessor.Global
public int StartIndex { get; }

/// <summary>
Expand All @@ -53,6 +50,6 @@ protected MessageTemplateToken(int startIndex)
/// <param name="properties">Properties that may be represented by the token.</param>
/// <param name="output">Output for the rendered string.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
public abstract void Render(IPropertyDictionary properties, TextWriter output, IFormatProvider formatProvider = null);
public abstract void Render(IReadOnlyDictionary<string, LogEventPropertyValue> properties, TextWriter output, IFormatProvider formatProvider = null);
}
}
Loading

0 comments on commit e87b999

Please sign in to comment.