trace<T> method
Runs a function with a trace. This is useful for wrapping functions that
need to be traced. It can also be part of a nested trace, using the parentTrace
parameter.
Implementation
Future<T?> trace<T>({
required String name,
required String operation,
required FutureOr<T?> Function(Trace? parentTrace) fn,
Trace? parentTrace,
LogLevel? level = LogLevel.info,
}) async {
final trace = parentTrace != null
? await parentTrace.startChild(name, operation, level: level)
: await startTrace(name, operation, level: level);
try {
final value = fn(trace);
if (value is Future) {
return await value;
}
return value;
} catch (ex, stackTrace) {
reportError(ex, stackTrace: stackTrace);
rethrow;
} finally {
await trace.stop();
}
}