From a0677453f0f2683e5d77e938433946dfec79f6c3 Mon Sep 17 00:00:00 2001 From: Kirill Date: Thu, 6 Jul 2023 19:13:42 +0300 Subject: [PATCH 1/2] add rollOnFileSizeLimit --- README.md | 12 +++++++-- example/WebApplication/appsettings.json | 2 ++ .../Logging/FileLoggerExtensions.cs | 27 ++++++++++++------- .../Serilog.Extensions.Logging.File.csproj | 4 +-- .../Logging/File/FileLoggingConfiguration.cs | 8 ++++++ 5 files changed, 40 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index da52a1d..2311909 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,13 @@ log-20160701.txt log-20160702.txt ``` -To prevent outages due to disk space exhaustion, each file is capped to 1 GB in size. If the file size is exceeded, events will be dropped until the next roll point. +To prevent outages due to disk space exhaustion, each file is capped to 1 GB in size. If the file size is exceeded, events will be dropped until the next roll point or new file with number appended in the format _NNN, with the first filename given no number, if rollOnFileSizeLimit is true. + +``` +log-20160631.txt +log-20160631_001.txt +log-20160631_002.txt +``` ### Message templates and event ids @@ -136,6 +142,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 +176,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 +190,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/example/WebApplication/appsettings.json b/example/WebApplication/appsettings.json index 59370a1..1eac5ad 100644 --- a/example/WebApplication/appsettings.json +++ b/example/WebApplication/appsettings.json @@ -3,6 +3,8 @@ "PathFormat": "Logs/log-{Date}.txt", "OutputTemplate": "{Timestamp:o} {RequestId,13} [{Level:u3}] {Message} ({EventId:x8}) {Properties:j}{NewLine}{Exception}", "IncludeScopes": true, + "RollOnFileSizeLimit": true, + "FileSizeLimitBytes": 10485760, "LogLevel": { "Default": "Warning" } 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..ac469df 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 + 3.1.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; } } From cca0c2d3c124924c480e74d50fe6fb98c2387607 Mon Sep 17 00:00:00 2001 From: Kirill Date: Thu, 13 Jul 2023 14:28:10 +0300 Subject: [PATCH 2/2] fix review points --- README.md | 8 +------- example/WebApplication/appsettings.json | 2 -- .../Serilog.Extensions.Logging.File.csproj | 2 +- 3 files changed, 2 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 2311909..aa37f27 100644 --- a/README.md +++ b/README.md @@ -109,13 +109,7 @@ log-20160701.txt log-20160702.txt ``` -To prevent outages due to disk space exhaustion, each file is capped to 1 GB in size. If the file size is exceeded, events will be dropped until the next roll point or new file with number appended in the format _NNN, with the first filename given no number, if rollOnFileSizeLimit is true. - -``` -log-20160631.txt -log-20160631_001.txt -log-20160631_002.txt -``` +To prevent outages due to disk space exhaustion, each file is capped to 1 GB in size. If the file size is exceeded, events will be dropped until the next roll point. ### Message templates and event ids diff --git a/example/WebApplication/appsettings.json b/example/WebApplication/appsettings.json index 1eac5ad..59370a1 100644 --- a/example/WebApplication/appsettings.json +++ b/example/WebApplication/appsettings.json @@ -3,8 +3,6 @@ "PathFormat": "Logs/log-{Date}.txt", "OutputTemplate": "{Timestamp:o} {RequestId,13} [{Level:u3}] {Message} ({EventId:x8}) {Properties:j}{NewLine}{Exception}", "IncludeScopes": true, - "RollOnFileSizeLimit": true, - "FileSizeLimitBytes": 10485760, "LogLevel": { "Default": "Warning" } 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 ac469df..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.1.1 + 4.0.1 Serilog Contributors netstandard2.0;netcoreapp3.1;net5.0;net6.0 true