Sanitize build output before writing to database.

Build output must also be sanitized when writing to database. If the
database has connection encoding configured to utf-8 (as it should)
updating the trace attribute with non utf-8 data will cause an
exception in runner. This will leave the build in a stuck running
state, even if the process terminates some time after. This is a
separate bug that needs to be addressed in a different commit.