@@ -94,27 +94,97 @@ public static LoggerConfiguration File(
9494 long ? fileSizeLimitBytes = DefaultFileSizeLimitBytes ,
9595 LoggingLevelSwitch levelSwitch = null ,
9696 bool buffered = false )
97+ {
98+ return ConfigureFile ( sinkConfiguration . Sink , formatter , path , restrictedToMinimumLevel , fileSizeLimitBytes , levelSwitch , buffered ) ;
99+ }
100+
101+ /// <summary>
102+ /// Write log events to the specified file.
103+ /// </summary>
104+ /// <param name="sinkConfiguration">Logger sink configuration.</param>
105+ /// <param name="path">Path to the file.</param>
106+ /// <param name="restrictedToMinimumLevel">The minimum level for
107+ /// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
108+ /// <param name="levelSwitch">A switch allowing the pass-through minimum level
109+ /// to be changed at runtime.</param>
110+ /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
111+ /// <param name="outputTemplate">A message template describing the format used to write to the sink.
112+ /// the default is "{Timestamp} [{Level}] {Message}{NewLine}{Exception}".</param>
113+ /// <returns>Configuration object allowing method chaining.</returns>
114+ /// <remarks>The file will be written using the UTF-8 character set.</remarks>
115+ public static LoggerConfiguration File (
116+ this LoggerAuditSinkConfiguration sinkConfiguration ,
117+ string path ,
118+ LogEventLevel restrictedToMinimumLevel = LevelAlias . Minimum ,
119+ string outputTemplate = DefaultOutputTemplate ,
120+ IFormatProvider formatProvider = null ,
121+ LoggingLevelSwitch levelSwitch = null )
97122 {
98123 if ( sinkConfiguration == null ) throw new ArgumentNullException ( nameof ( sinkConfiguration ) ) ;
124+ if ( path == null ) throw new ArgumentNullException ( nameof ( path ) ) ;
125+ if ( outputTemplate == null ) throw new ArgumentNullException ( nameof ( outputTemplate ) ) ;
126+
127+ var formatter = new MessageTemplateTextFormatter ( outputTemplate , formatProvider ) ;
128+ return File ( sinkConfiguration , formatter , path , restrictedToMinimumLevel , levelSwitch ) ;
129+ }
130+
131+ /// <summary>
132+ /// Write log events to the specified file.
133+ /// </summary>
134+ /// <param name="sinkConfiguration">Logger sink configuration.</param>
135+ /// <param name="formatter">A formatter, such as <see cref="JsonFormatter"/>, to convert the log events into
136+ /// text for the file. If control of regular text formatting is required, use the other
137+ /// overload of <see cref="File(LoggerSinkConfiguration, string, LogEventLevel, string, IFormatProvider, long?, LoggingLevelSwitch, bool)"/>
138+ /// and specify the outputTemplate parameter instead.
139+ /// </param>
140+ /// <param name="path">Path to the file.</param>
141+ /// <param name="restrictedToMinimumLevel">The minimum level for
142+ /// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
143+ /// <param name="levelSwitch">A switch allowing the pass-through minimum level
144+ /// to be changed at runtime.</param>
145+ /// <returns>Configuration object allowing method chaining.</returns>
146+ /// <remarks>The file will be written using the UTF-8 character set.</remarks>
147+ public static LoggerConfiguration File (
148+ this LoggerAuditSinkConfiguration sinkConfiguration ,
149+ ITextFormatter formatter ,
150+ string path ,
151+ LogEventLevel restrictedToMinimumLevel = LevelAlias . Minimum ,
152+ LoggingLevelSwitch levelSwitch = null )
153+ {
154+ return ConfigureFile ( sinkConfiguration . Sink , formatter , path , restrictedToMinimumLevel , null , levelSwitch , false , true ) ;
155+ }
156+
157+ static LoggerConfiguration ConfigureFile (
158+ this Func < ILogEventSink , LogEventLevel , LoggingLevelSwitch , LoggerConfiguration > addSink ,
159+ ITextFormatter formatter ,
160+ string path ,
161+ LogEventLevel restrictedToMinimumLevel = LevelAlias . Minimum ,
162+ long ? fileSizeLimitBytes = DefaultFileSizeLimitBytes ,
163+ LoggingLevelSwitch levelSwitch = null ,
164+ bool buffered = false ,
165+ bool propagateExceptions = false )
166+ {
167+ if ( addSink == null ) throw new ArgumentNullException ( nameof ( addSink ) ) ;
99168 if ( formatter == null ) throw new ArgumentNullException ( nameof ( formatter ) ) ;
100169 if ( path == null ) throw new ArgumentNullException ( nameof ( path ) ) ;
170+ if ( fileSizeLimitBytes . HasValue && fileSizeLimitBytes < 0 ) throw new ArgumentException ( "Negative value provided; file size limit must be non-negative" ) ;
101171
102172 FileSink sink ;
103173 try
104174 {
105175 sink = new FileSink ( path , formatter , fileSizeLimitBytes , buffered : buffered ) ;
106176 }
107- catch ( ArgumentException )
108- {
109- throw ;
110- }
111177 catch ( Exception ex )
112178 {
113179 SelfLog . WriteLine ( "Unable to open file sink for {0}: {1}" , path , ex ) ;
114- return sinkConfiguration . Sink ( new NullSink ( ) ) ;
180+
181+ if ( propagateExceptions )
182+ throw ;
183+
184+ return addSink ( new NullSink ( ) , LevelAlias . Maximum , null ) ;
115185 }
116186
117- return sinkConfiguration . Sink ( sink , restrictedToMinimumLevel , levelSwitch ) ;
187+ return addSink ( sink , restrictedToMinimumLevel , levelSwitch ) ;
118188 }
119189 }
120190}
0 commit comments