@@ -23,7 +23,6 @@ namespace Microsoft.Azure.WebJobs.Script.Description
23
23
{
24
24
public abstract class FunctionInvokerBase : IFunctionInvoker , IDisposable
25
25
{
26
- private readonly Stopwatch _stopwatch = new Stopwatch ( ) ;
27
26
private bool _disposed = false ;
28
27
private IMetricsLogger _metrics ;
29
28
private IDisposable _fileChangeSubscription ;
@@ -126,7 +125,8 @@ public async Task Invoke(object[] parameters)
126
125
var startedEvent = new FunctionStartedEvent ( functionExecutionContext . InvocationId , Metadata ) ;
127
126
_metrics . BeginEvent ( startedEvent ) ;
128
127
var invokeLatencyEvent = LogInvocationMetrics ( _metrics , Metadata ) ;
129
- _stopwatch . Restart ( ) ;
128
+ var invocationStopWatch = new Stopwatch ( ) ;
129
+ invocationStopWatch . Start ( ) ;
130
130
131
131
try
132
132
{
@@ -144,11 +144,8 @@ public async Task Invoke(object[] parameters)
144
144
145
145
await InvokeCore ( parameters , context ) ;
146
146
147
- _stopwatch . Stop ( ) ;
148
-
149
- string completeMessage = $ "Function completed (Success, Id={ invocationId } , Duration={ _stopwatch . ElapsedMilliseconds } ms)";
150
- TraceWriter . Info ( completeMessage ) ;
151
- Logger ? . LogInformation ( completeMessage ) ;
147
+ invocationStopWatch . Stop ( ) ;
148
+ LogFunctionResult ( startedEvent , true , invocationId , invocationStopWatch . ElapsedMilliseconds ) ;
152
149
}
153
150
catch ( AggregateException ex )
154
151
{
@@ -165,14 +162,14 @@ public async Task Invoke(object[] parameters)
165
162
exInfo = ExceptionDispatchInfo . Capture ( ex ) ;
166
163
}
167
164
168
- _stopwatch . Stop ( ) ;
169
- LogFunctionFailed ( startedEvent , "Failure" , invocationId , _stopwatch . ElapsedMilliseconds ) ;
165
+ invocationStopWatch . Stop ( ) ;
166
+ LogFunctionResult ( startedEvent , false , invocationId , invocationStopWatch . ElapsedMilliseconds ) ;
170
167
exInfo . Throw ( ) ;
171
168
}
172
169
catch
173
170
{
174
- _stopwatch . Stop ( ) ;
175
- LogFunctionFailed ( startedEvent , "Failure" , invocationId , _stopwatch . ElapsedMilliseconds ) ;
171
+ invocationStopWatch . Stop ( ) ;
172
+ LogFunctionResult ( startedEvent , false , invocationId , invocationStopWatch . ElapsedMilliseconds ) ;
176
173
throw ;
177
174
}
178
175
finally
@@ -202,16 +199,21 @@ internal static object LogInvocationMetrics(IMetricsLogger metrics, FunctionMeta
202
199
return metrics . BeginEvent ( MetricEventNames . FunctionInvokeLatency , metadata . Name ) ;
203
200
}
204
201
205
- private void LogFunctionFailed ( FunctionStartedEvent startedEvent , string resultString , string invocationId , long elapsedMs )
202
+ private void LogFunctionResult ( FunctionStartedEvent startedEvent , bool success , string invocationId , long elapsedMs )
206
203
{
207
204
if ( startedEvent != null )
208
205
{
209
- startedEvent . Success = false ;
206
+ startedEvent . Success = success ;
210
207
}
211
208
209
+ string resultString = success ? "Success" : "Failure" ;
212
210
string message = $ "Function completed ({ resultString } , Id={ invocationId ?? "0" } , Duration={ elapsedMs } ms)";
213
- TraceWriter . Error ( message ) ;
214
- Logger ? . LogError ( message ) ;
211
+
212
+ TraceLevel traceWriterLevel = success ? TraceLevel . Info : TraceLevel . Error ;
213
+ LogLevel logLevel = success ? LogLevel . Information : LogLevel . Error ;
214
+
215
+ TraceWriter . Trace ( message , traceWriterLevel , null ) ;
216
+ Logger ? . Log ( logLevel , new EventId ( 0 ) , message , null , ( s , e ) => s ) ;
215
217
}
216
218
217
219
protected TraceWriter CreateUserTraceWriter ( TraceWriter traceWriter )
0 commit comments