Adding logging to a SwiftUI View's body var

Pretty sure this is a no-no, but asking just in case there's an easy way to make this work

struct DocumentContentView: View {
    private static let logger = Logger(
        subsystem: "mySubsystem",
        category: String(describing: Self.self)
    )
    var body: some View {
        VStack {
            Text("Hello")
            logger.trace("hello")
        }
    }
}

This code generates the following compile error at the logger.trace line buildExpression is unavailable: this expression does not conform to View

I suspect every line of the body var (or any @ViewBuilder - designated code?) needs to 'return' a View. Is this correct? or more importantly any work arounds other than putting some/all of the view contents in a. func()?

fyi, I know that I can do something like:

    func emptyView() -> EmptyView {
        Self.logger.debug("hello")
        return EmptyView()
    }

But it just feels like a contrived way to have to add logging.

You can use let _ = to make this work, example:

var body: some View {
    VStack {
        Text("Hello")
        let _ = Self.logger.trace("hello")
    }
}
Adding logging to a SwiftUI View's body var
 
 
Q