/// <summary> /// Reads logger settings from the provided configuration object using the provided section name. Generally this /// is preferable over the other method that takes a configuration section. Only this version will populate /// IConfiguration parameters on target methods. /// </summary> /// <param name="settingConfiguration">Logger setting configuration.</param> /// <param name="configuration">A configuration object which contains a Serilog section.</param> /// <param name="sectionName">A section name for section which contains a Serilog section.</param> /// <param name="dependencyContext">The dependency context from which sink/enricher packages can be located. If not supplied, the platform /// default will be used.</param> /// <returns>An object allowing configuration to continue.</returns> public static LoggerConfiguration Configuration( this LoggerSettingsConfiguration settingConfiguration, IConfiguration configuration, string sectionName, DependencyContext dependencyContext = null) { if (settingConfiguration == null) { throw new ArgumentNullException(nameof(settingConfiguration)); } if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if (sectionName == null) { throw new ArgumentNullException(nameof(sectionName)); } var assemblyFinder = dependencyContext == null ? AssemblyFinder.Auto() : AssemblyFinder.ForDependencyContext(dependencyContext); return(settingConfiguration.Settings( new ConfigurationReader( configuration.GetSection(sectionName), assemblyFinder, configuration))); }
/// <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. 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))); }
/// <summary> /// Reads logger settings from the provided configuration section. /// </summary> /// <param name="settingConfiguration">Logger setting configuration.</param> /// <param name="configuration">The Serilog configuration section</param> /// <param name="dependencyContext">The dependency context from which sink/enricher packages can be located. If not supplied, the platform /// default will be used.</param> /// <returns>An object allowing configuration to continue.</returns> public static LoggerConfiguration ConfigurationSection( this LoggerSettingsConfiguration settingConfiguration, IConfigurationSection configuration, DependencyContext dependencyContext = null) { if (settingConfiguration == null) { throw new ArgumentNullException(nameof(settingConfiguration)); } if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } var context = dependencyContext; if (context == null) { context = DependencyContext.Default; if (context == null) { throw new InvalidOperationException("Add `\"preserveCompilationContext\": true` to `\"buildOptions\"` for support on .NET 4.x."); } } return(settingConfiguration.Settings(new ConfigurationReader(configuration, context))); }
/// <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> /// <returns>An object allowing configuration to continue.</returns> public static LoggerConfiguration AppSettings( this LoggerSettingsConfiguration settingConfiguration) { if (settingConfiguration == null) { throw new ArgumentNullException("settingConfiguration"); } return(settingConfiguration.Settings(new AppSettingsSettings())); }
/// <summary> /// Reads the xml file. /// </summary> /// <param name="settingConfiguration">Logger setting configuration.</param> /// <param name="filePath">Specify the path to xml file location. /// If the file does not exist it will be ignored.</param> /// <returns>An object allowing configuration to continue.</returns> public static LoggerConfiguration Xml(this LoggerSettingsConfiguration settingConfiguration, string filePath = null) { if (settingConfiguration == null) { throw new ArgumentNullException(nameof(settingConfiguration)); } return(settingConfiguration.Settings(new XmlSettings(filePath))); }
/// <summary> /// Reads logger settings from the provided configuration object using the default section name. Generally this /// is preferable over the other method that takes a configuration section. Only this version will populate /// IConfiguration parameters on target methods. /// </summary> /// <param name="settingConfiguration">Logger setting configuration.</param> /// <param name="configuration">A configuration object which contains a Serilog section.</param> /// <param name="dependencyContext">The dependency context from which sink/enricher packages can be located. If not supplied, the platform /// default will be used.</param> /// <returns>An object allowing configuration to continue.</returns> public static LoggerConfiguration Configuration( this LoggerSettingsConfiguration settingConfiguration, IConfiguration configuration, DependencyContext dependencyContext = null) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } return(settingConfiguration.Settings( new ConfigurationReader( configuration, dependencyContext ?? (Assembly.GetEntryAssembly() != null ? DependencyContext.Default : null)))); }
public static LoggerConfiguration ConfigurationSection( this LoggerSettingsConfiguration settingConfiguration, IConfigurationSection configSection, ConfigurationAssemblySource configurationAssemblySource) { if (settingConfiguration == null) { throw new ArgumentNullException(nameof(settingConfiguration)); } if (configSection == null) { throw new ArgumentNullException(nameof(configSection)); } var assemblyFinder = AssemblyFinder.ForSource(configurationAssemblySource); return(settingConfiguration.Settings(new ConfigurationReader(configSection, assemblyFinder, configuration: null))); }
/// <summary> /// Reads logger settings from the provided configuration object using the default section name. Generally this /// is preferable over the other method that takes a configuration section. Only this version will populate /// IConfiguration parameters on target methods. /// </summary> /// <param name="settingConfiguration">Logger setting configuration.</param> /// <param name="configuration">A configuration object which contains a Serilog section.</param> /// <param name="configurationAssemblySource">Defines how the package identifies assemblies to scan for sinks and other Types.</param> /// <returns>An object allowing configuration to continue.</returns> public static LoggerConfiguration Configuration( this LoggerSettingsConfiguration settingConfiguration, IConfiguration configuration, ConfigurationAssemblySource configurationAssemblySource) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if (configurationAssemblySource == ConfigurationAssemblySource.UseLoadedAssemblies) { return(Configuration(settingConfiguration, configuration)); } else { return(settingConfiguration.Settings(new ConfigurationReader(configuration, null))); } }
/// <summary> /// Supports command-line syntax like: --quiet --write-to:Console.outputTemplate="{Message}{NewLine}" /// </summary> /// <param name="settingsConfiguration"></param> /// <returns></returns> public static LoggerConfiguration CommandLine(this LoggerSettingsConfiguration settingsConfiguration) { return(settingsConfiguration.Settings(new CommandLineSettings())); }
/// <summary> /// Configure the logger using components from the <paramref name="services"/>. If present, the logger will /// receive implementations/instances of <see cref="LoggingLevelSwitch"/>, <see cref="IDestructuringPolicy"/>, /// <see cref="ILogEventFilter"/>, <see cref="ILogEventEnricher"/>, <see cref="ILogEventSink"/>, and /// <see cref="ILoggerSettings"/>. /// </summary> /// <param name="loggerSettingsConfiguration">The `ReadFrom` configuration object.</param> /// <param name="services">A <see cref="IServiceProvider"/> from which services will be requested.</param> /// <returns>A <see cref="LoggerConfiguration"/> to support method chaining.</returns> public static LoggerConfiguration Services( this LoggerSettingsConfiguration loggerSettingsConfiguration, IServiceProvider services) { return(loggerSettingsConfiguration.Settings(new InjectedLoggerSettings(services))); }
public static LoggerConfiguration CodeString(this LoggerSettingsConfiguration lsc, string csharpCode, Assembly[] referencedAssemblies = null) { return(lsc.Settings(new CodeSettings(csharpCode, referencedAssemblies))); }