diff --git a/src/NReco.Logging.File/FileLoggerConfig.cs b/src/NReco.Logging.File/FileLoggerConfig.cs index bf4b308..82aa13f 100644 --- a/src/NReco.Logging.File/FileLoggerConfig.cs +++ b/src/NReco.Logging.File/FileLoggerConfig.cs @@ -23,45 +23,42 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Configuration; -namespace NReco.Logging.File -{ +namespace NReco.Logging.File { - /// - /// Generic file logger Configuration. - /// - public class FileLoggerConfig - { - /// - /// Path of the LogFile to use. - /// - public string Path { get; set; } = null; + /// + /// Generic file logger Configuration. + /// + public class FileLoggerConfig { + /// + /// Path of the LogFile to use. + /// + public string Path { get; set; } = null; - /// - /// Append to existing log files or override them. - /// - public bool Append { get; set; } = true; + /// + /// Append to existing log files or override them. + /// + public bool Append { get; set; } = true; - /// - /// Determines max size of the one log file. - /// - /// If log file limit is specified logger will create new file when limit is reached. - /// For example, if log file name is 'test.log', logger will create 'test1.log', 'test2.log' etc. - /// - public long FileSizeLimitBytes { get; set; } = 0; + /// + /// Determines max size of the one log file. + /// + /// If log file limit is specified logger will create new file when limit is reached. + /// For example, if log file name is 'test.log', logger will create 'test1.log', 'test2.log' etc. + /// + public long FileSizeLimitBytes { get; set; } = 0; - /// - /// Determines max number of log files if is specified. - /// - /// If MaxRollingFiles is specified file logger will re-write previously created log files. - /// For example, if log file name is 'test.log' and max files = 3, logger will use: 'test.log', then 'test1.log', then 'test2.log' and then 'test.log' again (old content is removed). - /// - public int MaxRollingFiles { get; set; } = 0; - - /// - /// Minimal logging level for the file logger. - /// - public LogLevel MinLevel { get; set; } = LogLevel.Trace; - } + /// + /// Determines max number of log files if is specified. + /// + /// If MaxRollingFiles is specified file logger will re-write previously created log files. + /// For example, if log file name is 'test.log' and max files = 3, logger will use: 'test.log', then 'test1.log', then 'test2.log' and then 'test.log' again (old content is removed). + /// + public int MaxRollingFiles { get; set; } = 0; + /// + /// Minimal logging level for the file logger. + /// + public LogLevel MinLevel { get; set; } = LogLevel.Trace; + } } diff --git a/src/NReco.Logging.File/FileLoggerExtensions.cs b/src/NReco.Logging.File/FileLoggerExtensions.cs index 16ddede..a30cb0c 100644 --- a/src/NReco.Logging.File/FileLoggerExtensions.cs +++ b/src/NReco.Logging.File/FileLoggerExtensions.cs @@ -24,132 +24,120 @@ namespace NReco.Logging.File { - public static class FileLoggerExtensions - { - - /// - /// Adds a file logger. - /// - public static ILoggingBuilder AddFile(this ILoggingBuilder builder, string fileName, bool append = true) - { - builder.Services.Add(ServiceDescriptor.Singleton( - (srvPrv) => - { - return new FileLoggerProvider(fileName, append); - } - )); - return builder; - } - - /// - /// Adds a file logger. - /// - public static ILoggingBuilder AddFile(this ILoggingBuilder builder, string fileName, Action configure) - { - builder.Services.Add(ServiceDescriptor.Singleton( - (srvPrv) => - { - var options = new FileLoggerOptions(); - configure(options); - return new FileLoggerProvider(fileName, options); - } - )); - return builder; - } - - /// - /// Adds a file logger by specified configuration. - /// - /// File logger is not added if "File" section is not present or it doesn't contain "Path" property. - public static ILoggingBuilder AddFile(this ILoggingBuilder builder, IConfiguration configuration, Action configure = null) - { - var fileLoggerPrv = CreateFromConfiguration(configuration, configure); - if (fileLoggerPrv != null) - { - builder.Services.AddSingleton( - (srvPrv) => - { - return fileLoggerPrv; - } - ); - } - return builder; - } - - /// - /// Adds a file logger. - /// - /// The to use. - /// log file name. - /// if true new log entries are appended to the existing file. - public static ILoggerFactory AddFile(this ILoggerFactory factory, string fileName, bool append = true) - { - factory.AddProvider(new FileLoggerProvider(fileName, append)); - return factory; - } - - /// - /// Adds a file logger. - /// - /// The to use. - /// log file name. - /// a handler that initializes . - public static ILoggerFactory AddFile(this ILoggerFactory factory, string fileName, Action configure) { - var fileLoggerOptions = new FileLoggerOptions(); - configure(fileLoggerOptions); - factory.AddProvider(new FileLoggerProvider(fileName, fileLoggerOptions)); - return factory; - } - - /// - /// Adds a file logger and configures it with given (usually "Logging" section). - /// - /// The to use. - /// The to use getting settings. - /// a handler that initializes . - public static ILoggerFactory AddFile(this ILoggerFactory factory, IConfiguration configuration, Action configure = null) - { - var prvFactory = factory; - var fileLoggerPrv = CreateFromConfiguration(configuration, configure); - if (fileLoggerPrv == null) - return factory; - prvFactory.AddProvider(fileLoggerPrv); - return factory; - } - - private static FileLoggerProvider CreateFromConfiguration(IConfiguration configuration, Action configure) - { - var config = new FileLoggerConfig(); - var fileSection = configuration.GetSection("File"); - if (!fileSection.Exists()) { - var pathValue = configuration["Path"]; - if (String.IsNullOrEmpty(pathValue)) - return null; // file logger is not configured - else { - // configuration contains "Path" property so this is explicitly-passed configuration section - configuration.Bind(config); - } - } else { - fileSection.Bind(config); - } - - if (String.IsNullOrWhiteSpace(config.Path)) - return null; // file logger is not configured - - var fileLoggerOptions = new FileLoggerOptions(); - - fileLoggerOptions.Append = config.Append; - fileLoggerOptions.MinLevel = config.MinLevel; - fileLoggerOptions.FileSizeLimitBytes = config.FileSizeLimitBytes; - fileLoggerOptions.MaxRollingFiles = config.MaxRollingFiles; - - if (configure != null) - configure(fileLoggerOptions); - - return new FileLoggerProvider(config.Path, fileLoggerOptions); - } - - - } + public static class FileLoggerExtensions { + + /// + /// Adds a file logger. + /// + public static ILoggingBuilder AddFile(this ILoggingBuilder builder, string fileName, bool append = true) { + builder.Services.Add(ServiceDescriptor.Singleton( + (srvPrv) => { + return new FileLoggerProvider(fileName, append); + } + )); + return builder; + } + + /// + /// Adds a file logger. + /// + public static ILoggingBuilder AddFile(this ILoggingBuilder builder, string fileName, Action configure) { + builder.Services.Add(ServiceDescriptor.Singleton( + (srvPrv) => { + var options = new FileLoggerOptions(); + configure(options); + return new FileLoggerProvider(fileName, options); + } + )); + return builder; + } + + /// + /// Adds a file logger by specified configuration. + /// + /// File logger is not added if "File" section is not present or it doesn't contain "Path" property. + public static ILoggingBuilder AddFile(this ILoggingBuilder builder, IConfiguration configuration, Action configure = null) { + var fileLoggerPrv = CreateFromConfiguration(configuration, configure); + if (fileLoggerPrv != null) { + builder.Services.AddSingleton( + (srvPrv) => { + return fileLoggerPrv; + } + ); + } + return builder; + } + + /// + /// Adds a file logger. + /// + /// The to use. + /// log file name. + /// if true new log entries are appended to the existing file. + public static ILoggerFactory AddFile(this ILoggerFactory factory, string fileName, bool append = true) { + factory.AddProvider(new FileLoggerProvider(fileName, append)); + return factory; + } + + /// + /// Adds a file logger. + /// + /// The to use. + /// log file name. + /// a handler that initializes . + public static ILoggerFactory AddFile(this ILoggerFactory factory, string fileName, Action configure) { + var fileLoggerOptions = new FileLoggerOptions(); + configure(fileLoggerOptions); + factory.AddProvider(new FileLoggerProvider(fileName, fileLoggerOptions)); + return factory; + } + + /// + /// Adds a file logger and configures it with given (usually "Logging" section). + /// + /// The to use. + /// The to use getting settings. + /// a handler that initializes . + public static ILoggerFactory AddFile(this ILoggerFactory factory, IConfiguration configuration, Action configure = null) { + var prvFactory = factory; + var fileLoggerPrv = CreateFromConfiguration(configuration, configure); + if (fileLoggerPrv == null) + return factory; + prvFactory.AddProvider(fileLoggerPrv); + return factory; + } + + private static FileLoggerProvider CreateFromConfiguration(IConfiguration configuration, Action configure) { + var config = new FileLoggerConfig(); + var fileSection = configuration.GetSection("File"); + if (!fileSection.Exists()) { + var pathValue = configuration["Path"]; + if (String.IsNullOrEmpty(pathValue)) + return null; // file logger is not configured + else { + // configuration contains "Path" property so this is explicitly-passed configuration section + configuration.Bind(config); + } + } else { + fileSection.Bind(config); + } + + if (String.IsNullOrWhiteSpace(config.Path)) + return null; // file logger is not configured + + var fileLoggerOptions = new FileLoggerOptions(); + + fileLoggerOptions.Append = config.Append; + fileLoggerOptions.MinLevel = config.MinLevel; + fileLoggerOptions.FileSizeLimitBytes = config.FileSizeLimitBytes; + fileLoggerOptions.MaxRollingFiles = config.MaxRollingFiles; + + if (configure != null) + configure(fileLoggerOptions); + + return new FileLoggerProvider(config.Path, fileLoggerOptions); + } + + } } diff --git a/src/NReco.Logging.File/FileLoggerOptions.cs b/src/NReco.Logging.File/FileLoggerOptions.cs index 76083e3..4a81cc8 100644 --- a/src/NReco.Logging.File/FileLoggerOptions.cs +++ b/src/NReco.Logging.File/FileLoggerOptions.cs @@ -23,83 +23,82 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Configuration; -namespace NReco.Logging.File -{ +namespace NReco.Logging.File { - /// - /// Generic file logger options. - /// - public class FileLoggerOptions - { - /// - /// Append to existing log files or override them. - /// - public bool Append { get; set; } = true; + /// + /// Generic file logger options. + /// + public class FileLoggerOptions + { + /// + /// Append to existing log files or override them. + /// + public bool Append { get; set; } = true; - /// - /// Determines max size of the one log file. - /// - /// If log file limit is specified logger will create new file when limit is reached. - /// For example, if log file name is 'test.log', logger will create 'test1.log', 'test2.log' etc. - /// - public long FileSizeLimitBytes { get; set; } = 0; + /// + /// Determines max size of the one log file. + /// + /// If log file limit is specified logger will create new file when limit is reached. + /// For example, if log file name is 'test.log', logger will create 'test1.log', 'test2.log' etc. + /// + public long FileSizeLimitBytes { get; set; } = 0; - /// - /// Determines max number of log files if is specified. - /// - /// If MaxRollingFiles is specified file logger will re-write previously created log files. - /// For example, if log file name is 'test.log' and max files = 3, logger will use: 'test.log', then 'test1.log', then 'test2.log' and then 'test.log' again (old content is removed). - /// - public int MaxRollingFiles { get; set; } = 0; + /// + /// Determines max number of log files if is specified. + /// + /// If MaxRollingFiles is specified file logger will re-write previously created log files. + /// For example, if log file name is 'test.log' and max files = 3, logger will use: 'test.log', then 'test1.log', then 'test2.log' and then 'test.log' again (old content is removed). + /// + public int MaxRollingFiles { get; set; } = 0; - /// + /// /// Gets or sets indication whether or not UTC timezone should be used to for timestamps in logging messages. Defaults to false. /// public bool UseUtcTimestamp { get; set; } - /// - /// Custom formatter for the log entry line. - /// - public Func FormatLogEntry { get; set; } + /// + /// Custom formatter for the log entry line. + /// + public Func FormatLogEntry { get; set; } - /// - /// Custom filter for the log entry. - /// - public Func FilterLogEntry { get; set; } + /// + /// Custom filter for the log entry. + /// + public Func FilterLogEntry { get; set; } - /// - /// Minimal logging level for the file logger. - /// - public LogLevel MinLevel { get; set; } = LogLevel.Trace; + /// + /// Minimal logging level for the file logger. + /// + public LogLevel MinLevel { get; set; } = LogLevel.Trace; - /// - /// Custom formatter for the log file name. - /// - /// By specifying custom formatting handler you can define your own criteria for creation of log files. Note that this handler is called - /// on EVERY log message 'write'; you may cache the log file name calculation in your handler to avoid any potential overhead in case of high-load logger usage. - /// For example: - /// - /// - /// fileLoggerOpts.FormatLogFileName = (fname) => { - /// return String.Format( Path.GetFileNameWithoutExtension(fname) + "_{0:yyyy}-{0:MM}-{0:dd}" + Path.GetExtension(fname), DateTime.UtcNow); - /// }; - /// - public Func FormatLogFileName { get; set; } + /// + /// Custom formatter for the log file name. + /// + /// By specifying custom formatting handler you can define your own criteria for creation of log files. Note that this handler is called + /// on EVERY log message 'write'; you may cache the log file name calculation in your handler to avoid any potential overhead in case of high-load logger usage. + /// For example: + /// + /// + /// fileLoggerOpts.FormatLogFileName = (fname) => { + /// return String.Format( Path.GetFileNameWithoutExtension(fname) + "_{0:yyyy}-{0:MM}-{0:dd}" + Path.GetExtension(fname), DateTime.UtcNow); + /// }; + /// + public Func FormatLogFileName { get; set; } - /// - /// Custom handler for log file errors. - /// - /// If this handler is provided file open exception (on FileLoggerProvider creation) will be suppressed. - /// You can handle file error exception according to your app's logic, and propose an alternative log file name (if you want to keep file logger working). - /// - /// - /// fileLoggerOpts.HandleFileError = (err) => { - /// err.UseNewLogFileName( Path.GetFileNameWithoutExtension(err.LogFileName)+ "_alt" + Path.GetExtension(err.LogFileName) ); - /// }; - /// - public Action HandleFileError { get; set; } + /// + /// Custom handler for log file errors. + /// + /// If this handler is provided file open exception (on FileLoggerProvider creation) will be suppressed. + /// You can handle file error exception according to your app's logic, and propose an alternative log file name (if you want to keep file logger working). + /// + /// + /// fileLoggerOpts.HandleFileError = (err) => { + /// err.UseNewLogFileName( Path.GetFileNameWithoutExtension(err.LogFileName)+ "_alt" + Path.GetExtension(err.LogFileName) ); + /// }; + /// + public Action HandleFileError { get; set; } - } + } } diff --git a/src/NReco.Logging.File/LogMessage.cs b/src/NReco.Logging.File/LogMessage.cs index 9c12b21..1a6c72f 100644 --- a/src/NReco.Logging.File/LogMessage.cs +++ b/src/NReco.Logging.File/LogMessage.cs @@ -17,8 +17,8 @@ using System.Text; using Microsoft.Extensions.Logging; -namespace NReco.Logging.File -{ +namespace NReco.Logging.File { + public struct LogMessage { public readonly string LogName; public readonly string Message;