diff --git a/README.md b/README.md index da52a1d..aa37f27 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,7 @@ The `AddFile()` method exposes some basic options for controlling the connection | `levelOverrides` | A dictionary mapping logger name prefixes to minimum logging levels. | | | `isJson` | If true, the log file will be written in JSON format. | `true` | | `fileSizeLimitBytes` | The maximum size, in bytes, to which any single log file will be allowed to grow. For unrestricted growth, pass`null`. The default is 1 GiB. | `1024 * 1024 * 1024` | +| `rollOnFileSizeLimit` | If true, a new file will be created when the file size limit is reached. The default is `false`. | `true` | | `retainedFileCountLimit` | The maximum number of log files that will be retained, including the current log file. For unlimited retention, pass `null`. The default is `31`. | `31` | | `outputTemplate` | The template used for formatting plain text log output. The default is `{Timestamp:o} {RequestId,13} [{Level:u3}] {Message} ({EventId:x8}){NewLine}{Exception}` | `{Timestamp:o} {RequestId,13} [{Level:u3}] {Message} {Properties:j} ({EventId:x8}){NewLine}{Exception}` | @@ -169,6 +170,7 @@ In addition to the properties shown above, the `"Logging"` configuration support | -------- | ----------- | ------- | | `Json` | If `true`, the log file will be written in JSON format. | `true` | | `FileSizeLimitBytes` | The maximum size, in bytes, to which any single log file will be allowed to grow. For unrestricted growth, pass`null`. The default is 1 GiB. | `1024 * 1024 * 1024` | +| `RollOnFileSizeLimit` | If true, a new file will be created when the file size limit is reached. The default is `false`. | `true` | | `RetainedFileCountLimit` | The maximum number of log files that will be retained, including the current log file. For unlimited retention, pass `null`. The default is `31`. | `31` | | `OutputTemplate` | The template used for formatting plain text log output. The default is `{Timestamp:o} {RequestId,13} [{Level:u3}] {Message} ({EventId:x8}){NewLine}{Exception}` | `{Timestamp:o} {RequestId,13} [{Level:u3}] {Message} {Properties:j} ({EventId:x8}){NewLine}{Exception}` | @@ -182,6 +184,6 @@ The following packages are used to provide `loggingBuilder.AddFile()`: * [Serilog.Formatting.Compact](https://github.com/serilog/serilog-formatting-compact) - JSON event formatting * [Serilog.Extensions.Logging](https://github.com/serilog/serilog-extensions-logging) - ASP.NET Core integration * [Serilog.Sinks.Async](https://github.com/serilog/serilog-sinks-async) - wrapper to perform log writes asynchronously - * [Serilog.Sinks.RollingFile](https://github.com/serilog/serilog-sinks-rollingfile) - rolling file output + * [Serilog.Sinks.File](https://github.com/serilog/serilog-sinks-file) - rolling file output If you decide to switch to the full Serilog API and need help, please drop into the [Gitter channel](https://gitter.im/serilog/serilog) or post your question on [Stack Overflow](http://stackoverflow.com/questions/tagged/serilog). diff --git a/src/Serilog.Extensions.Logging.File/Microsoft/Extensions/Logging/FileLoggerExtensions.cs b/src/Serilog.Extensions.Logging.File/Microsoft/Extensions/Logging/FileLoggerExtensions.cs index 784957a..e6e2873 100644 --- a/src/Serilog.Extensions.Logging.File/Microsoft/Extensions/Logging/FileLoggerExtensions.cs +++ b/src/Serilog.Extensions.Logging.File/Microsoft/Extensions/Logging/FileLoggerExtensions.cs @@ -37,7 +37,7 @@ public static ILoggerFactory AddFile(this ILoggerFactory loggerFactory, IConfigu var minimumLevel = GetMinimumLogLevel(configuration); var levelOverrides = GetLevelOverrides(configuration); - return loggerFactory.AddFile(config.PathFormat, minimumLevel, levelOverrides, config.Json, config.FileSizeLimitBytes, config.RetainedFileCountLimit, config.OutputTemplate); + return loggerFactory.AddFile(config.PathFormat, minimumLevel, levelOverrides, config.Json, config.FileSizeLimitBytes, config.RetainedFileCountLimit, config.OutputTemplate, config.RollOnFileSizeLimit); } /// @@ -55,6 +55,8 @@ public static ILoggerFactory AddFile(this ILoggerFactory loggerFactory, IConfigu /// log file. For unlimited retention, pass null. The default is 31. /// The template used for formatting plain text log output. The default is /// "{Timestamp:o} {RequestId,13} [{Level:u3}] {Message} ({EventId:x8}){NewLine}{Exception}" + /// If true, a new file will be created when the file size limit is reached. Filenames + /// will have a number appended in the format _NNN, with the first filename given no number. The default is false /// A logger factory to allow further configuration. public static ILoggerFactory AddFile( this ILoggerFactory loggerFactory, @@ -64,9 +66,10 @@ public static ILoggerFactory AddFile( bool isJson = false, long? fileSizeLimitBytes = FileLoggingConfiguration.DefaultFileSizeLimitBytes, int? retainedFileCountLimit = FileLoggingConfiguration.DefaultRetainedFileCountLimit, - string outputTemplate = FileLoggingConfiguration.DefaultOutputTemplate) + string outputTemplate = FileLoggingConfiguration.DefaultOutputTemplate, + bool rollOnFileSizeLimit = false) { - var logger = CreateLogger(pathFormat, minimumLevel, levelOverrides, isJson, fileSizeLimitBytes, retainedFileCountLimit, outputTemplate); + var logger = CreateLogger(pathFormat, minimumLevel, levelOverrides, isJson, fileSizeLimitBytes, retainedFileCountLimit, outputTemplate, rollOnFileSizeLimit); return loggerFactory.AddSerilog(logger, dispose: true); } @@ -91,7 +94,7 @@ public static ILoggingBuilder AddFile(this ILoggingBuilder loggingBuilder, IConf var minimumLevel = GetMinimumLogLevel(configuration); var levelOverrides = GetLevelOverrides(configuration); - return loggingBuilder.AddFile(config.PathFormat, minimumLevel, levelOverrides, config.Json, config.FileSizeLimitBytes, config.RetainedFileCountLimit, config.OutputTemplate); + return loggingBuilder.AddFile(config.PathFormat, minimumLevel, levelOverrides, config.Json, config.FileSizeLimitBytes, config.RetainedFileCountLimit, config.OutputTemplate, config.RollOnFileSizeLimit); } /// @@ -109,6 +112,8 @@ public static ILoggingBuilder AddFile(this ILoggingBuilder loggingBuilder, IConf /// log file. For unlimited retention, pass null. The default is 31. /// The template used for formatting plain text log output. The default is /// "{Timestamp:o} {RequestId,13} [{Level:u3}] {Message} ({EventId:x8}){NewLine}{Exception}" + /// If true, a new file will be created when the file size limit is reached. Filenames + /// will have a number appended in the format _NNN, with the first filename given no number. The default is false /// The logging builder to allow further configuration. public static ILoggingBuilder AddFile(this ILoggingBuilder loggingBuilder, string pathFormat, @@ -117,9 +122,10 @@ public static ILoggingBuilder AddFile(this ILoggingBuilder loggingBuilder, bool isJson = false, long? fileSizeLimitBytes = FileLoggingConfiguration.DefaultFileSizeLimitBytes, int? retainedFileCountLimit = FileLoggingConfiguration.DefaultRetainedFileCountLimit, - string outputTemplate = FileLoggingConfiguration.DefaultOutputTemplate) + string outputTemplate = FileLoggingConfiguration.DefaultOutputTemplate, + bool rollOnFileSizeLimit = false) { - var logger = CreateLogger(pathFormat, minimumLevel, levelOverrides, isJson, fileSizeLimitBytes, retainedFileCountLimit, outputTemplate); + var logger = CreateLogger(pathFormat, minimumLevel, levelOverrides, isJson, fileSizeLimitBytes, retainedFileCountLimit, outputTemplate, rollOnFileSizeLimit); return loggingBuilder.AddSerilog(logger, dispose: true); } @@ -130,7 +136,8 @@ private static Serilog.Core.Logger CreateLogger(string pathFormat, bool isJson, long? fileSizeLimitBytes, int? retainedFileCountLimit, - string outputTemplate) + string outputTemplate, + bool rollOnFileSizeLimit) { if (pathFormat == null) throw new ArgumentNullException(nameof(pathFormat)); @@ -143,13 +150,15 @@ private static Serilog.Core.Logger CreateLogger(string pathFormat, var configuration = new LoggerConfiguration() .MinimumLevel.Is(Conversions.MicrosoftToSerilogLevel(minimumLevel)) .Enrich.FromLogContext() - .WriteTo.Async(w => w.RollingFile( + .WriteTo.Async(w => w.File( formatter, Environment.ExpandEnvironmentVariables(pathFormat), fileSizeLimitBytes: fileSizeLimitBytes, retainedFileCountLimit: retainedFileCountLimit, shared: true, - flushToDiskInterval: TimeSpan.FromSeconds(2))); + flushToDiskInterval: TimeSpan.FromSeconds(2), + rollingInterval: RollingInterval.Day, + rollOnFileSizeLimit: rollOnFileSizeLimit)); if (!isJson) { diff --git a/src/Serilog.Extensions.Logging.File/Serilog.Extensions.Logging.File.csproj b/src/Serilog.Extensions.Logging.File/Serilog.Extensions.Logging.File.csproj index 1240b45..647b2fa 100644 --- a/src/Serilog.Extensions.Logging.File/Serilog.Extensions.Logging.File.csproj +++ b/src/Serilog.Extensions.Logging.File/Serilog.Extensions.Logging.File.csproj @@ -2,7 +2,7 @@ Add file logging to ASP.NET Core apps with one line of code. - 3.0.1 + 4.0.1 Serilog Contributors netstandard2.0;netcoreapp3.1;net5.0;net6.0 true @@ -32,9 +32,9 @@ - + diff --git a/src/Serilog.Extensions.Logging.File/Serilog/Extensions/Logging/File/FileLoggingConfiguration.cs b/src/Serilog.Extensions.Logging.File/Serilog/Extensions/Logging/File/FileLoggingConfiguration.cs index 7440c4f..ba014dd 100644 --- a/src/Serilog.Extensions.Logging.File/Serilog/Extensions/Logging/File/FileLoggingConfiguration.cs +++ b/src/Serilog.Extensions.Logging.File/Serilog/Extensions/Logging/File/FileLoggingConfiguration.cs @@ -46,5 +46,13 @@ public int? RetainedFileCountLimit /// The default is "{Timestamp:o} {RequestId,13} [{Level:u3}] {Message} ({EventId:x8}){NewLine}{Exception}" /// public string OutputTemplate { get; set; } = DefaultOutputTemplate; + + /// + /// If true, a new file will be created when the file size limit is reached. Filenames + /// will have a number appended in the format _NNN, with the first filename given no number. + /// The default is false + /// + public bool RollOnFileSizeLimit + { get; set; } = false; } }