diff --git a/sample/Sample/Program.cs b/sample/Sample/Program.cs index 562e427..d3441da 100644 --- a/sample/Sample/Program.cs +++ b/sample/Sample/Program.cs @@ -1,12 +1,15 @@ using System; -using Microsoft.Extensions.Configuration; -using Serilog; using System.IO; using System.Linq; +using System.Collections.Generic; +using System.Threading; + +using Microsoft.Extensions.Configuration; + +using Serilog; using Serilog.Core; using Serilog.Events; -using System.Collections.Generic; namespace Sample { @@ -14,6 +17,8 @@ public class Program { public static void Main(string[] args) { + Thread.CurrentThread.Name = "Main thread"; + var configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile(path: "appsettings.json", optional: false, reloadOnChange: true) @@ -47,7 +52,7 @@ public static void Main(string[] args) Console.WriteLine("\nPress \"q\" to quit, or any other key to run again.\n"); } - while(!args.Contains("--run-once") && (Console.ReadKey().KeyChar != 'q')); + while (!args.Contains("--run-once") && (Console.ReadKey().KeyChar != 'q')); } } @@ -73,7 +78,7 @@ public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyV { result = null; - if(value is LoginData) + if (value is LoginData) { result = new StructureValue( new List diff --git a/sample/Sample/Sample.csproj b/sample/Sample/Sample.csproj index 1b69583..6747952 100644 --- a/sample/Sample/Sample.csproj +++ b/sample/Sample/Sample.csproj @@ -22,12 +22,12 @@ - + - - - - + + + + diff --git a/sample/Sample/appsettings.json b/sample/Sample/appsettings.json index 2771a7f..01ffc67 100644 --- a/sample/Sample/appsettings.json +++ b/sample/Sample/appsettings.json @@ -36,13 +36,44 @@ "Name": "File", "Args": { "path": "%TEMP%\\Logs\\serilog-configuration-sample.txt", - "outputTemplate": "{Timestamp:o} [{Level:u3}] ({Application}/{MachineName}/{ThreadId}) {Message}{NewLine}{Exception}" + "outputTemplate": "{Timestamp:o} [{Level:u3}] ({Application}/{MachineName}/{ThreadId}/{ThreadName}) {Message}{NewLine}{Exception}" } } ] } }, - "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ], + "WriteTo:ConditionalSink": { + "Name": "Conditional", + "Args": { + "expression": "@Level in ['Error', 'Fatal']", + "configureSink": [ + { + "Name": "File", + "Args": { + "path": "%TEMP%\\Logs\\serilog-configuration-sample-errors.txt" + } + } + ] + } + }, + "Enrich": [ + "FromLogContext", + "WithThreadId", + { + "Name": "AtLevel", + "Args": { + "enrichFromLevel": "Error", + "configureEnricher": [ "WithThreadName" ] + } + }, + { + "Name": "When", + "Args": { + "expression": "Application = 'Sample'", + "configureEnricher": [ "WithMachineName" ] + } + } + ], "Properties": { "Application": "Sample" }, diff --git a/src/Serilog.Settings.Configuration/Serilog.Settings.Configuration.csproj b/src/Serilog.Settings.Configuration/Serilog.Settings.Configuration.csproj index 5685038..02ef562 100644 --- a/src/Serilog.Settings.Configuration/Serilog.Settings.Configuration.csproj +++ b/src/Serilog.Settings.Configuration/Serilog.Settings.Configuration.csproj @@ -2,7 +2,7 @@ Microsoft.Extensions.Configuration (appsettings.json) support for Serilog. - 3.1.1 + 3.2.0 latest Serilog Contributors netstandard2.0;net451;net461 @@ -31,7 +31,7 @@ - + diff --git a/src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReader.cs b/src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReader.cs index 4a5aa71..4d44c32 100644 --- a/src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReader.cs +++ b/src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReader.cs @@ -185,6 +185,12 @@ void IConfigurationReader.ApplySinks(LoggerSinkConfiguration loggerSinkConfigura CallConfigurationMethods(methodCalls, FindSinkConfigurationMethods(_configurationAssemblies), loggerSinkConfiguration); } + void IConfigurationReader.ApplyEnrichment(LoggerEnrichmentConfiguration loggerEnrichmentConfiguration) + { + var methodCalls = GetMethodCalls(_section); + CallConfigurationMethods(methodCalls, FindEventEnricherConfigurationMethods(_configurationAssemblies), loggerEnrichmentConfiguration); + } + void ApplyEnrichment(LoggerConfiguration loggerConfiguration) { var enrichDirective = _section.GetSection("Enrich"); diff --git a/src/Serilog.Settings.Configuration/Settings/Configuration/IConfigurationReader.cs b/src/Serilog.Settings.Configuration/Settings/Configuration/IConfigurationReader.cs index f19f142..af815af 100644 --- a/src/Serilog.Settings.Configuration/Settings/Configuration/IConfigurationReader.cs +++ b/src/Serilog.Settings.Configuration/Settings/Configuration/IConfigurationReader.cs @@ -1,9 +1,10 @@ -using Serilog.Configuration; +using Serilog.Configuration; namespace Serilog.Settings.Configuration { interface IConfigurationReader : ILoggerSettings { void ApplySinks(LoggerSinkConfiguration loggerSinkConfiguration); + void ApplyEnrichment(LoggerEnrichmentConfiguration loggerEnrichmentConfiguration); } } diff --git a/src/Serilog.Settings.Configuration/Settings/Configuration/ObjectArgumentValue.cs b/src/Serilog.Settings.Configuration/Settings/Configuration/ObjectArgumentValue.cs index e1730d2..83f7443 100644 --- a/src/Serilog.Settings.Configuration/Settings/Configuration/ObjectArgumentValue.cs +++ b/src/Serilog.Settings.Configuration/Settings/Configuration/ObjectArgumentValue.cs @@ -1,10 +1,12 @@ -using Microsoft.Extensions.Configuration; -using Serilog.Configuration; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; +using Microsoft.Extensions.Configuration; + +using Serilog.Configuration; + namespace Serilog.Settings.Configuration { class ObjectArgumentValue : IConfigurationArgumentValue @@ -31,20 +33,15 @@ public object ConvertTo(Type toType, ResolutionContext resolutionContext) typeInfo.GetGenericTypeDefinition() is Type genericType && genericType == typeof(Action<>)) { var configType = typeInfo.GenericTypeArguments[0]; - if (configType != typeof(LoggerConfiguration) && configType != typeof(LoggerSinkConfiguration)) - throw new ArgumentException($"Configuration for Action<{configType}> is not implemented."); - IConfigurationReader configReader = new ConfigurationReader(_section, _configurationAssemblies, resolutionContext); - if (configType == typeof(LoggerConfiguration)) + return configType switch { - return new Action(configReader.Configure); - } - - if (configType == typeof(LoggerSinkConfiguration)) - { - return new Action(loggerSinkConfig => configReader.ApplySinks(loggerSinkConfig)); - } + _ when configType == typeof(LoggerConfiguration) => new Action(configReader.Configure), + _ when configType == typeof(LoggerSinkConfiguration) => new Action(configReader.ApplySinks), + _ when configType == typeof(LoggerEnrichmentConfiguration) => new Action(configReader.ApplyEnrichment), + _ => throw new ArgumentException($"Configuration resolution for Action<{configType.Name}> parameter type at the path {_section.Path} is not implemented.") + }; } if (toType.IsArray) @@ -97,7 +94,7 @@ bool TryCreateContainer(out object result) } } - private static bool IsContainer(Type type, out Type elementType) + static bool IsContainer(Type type, out Type elementType) { elementType = null; foreach (var iface in type.GetInterfaces()) diff --git a/src/Serilog.Settings.Configuration/Settings/Configuration/SurrogateConfigurationMethods.cs b/src/Serilog.Settings.Configuration/Settings/Configuration/SurrogateConfigurationMethods.cs index 36290a7..4b460b3 100644 --- a/src/Serilog.Settings.Configuration/Settings/Configuration/SurrogateConfigurationMethods.cs +++ b/src/Serilog.Settings.Configuration/Settings/Configuration/SurrogateConfigurationMethods.cs @@ -108,6 +108,14 @@ static LoggerConfiguration With( ILogEventEnricher enricher) => loggerEnrichmentConfiguration.With(enricher); + static LoggerConfiguration AtLevel( + LoggerEnrichmentConfiguration loggerEnrichmentConfiguration, + Action configureEnricher, + LogEventLevel enrichFromLevel = LevelAlias.Minimum, + LoggingLevelSwitch levelSwitch = null) + => levelSwitch != null ? loggerEnrichmentConfiguration.AtLevel(levelSwitch, configureEnricher) + : loggerEnrichmentConfiguration.AtLevel(enrichFromLevel, configureEnricher); + static LoggerConfiguration FromLogContext(LoggerEnrichmentConfiguration loggerEnrichmentConfiguration) => loggerEnrichmentConfiguration.FromLogContext();