@@ -69,7 +69,7 @@ public enum Properties
6969 readonly LogEventLevel _abandonmentLevel ;
7070 readonly TimeSpan ? _warningThreshold ;
7171 Exception ? _exception ;
72-
72+
7373 internal Operation ( ILogger target , string messageTemplate , object [ ] args ,
7474 CompletionBehaviour completionBehaviour , LogEventLevel completionLevel , LogEventLevel abandonmentLevel ,
7575 TimeSpan ? warningThreshold = null )
@@ -98,6 +98,7 @@ static long GetTimestamp()
9898 /// <param name="args">Arguments to the log message. These will be stored and captured only when the
9999 /// operation completes, so do not pass arguments that are mutated during the operation.</param>
100100 /// <returns>An <see cref="Operation"/> object.</returns>
101+ [ MessageTemplateFormatMethod ( "messageTemplate" ) ]
101102 public static Operation Begin ( string messageTemplate , params object [ ] args )
102103 {
103104 return Log . Logger . BeginOperation ( messageTemplate , args ) ;
@@ -110,6 +111,7 @@ public static Operation Begin(string messageTemplate, params object[] args)
110111 /// <param name="args">Arguments to the log message. These will be stored and captured only when the
111112 /// operation completes, so do not pass arguments that are mutated during the operation.</param>
112113 /// <returns>An <see cref="Operation"/> object.</returns>
114+ [ MessageTemplateFormatMethod ( "messageTemplate" ) ]
113115 public static IDisposable Time ( string messageTemplate , params object [ ] args )
114116 {
115117 return Log . Logger . TimeOperation ( messageTemplate , args ) ;
@@ -147,7 +149,7 @@ public TimeSpan Elapsed
147149 // (HAL) on machines with variable-speed CPUs (e.g. Intel SpeedStep).
148150 return TimeSpan . Zero ;
149151 }
150-
152+
151153 return TimeSpan . FromTicks ( elapsedTicks ) ;
152154 }
153155 }
@@ -163,6 +165,20 @@ public void Complete()
163165 Write ( _target , _completionLevel , OutcomeCompleted ) ;
164166 }
165167
168+ /// <summary>
169+ /// Complete the timed operation with the given Log Event level. This will write the event and elapsed time to the log.
170+ /// </summary>
171+ /// <param name="level">The log event level with which the complete operation will be logged</param>
172+
173+ public void Complete ( LogEventLevel level )
174+ {
175+ if ( _completionBehaviour == CompletionBehaviour . Silent )
176+ return ;
177+
178+ Write ( _target , level , OutcomeCompleted ) ;
179+ }
180+
181+
166182 /// <summary>
167183 /// Complete the timed operation with an included result value.
168184 /// </summary>
@@ -179,6 +195,23 @@ public void Complete(string resultPropertyName, object result, bool destructureO
179195 Write ( _target . ForContext ( resultPropertyName , result , destructureObjects ) , _completionLevel , OutcomeCompleted ) ;
180196 }
181197
198+ /// <summary>
199+ /// Complete the timed operation with an included result value and log event level.
200+ /// </summary>
201+ /// <param name="level">The log event level with which the complete operation will be logged</param>
202+ /// <param name="resultPropertyName">The name for the property to attach to the event.</param>
203+ /// <param name="result">The result value.</param>
204+ /// <param name="destructureObjects">If true, the property value will be destructured (serialized).</param>
205+ public void Complete ( string resultPropertyName , object result , LogEventLevel level , bool destructureObjects = false )
206+ {
207+ if ( resultPropertyName == null ) throw new ArgumentNullException ( nameof ( resultPropertyName ) ) ;
208+
209+ if ( _completionBehaviour == CompletionBehaviour . Silent )
210+ return ;
211+
212+ Write ( _target . ForContext ( resultPropertyName , result , destructureObjects ) , level , OutcomeCompleted ) ;
213+ }
214+
182215 /// <summary>
183216 /// Abandon the timed operation. This will write the event and elapsed time to the log.
184217 /// </summary>
@@ -243,10 +276,10 @@ void Write(ILogger target, LogEventLevel level, string outcome)
243276 _completionBehaviour = CompletionBehaviour . Silent ;
244277
245278 var elapsed = Elapsed . TotalMilliseconds ;
246-
279+
247280 level = elapsed > _warningThreshold ? . TotalMilliseconds && level < LogEventLevel . Warning
248281 ? LogEventLevel . Warning
249- : level ;
282+ : level ;
250283
251284 target . Write ( level , _exception , $ "{ _messageTemplate } {{{nameof(Properties.Outcome)}}} in {{{nameof(Properties.Elapsed)}:0.0}} ms", _args . Concat ( new object [ ] { outcome , elapsed } ) . ToArray ( ) ) ;
252285
0 commit comments