os_log NSURLSession, background upload and missing logs

I'm using NSURLSession to do a background upload of multiple files. It seems to be working fine but I still have a few things to troubleshoot. My issue is around os_log usage for this specific scenario.

I start an upload in the release build of my app on a physical device, connect the console app and then send the app to the background and switch my screen phone off.

The upload is on an unstable network connection, so I expect the uploads to run overnight with retries and all kind of good things

When I come back in the morning there is no trace of any of the overnight logs. The uploads have clearly kept on going and the events have been processed as I can see in the UI the status of the updates and that my retry logic has kicked in.

Essentially it seems that as soon as the app is sent to the background os_log doesn't show up in the console. Never to be seen again even when I start the app again.


What am I missing about viewing those logs. Do I have to also write to a text file to make sure they don't get lost?

What log type are you using? The two lowest log types, OS_LOG_TYPE_INFO and OS_LOG_TYPE_DEBUG, are volatile, meaning that they won’t be written to disk. On macOS you can use the log command to tweak that default but that option isn’t available on iOS.

Adding logging to your NSURLSession background session code is a delicate balancing act. On the one hand, you need to have enough logging to understand problems coming in from the field. On the other hand, too much persistent logging is actively bad for the system.

For more hints and tips, see Testing Background Session Code.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
Thanks for your response @eskimo. I'm indeed using info and debug... but also error, when I get an error. And even those don't seem to appear once the app is sent to the background.


I have your page bookmarked about the testing of background session code.

Thanks!

And even those don't seem to appear once the app is sent to the
background.

OK, there’s definitely something wonky going on there. Log entries of type OS_LOG_TYPE_ERROR are persisted to disk and should stay around for a while. There’s a relatively straightforward way to test that:
  1. Create a new app that, on moving into the background, starts a UIApplication background task and then, after 10 seconds or so, logs an entry of type OS_LOG_TYPE_ERROR.

  2. Run that from the Home screen.

  3. Move it to the background.

  4. Screen lock your device and leave it overnight.

  5. In the morning, unlock your device.

  6. Trigger a sysdiagnose log and extract the system log from that.

  7. Look for your log entry.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
os_log NSURLSession, background upload and missing logs
 
 
Q