Skip to content

Commit

Permalink
Apply common formatting: tabs + open block bracket on the same line
Browse files Browse the repository at this point in the history
  • Loading branch information
VitaliyMF committed Dec 24, 2023
1 parent 1e9b991 commit 78872bb
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 228 deletions.
67 changes: 32 additions & 35 deletions src/NReco.Logging.File/FileLoggerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,45 +23,42 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;

namespace NReco.Logging.File
{
namespace NReco.Logging.File {

/// <summary>
/// Generic file logger Configuration.
/// </summary>
public class FileLoggerConfig
{
/// <summary>
/// Path of the LogFile to use.
/// </summary>
public string Path { get; set; } = null;
/// <summary>
/// Generic file logger Configuration.
/// </summary>
public class FileLoggerConfig {
/// <summary>
/// Path of the LogFile to use.
/// </summary>
public string Path { get; set; } = null;

/// <summary>
/// Append to existing log files or override them.
/// </summary>
public bool Append { get; set; } = true;
/// <summary>
/// Append to existing log files or override them.
/// </summary>
public bool Append { get; set; } = true;

/// <summary>
/// Determines max size of the one log file.
/// </summary>
/// <remarks>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.
/// </remarks>
public long FileSizeLimitBytes { get; set; } = 0;
/// <summary>
/// Determines max size of the one log file.
/// </summary>
/// <remarks>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.
/// </remarks>
public long FileSizeLimitBytes { get; set; } = 0;

/// <summary>
/// Determines max number of log files if <see cref="FileSizeLimitBytes"/> is specified.
/// </summary>
/// <remarks>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).
/// </remarks>
public int MaxRollingFiles { get; set; } = 0;

/// <summary>
/// Minimal logging level for the file logger.
/// </summary>
public LogLevel MinLevel { get; set; } = LogLevel.Trace;
}
/// <summary>
/// Determines max number of log files if <see cref="FileSizeLimitBytes"/> is specified.
/// </summary>
/// <remarks>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).
/// </remarks>
public int MaxRollingFiles { get; set; } = 0;

/// <summary>
/// Minimal logging level for the file logger.
/// </summary>
public LogLevel MinLevel { get; set; } = LogLevel.Trace;
}

}
242 changes: 115 additions & 127 deletions src/NReco.Logging.File/FileLoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,132 +24,120 @@

namespace NReco.Logging.File {

public static class FileLoggerExtensions
{

/// <summary>
/// Adds a file logger.
/// </summary>
public static ILoggingBuilder AddFile(this ILoggingBuilder builder, string fileName, bool append = true)
{
builder.Services.Add(ServiceDescriptor.Singleton<ILoggerProvider, FileLoggerProvider>(
(srvPrv) =>
{
return new FileLoggerProvider(fileName, append);
}
));
return builder;
}

/// <summary>
/// Adds a file logger.
/// </summary>
public static ILoggingBuilder AddFile(this ILoggingBuilder builder, string fileName, Action<FileLoggerOptions> configure)
{
builder.Services.Add(ServiceDescriptor.Singleton<ILoggerProvider, FileLoggerProvider>(
(srvPrv) =>
{
var options = new FileLoggerOptions();
configure(options);
return new FileLoggerProvider(fileName, options);
}
));
return builder;
}

/// <summary>
/// Adds a file logger by specified configuration.
/// </summary>
/// <remarks>File logger is not added if "File" section is not present or it doesn't contain "Path" property.</remarks>
public static ILoggingBuilder AddFile(this ILoggingBuilder builder, IConfiguration configuration, Action<FileLoggerOptions> configure = null)
{
var fileLoggerPrv = CreateFromConfiguration(configuration, configure);
if (fileLoggerPrv != null)
{
builder.Services.AddSingleton<ILoggerProvider, FileLoggerProvider>(
(srvPrv) =>
{
return fileLoggerPrv;
}
);
}
return builder;
}

/// <summary>
/// Adds a file logger.
/// </summary>
/// <param name="factory">The <see cref="ILoggerFactory"/> to use.</param>
/// <param name="fileName">log file name.</param>
/// <param name="append">if true new log entries are appended to the existing file.</param>
public static ILoggerFactory AddFile(this ILoggerFactory factory, string fileName, bool append = true)
{
factory.AddProvider(new FileLoggerProvider(fileName, append));
return factory;
}

/// <summary>
/// Adds a file logger.
/// </summary>
/// <param name="factory">The <see cref="ILoggerFactory"/> to use.</param>
/// <param name="fileName">log file name.</param>
/// <param name="configure">a handler that initializes <see cref="FileLoggerOptions"/>.</param>
public static ILoggerFactory AddFile(this ILoggerFactory factory, string fileName, Action<FileLoggerOptions> configure) {
var fileLoggerOptions = new FileLoggerOptions();
configure(fileLoggerOptions);
factory.AddProvider(new FileLoggerProvider(fileName, fileLoggerOptions));
return factory;
}

/// <summary>
/// Adds a file logger and configures it with given <see cref="IConfiguration"/> (usually "Logging" section).
/// </summary>
/// <param name="factory">The <see cref="ILoggerFactory"/> to use.</param>
/// <param name="configuration">The <see cref="IConfiguration"/> to use getting <see cref="FileLoggerProvider"/> settings.</param>
/// <param name="configure">a handler that initializes <see cref="FileLoggerOptions"/>.</param>
public static ILoggerFactory AddFile(this ILoggerFactory factory, IConfiguration configuration, Action<FileLoggerOptions> 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<FileLoggerOptions> 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 {

Check warning on line 27 in src/NReco.Logging.File/FileLoggerExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'FileLoggerExtensions'

/// <summary>
/// Adds a file logger.
/// </summary>
public static ILoggingBuilder AddFile(this ILoggingBuilder builder, string fileName, bool append = true) {
builder.Services.Add(ServiceDescriptor.Singleton<ILoggerProvider, FileLoggerProvider>(
(srvPrv) => {
return new FileLoggerProvider(fileName, append);
}
));
return builder;
}

/// <summary>
/// Adds a file logger.
/// </summary>
public static ILoggingBuilder AddFile(this ILoggingBuilder builder, string fileName, Action<FileLoggerOptions> configure) {
builder.Services.Add(ServiceDescriptor.Singleton<ILoggerProvider, FileLoggerProvider>(
(srvPrv) => {
var options = new FileLoggerOptions();
configure(options);
return new FileLoggerProvider(fileName, options);
}
));
return builder;
}

/// <summary>
/// Adds a file logger by specified configuration.
/// </summary>
/// <remarks>File logger is not added if "File" section is not present or it doesn't contain "Path" property.</remarks>
public static ILoggingBuilder AddFile(this ILoggingBuilder builder, IConfiguration configuration, Action<FileLoggerOptions> configure = null) {
var fileLoggerPrv = CreateFromConfiguration(configuration, configure);
if (fileLoggerPrv != null) {
builder.Services.AddSingleton<ILoggerProvider, FileLoggerProvider>(
(srvPrv) => {
return fileLoggerPrv;
}
);
}
return builder;
}

/// <summary>
/// Adds a file logger.
/// </summary>
/// <param name="factory">The <see cref="ILoggerFactory"/> to use.</param>
/// <param name="fileName">log file name.</param>
/// <param name="append">if true new log entries are appended to the existing file.</param>
public static ILoggerFactory AddFile(this ILoggerFactory factory, string fileName, bool append = true) {
factory.AddProvider(new FileLoggerProvider(fileName, append));
return factory;
}

/// <summary>
/// Adds a file logger.
/// </summary>
/// <param name="factory">The <see cref="ILoggerFactory"/> to use.</param>
/// <param name="fileName">log file name.</param>
/// <param name="configure">a handler that initializes <see cref="FileLoggerOptions"/>.</param>
public static ILoggerFactory AddFile(this ILoggerFactory factory, string fileName, Action<FileLoggerOptions> configure) {
var fileLoggerOptions = new FileLoggerOptions();
configure(fileLoggerOptions);
factory.AddProvider(new FileLoggerProvider(fileName, fileLoggerOptions));
return factory;
}

/// <summary>
/// Adds a file logger and configures it with given <see cref="IConfiguration"/> (usually "Logging" section).
/// </summary>
/// <param name="factory">The <see cref="ILoggerFactory"/> to use.</param>
/// <param name="configuration">The <see cref="IConfiguration"/> to use getting <see cref="FileLoggerProvider"/> settings.</param>
/// <param name="configure">a handler that initializes <see cref="FileLoggerOptions"/>.</param>
public static ILoggerFactory AddFile(this ILoggerFactory factory, IConfiguration configuration, Action<FileLoggerOptions> 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<FileLoggerOptions> 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);
}

}

}
Loading

0 comments on commit 78872bb

Please sign in to comment.