Skip to content

Commit b8986cb

Browse files
committed
Rolling WIP
1 parent 0639f1e commit b8986cb

13 files changed

+272
-165
lines changed

example/Sample/Sample.csproj

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netcoreapp1.0;net45</TargetFrameworks>
5+
<AssemblyName>Sample</AssemblyName>
6+
<OutputType>Exe</OutputType>
7+
<PackageId>Sample</PackageId>
8+
<RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\..\src\Serilog.Sinks.File\Serilog.Sinks.File.csproj" />
13+
</ItemGroup>
14+
15+
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
16+
<Reference Include="System" />
17+
<Reference Include="Microsoft.CSharp" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<PackageReference Include="System.Threading" Version="4.3.0" />
22+
<PackageReference Include="System.Threading.Thread" Version="4.3.0" />
23+
</ItemGroup>
24+
25+
</Project>
26+

example/Sample/Sample.xproj

Lines changed: 0 additions & 21 deletions
This file was deleted.

example/Sample/project.json

Lines changed: 0 additions & 23 deletions
This file was deleted.

global.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

serilog-sinks-file.sln

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 14
4-
VisualStudioVersion = 14.0.25420.1
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26730.15
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{037440DE-440B-4129-9F7A-09B42D00397E}"
77
EndProject
@@ -12,21 +12,20 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{E9D1B5
1212
appveyor.yml = appveyor.yml
1313
Build.ps1 = Build.ps1
1414
build.sh = build.sh
15-
global.json = global.json
1615
NuGet.Config = NuGet.Config
1716
README.md = README.md
1817
assets\Serilog.snk = assets\Serilog.snk
1918
EndProjectSection
2019
EndProject
21-
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Serilog.Sinks.File", "src\Serilog.Sinks.File\Serilog.Sinks.File.xproj", "{57E0ED0E-0F45-48AB-A73D-6A92B7C32095}"
22-
EndProject
2320
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{7B927378-9F16-4F6F-B3F6-156395136646}"
2421
EndProject
25-
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Serilog.Sinks.File.Tests", "test\Serilog.Sinks.File.Tests\Serilog.Sinks.File.Tests.xproj", "{3C2D8E01-5580-426A-BDD9-EC59CD98E618}"
26-
EndProject
2722
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "example", "example", "{196B1544-C617-4D7C-96D1-628713BDD52A}"
2823
EndProject
29-
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Sample", "example\Sample\Sample.xproj", "{A34235A2-A717-4A1C-BF5C-F4A9E06E1260}"
24+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Sinks.File", "src\Serilog.Sinks.File\Serilog.Sinks.File.csproj", "{57E0ED0E-0F45-48AB-A73D-6A92B7C32095}"
25+
EndProject
26+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Sinks.File.Tests", "test\Serilog.Sinks.File.Tests\Serilog.Sinks.File.Tests.csproj", "{3C2D8E01-5580-426A-BDD9-EC59CD98E618}"
27+
EndProject
28+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample", "example\Sample\Sample.csproj", "{A34235A2-A717-4A1C-BF5C-F4A9E06E1260}"
3029
EndProject
3130
Global
3231
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -55,4 +54,7 @@ Global
5554
{3C2D8E01-5580-426A-BDD9-EC59CD98E618} = {7B927378-9F16-4F6F-B3F6-156395136646}
5655
{A34235A2-A717-4A1C-BF5C-F4A9E06E1260} = {196B1544-C617-4D7C-96D1-628713BDD52A}
5756
EndGlobalSection
57+
GlobalSection(ExtensibilityGlobals) = postSolution
58+
SolutionGuid = {EA0197B4-FCA8-4DF2-BF34-274FA41333D1}
59+
EndGlobalSection
5860
EndGlobal

src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs

Lines changed: 105 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
using System;
16+
using System.ComponentModel;
1617
using Serilog.Configuration;
1718
using Serilog.Core;
1819
using Serilog.Debugging;
@@ -22,13 +23,15 @@
2223
using Serilog.Formatting.Json;
2324
using Serilog.Sinks.File;
2425

26+
// ReSharper disable MethodOverloadWithOptionalParameter
27+
2528
namespace Serilog
2629
{
2730
/// <summary>Extends <see cref="LoggerConfiguration"/> with methods to add file sinks.</summary>
2831
public static class FileLoggerConfigurationExtensions
2932
{
3033
const long DefaultFileSizeLimitBytes = 1L * 1024 * 1024 * 1024;
31-
const string DefaultOutputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}";
34+
const string DefaultOutputTemplate = "[{Timestamp:o} {Level:u3}] {Message:lj} {Properties}{NewLine}{Exception}";
3235

3336
/// <summary>
3437
/// Write log events to the specified file.
@@ -41,7 +44,7 @@ public static class FileLoggerConfigurationExtensions
4144
/// to be changed at runtime.</param>
4245
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
4346
/// <param name="outputTemplate">A message template describing the format used to write to the sink.
44-
/// the default is "{Timestamp} [{Level}] {Message}{NewLine}{Exception}".</param>
47+
/// the default is "[{Timestamp:o} {Level:u3}] {Message:lj} {Properties}{NewLine}{Exception}".</param>
4548
/// <param name="fileSizeLimitBytes">The approximate maximum size, in bytes, to which a log file will be allowed to grow.
4649
/// For unrestricted growth, pass null. The default is 1 GB. To avoid writing partial events, the last event within the limit
4750
/// will be written in full even if it exceeds the limit.</param>
@@ -51,6 +54,89 @@ public static class FileLoggerConfigurationExtensions
5154
/// <param name="flushToDiskInterval">If provided, a full disk flush will be performed periodically at the specified interval.</param>
5255
/// <returns>Configuration object allowing method chaining.</returns>
5356
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
57+
[Obsolete("New code should not be compiled against this obsolete overload"), EditorBrowsable(EditorBrowsableState.Never)]
58+
public static LoggerConfiguration File(
59+
this LoggerSinkConfiguration sinkConfiguration,
60+
string path,
61+
LogEventLevel restrictedToMinimumLevel,
62+
string outputTemplate,
63+
IFormatProvider formatProvider,
64+
long? fileSizeLimitBytes,
65+
LoggingLevelSwitch levelSwitch,
66+
bool buffered,
67+
bool shared,
68+
TimeSpan? flushToDiskInterval)
69+
{
70+
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
71+
if (path == null) throw new ArgumentNullException(nameof(path));
72+
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
73+
74+
var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
75+
return File(sinkConfiguration, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes, levelSwitch, buffered: buffered, shared: shared, flushToDiskInterval: flushToDiskInterval);
76+
}
77+
78+
/// <summary>
79+
/// Write log events to the specified file.
80+
/// </summary>
81+
/// <param name="sinkConfiguration">Logger sink configuration.</param>
82+
/// <param name="formatter">A formatter, such as <see cref="JsonFormatter"/>, to convert the log events into
83+
/// text for the file. If control of regular text formatting is required, use the other
84+
/// overload of <see cref="File(LoggerSinkConfiguration, string, LogEventLevel, string, IFormatProvider, long?, LoggingLevelSwitch, bool, bool, TimeSpan?)"/>
85+
/// and specify the outputTemplate parameter instead.
86+
/// </param>
87+
/// <param name="path">Path to the file.</param>
88+
/// <param name="restrictedToMinimumLevel">The minimum level for
89+
/// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
90+
/// <param name="levelSwitch">A switch allowing the pass-through minimum level
91+
/// to be changed at runtime.</param>
92+
/// <param name="fileSizeLimitBytes">The approximate maximum size, in bytes, to which a log file will be allowed to grow.
93+
/// For unrestricted growth, pass null. The default is 1 GB. To avoid writing partial events, the last event within the limit
94+
/// will be written in full even if it exceeds the limit.</param>
95+
/// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
96+
/// is false.</param>
97+
/// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
98+
/// <param name="flushToDiskInterval">If provided, a full disk flush will be performed periodically at the specified interval.</param>
99+
/// <returns>Configuration object allowing method chaining.</returns>
100+
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
101+
[Obsolete("New code should not be compiled against this obsolete overload"), EditorBrowsable(EditorBrowsableState.Never)]
102+
public static LoggerConfiguration File(
103+
this LoggerSinkConfiguration sinkConfiguration,
104+
ITextFormatter formatter,
105+
string path,
106+
LogEventLevel restrictedToMinimumLevel,
107+
long? fileSizeLimitBytes,
108+
LoggingLevelSwitch levelSwitch,
109+
bool buffered,
110+
bool shared,
111+
TimeSpan? flushToDiskInterval)
112+
{
113+
return ConfigureFile(sinkConfiguration.Sink, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes, levelSwitch, buffered: buffered, shared: shared, flushToDiskInterval: flushToDiskInterval);
114+
}
115+
116+
/// <summary>
117+
/// Write log events to the specified file.
118+
/// </summary>
119+
/// <param name="sinkConfiguration">Logger sink configuration.</param>
120+
/// <param name="path">Path to the file.</param>
121+
/// <param name="restrictedToMinimumLevel">The minimum level for
122+
/// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
123+
/// <param name="levelSwitch">A switch allowing the pass-through minimum level
124+
/// to be changed at runtime.</param>
125+
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
126+
/// <param name="outputTemplate">A message template describing the format used to write to the sink.
127+
/// the default is "[{Timestamp:o} {Level:u3}] {Message:lj} {Properties}{NewLine}{Exception}".</param>
128+
/// <param name="fileSizeLimitBytes">The approximate maximum size, in bytes, to which a log file will be allowed to grow.
129+
/// For unrestricted growth, pass null. The default is 1 GB. To avoid writing partial events, the last event within the limit
130+
/// will be written in full even if it exceeds the limit.</param>
131+
/// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
132+
/// is false.</param>
133+
/// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
134+
/// <param name="flushToDiskInterval">If provided, a full disk flush will be performed periodically at the specified interval.</param>
135+
/// <param name="rollingInterval">The interval at which logging will roll over to a new file.</param>
136+
/// <param name="rollOnFileSizeLimit">If <code>true</code>, a new file will be created when the file size limit is reached. Filenames
137+
/// will have a number appended in the format <code>_NNNNN</code>, with the first filename given no number.</param>
138+
/// <returns>Configuration object allowing method chaining.</returns>
139+
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
54140
public static LoggerConfiguration File(
55141
this LoggerSinkConfiguration sinkConfiguration,
56142
string path,
@@ -61,14 +147,16 @@ public static LoggerConfiguration File(
61147
LoggingLevelSwitch levelSwitch = null,
62148
bool buffered = false,
63149
bool shared = false,
64-
TimeSpan? flushToDiskInterval = null)
150+
TimeSpan? flushToDiskInterval = null,
151+
RollingInterval rollingInterval = RollingInterval.Infinite,
152+
bool rollOnFileSizeLimit = false)
65153
{
66154
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
67155
if (path == null) throw new ArgumentNullException(nameof(path));
68156
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
69157

70158
var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
71-
return File(sinkConfiguration, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes, levelSwitch, buffered: buffered, shared: shared, flushToDiskInterval: flushToDiskInterval);
159+
return File(sinkConfiguration, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes, levelSwitch, buffered: buffered, shared: shared, flushToDiskInterval: flushToDiskInterval, rollingInterval: rollingInterval, rollOnFileSizeLimit: rollOnFileSizeLimit);
72160
}
73161

74162
/// <summary>
@@ -77,7 +165,7 @@ public static LoggerConfiguration File(
77165
/// <param name="sinkConfiguration">Logger sink configuration.</param>
78166
/// <param name="formatter">A formatter, such as <see cref="JsonFormatter"/>, to convert the log events into
79167
/// text for the file. If control of regular text formatting is required, use the other
80-
/// overload of <see cref="File(LoggerSinkConfiguration, string, LogEventLevel, string, IFormatProvider, long?, LoggingLevelSwitch, bool, bool, TimeSpan?)"/>
168+
/// overload of <see cref="File(LoggerSinkConfiguration, string, LogEventLevel, string, IFormatProvider, long?, LoggingLevelSwitch, bool, bool, TimeSpan?, RollingInterval, bool)"/>
81169
/// and specify the outputTemplate parameter instead.
82170
/// </param>
83171
/// <param name="path">Path to the file.</param>
@@ -92,6 +180,9 @@ public static LoggerConfiguration File(
92180
/// is false.</param>
93181
/// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
94182
/// <param name="flushToDiskInterval">If provided, a full disk flush will be performed periodically at the specified interval.</param>
183+
/// <param name="rollingInterval">The interval at which logging will roll over to a new file.</param>
184+
/// <param name="rollOnFileSizeLimit">If <code>true</code>, a new file will be created when the file size limit is reached. Filenames
185+
/// will have a number appended in the format <code>_NNNNN</code>, with the first filename given no number.</param>
95186
/// <returns>Configuration object allowing method chaining.</returns>
96187
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
97188
public static LoggerConfiguration File(
@@ -103,9 +194,11 @@ public static LoggerConfiguration File(
103194
LoggingLevelSwitch levelSwitch = null,
104195
bool buffered = false,
105196
bool shared = false,
106-
TimeSpan? flushToDiskInterval = null)
197+
TimeSpan? flushToDiskInterval = null,
198+
RollingInterval rollingInterval = RollingInterval.Infinite,
199+
bool rollOnFileSizeLimit = false)
107200
{
108-
return ConfigureFile(sinkConfiguration.Sink, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes, levelSwitch, buffered: buffered, shared: shared, flushToDiskInterval: flushToDiskInterval);
201+
return ConfigureFile(sinkConfiguration.Sink, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes, levelSwitch, buffered: buffered, shared: shared, flushToDiskInterval: flushToDiskInterval, rollingInterval: rollingInterval, rollOnFileSizeLimit: rollOnFileSizeLimit);
109202
}
110203

111204
/// <summary>
@@ -119,7 +212,7 @@ public static LoggerConfiguration File(
119212
/// to be changed at runtime.</param>
120213
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
121214
/// <param name="outputTemplate">A message template describing the format used to write to the sink.
122-
/// the default is "{Timestamp} [{Level}] {Message}{NewLine}{Exception}".</param>
215+
/// the default is "[{Timestamp:o} {Level:u3}] {Message:lj} {Properties}{NewLine}{Exception}".</param>
123216
/// <returns>Configuration object allowing method chaining.</returns>
124217
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
125218
public static LoggerConfiguration File(
@@ -174,7 +267,9 @@ static LoggerConfiguration ConfigureFile(
174267
bool buffered = false,
175268
bool propagateExceptions = false,
176269
bool shared = false,
177-
TimeSpan? flushToDiskInterval = null)
270+
TimeSpan? flushToDiskInterval = null,
271+
RollingInterval rollingInterval = RollingInterval.Infinite,
272+
bool rollOnFileSizeLimit = false)
178273
{
179274
if (addSink == null) throw new ArgumentNullException(nameof(addSink));
180275
if (formatter == null) throw new ArgumentNullException(nameof(formatter));
@@ -212,5 +307,5 @@ static LoggerConfiguration ConfigureFile(
212307

213308
return addSink(sink, restrictedToMinimumLevel, levelSwitch);
214309
}
215-
}
310+
}
216311
}

0 commit comments

Comments
 (0)