This is Xamarin's bug tracking system. For product support, please use the support links listed in your Xamarin Account.
Bug 52308 - Console.WriteLine text is not showing in Device Log.
Summary: Console.WriteLine text is not showing in Device Log.
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll (show other bugs)
Version: XI 10.4 (C9)
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Future Cycle (TBD)
Assignee: Alex Soto
URL:
Depends on:
Blocks:
 
Reported: 2017-02-03 22:54 UTC by Jon Goldberger
Modified: 2017-05-05 06:04 UTC (History)
8 users (show)

See Also:
Tags:
Is this bug a regression?: Yes
Last known good build: XI 10.2.1.5


Attachments
Test Project (12.43 KB, application/zip)
2017-02-03 22:54 UTC, Jon Goldberger
Details

Description Jon Goldberger 2017-02-03 22:54:35 UTC
Created attachment 19721 [details]
Test Project

## Description

When using Console.WriteLine in an app deployed to a device, the message shows in Application Output, but in the iOS Device Log (whether using VS or XS) the tag is shown but not the text of the message. E.g. I see this in the Device Log (both in the IDE Device Log pane and in XCode Device Log):

In XCode Device Log:
> Feb  3 12:19:26 Johnny-Golds-iPhone-6s TestConsoleWriteLine(libxamarin-debug.dylib)[2214] <Notice>: 

In IDE Device Log pane:
> Info (2214) / TestConsoleWriteLine(libxamarin-debug.dylib): 

What should be shown is:
XCode:
> Feb  3 12:19:26 Johnny-Golds-iPhone-6s TestConsoleWriteLine(libxamarin-debug.dylib)[2214] <Notice>: ************* Test ***************

IDE Device Log:
> Info (2214) / TestConsoleWriteLine(libxamarin-debug.dylib): ************* Test ***************


## Steps to reproduce

1. Load the attached test project.

2. Deploy to a device.

3. Open the XCode Device Log window for the device and/or open the Device Log pane in the IDE and select the device.

4. Clear the log and then quickly press the button on the app (so the log statement will be towards the top and easy to find among all the OS noise).

Expected result: Log will display:
XCode:
> Feb  3 12:19:26 Johnny-Golds-iPhone-6s TestConsoleWriteLine(libxamarin-debug.dylib)[2214] <Notice>: ************* Test ***************
IDE Device Log:
> Info (2214) / TestConsoleWriteLine(libxamarin-debug.dylib): ************* Test ***************

Actual result: Log displays:
In XCode Device Log:
> Feb  3 12:19:26 Johnny-Golds-iPhone-6s TestConsoleWriteLine(libxamarin-debug.dylib)[2214] <Notice>: 
In IDE Device Log pane:
> Info (2214) / TestConsoleWriteLine(libxamarin-debug.dylib): 


## Regression status:

This is a regression:

BAD: XI 10.4.0.97
BAD: XI 10.3.1.8
GOOD: XI 10.2.1.5

## Notes

Using XCode and writing an NSLog, the message is printed to the device log as expected, so this does not seem to be an Apple issue. 

## Environment:

=== Xamarin Studio Enterprise ===

Version 6.2 (build 1798)
Installation UUID: ceaba76c-db06-4fbd-b326-f69ea53c3e01
Runtime:
	Mono 4.8.0 (mono-4.8.0-branch/084f912) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 408000478

=== NuGet ===

Version: 3.5.0.0

=== Xamarin.Profiler ===

Version: 1.1.7
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Apple Developer Tools ===

Xcode 8.2.1 (11766.1)
Build 8C1002

=== Xamarin.Mac ===

Version: 3.0.0.367 (Visual Studio Enterprise)

=== Xamarin Inspector ===

Version: 1.0.0.0
Hash: 1f3067d
Branch: master
Build date: Tue, 15 Nov 2016 21:13:59 GMT

=== Xamarin.Android ===

Version: 7.1.0.30 (Visual Studio Enterprise)
Android SDK: /Users/jongoldberger/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.3   (API level 18)
		4.4   (API level 19)
		5.0   (API level 21)
		5.1   (API level 22)
		6.0   (API level 23)
		7.0   (API level 24)

SDK Tools Version: 25.2.3
SDK Platform Tools Version: 25.0.1
SDK Build Tools Version: 25.0.1

Java SDK: /usr
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)

Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Xamarin Android Player ===

Version: 0.6.5
Location: /Applications/Xamarin Android Player.app

=== Xamarin.iOS ===

Version: 10.4.0.97 (Visual Studio Enterprise)
Hash: 2bcf787
Branch: cycle9
Build date: 2017-01-25 12:40:52-0500

=== Build Information ===

Release ID: 602001798
Git revision: 79fea1cb975eace20d5316687ebb9ff687b51451
Build date: 2017-01-27 07:34:26-05
Xamarin addins: 8e37b5263f1583e25ed2eda77f84829dfcf42759
Build lane: monodevelop-lion-cycle9

=== Operating System ===

Mac OS X 10.12.3
Darwin Jons-MacBook-Pro.local 16.4.0 Darwin Kernel Version 16.4.0
    Thu Dec 22 22:53:21 PST 2016
    root:xnu-3789.41.3~3/RELEASE_X86_64 x86_64

=== Enabled user installed addins ===

Addin Maker 1.3.2
Prism Template Pack 1.5
Manifest.addin 0.0.0.0
Comment 1 Jon Goldberger 2017-02-03 22:55:42 UTC
This was initially reported in SO post: http://stackoverflow.com/questions/41932322/xamarin-ios10-no-message-in-logging?noredirect=1#comment71171482_41932322

Setting as confirmed as I could reproduce the issue independently.
Comment 2 Nate Cook 2017-02-17 21:55:53 UTC
Yes, this is exactly what I'm seeing as well.
Comment 3 Nate Cook 2017-02-18 00:39:34 UTC
Just so that we're clear here, the problem only occurs on iOS 10. Test Cloud engineers can look at the device logs of the following test cloud run, which is for two devices (one iOS 10 and one iOS 9)

https://testcloud.xamarin.com/test/4fdc941c-296c-4acf-a1f2-c8f27f5b1e56

The app's log output contains the app's Console.WriteLine output on iOS 9, but not on iOS 10.

Is there any way we can just get Console.WriteLine to bind to printf instead of NSLog?
Comment 4 Alex Soto 2017-03-03 07:12:42 UTC
PR: https://github.com/xamarin/xamarin-macios/pull/1821
Comment 5 Alex Soto 2017-03-03 20:39:26 UTC
Merged PR: https://github.com/xamarin/xamarin-macios/pull/1821
Comment 6 Jon Goldberger 2017-03-22 01:37:54 UTC
Workaround until fix is released:

P/Invoke into NSLog:

>[DllImport("/System/Library/Frameworks/Foundation.framework/Foundation")]
>public extern static void NSLog(IntPtr format, IntPtr arg1);

And then call it with:

>NSString p1 = new NSString("%@"); // the format string.
>NSString p2 = new NSString("Hello NSLog!"); // your log message
>NSLog(p1.Handle, p2.Handle);
Comment 7 Nate Cook 2017-03-22 17:23:30 UTC
Hi Jon,

I tried that workaround before and it worked only on a simulator, not on a device (iPhone 6S). On a device I get a SIGSEGV. Tried it again just now:

critical: Stacktrace:

critical:   at <unknown> <0xffffffff>
critical:   at (wrapper managed-to-native) FooMobile.Logger.NSLog (intptr,intptr) <0x00007>
critical:   at FooMobile.Logger.OutputStringToConsole (string) [0x00032] in /Users/ncook/Foo/mobile/FooMobile.Core/Logger.cs:100
critical:   at FooMobile.Logger.Debug (string) [0x00002] in /Users/ncook/Foo/mobile/FooMobile.Core/Logger.cs:28
critical:   at FooMobile.DL.FooDatabase..cctor () [0x00024] in /Users/ncook/Foo/mobile/FooMobile.Core/DL/FooDatabase.cs:31
critical:   at (wrapper runtime-invoke) object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr) [0x0001e] in <54e129cad3ee4c61a6b46c3680ddc69a#D2F73BB0-AA2C-AC36-5BB6-3A298210327D>:0
critical:   at <unknown> <0xffffffff>
critical:   at FooMobile.Offline.CloudStorageUpdateQueue.get_IsEmpty () [0x00001] in /Users/ncook/Foo/mobile/FooMobile.Core/Offline/UpdateQueue/CloudStorageUpdateQueue.cs:100
critical:   at FooMobile.Offline.CloudRecordStorage..ctor () [0x00007] in /Users/ncook/Foo/mobile/FooMobile.Core/Offline/CloudRecordStorage.cs:61
critical:   at FooMobile.Offline.CloudRecordStorage..cctor () [0x00000] in /Users/ncook/Foo/mobile/FooMobile.Core/Offline/CloudRecordStorage.cs:52
critical:   at (wrapper runtime-invoke) object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr) [0x0001e] in <54e129cad3ee4c61a6b46c3680ddc69a#D2F73BB0-AA2C-AC36-5BB6-3A298210327D>:0
critical:   at <unknown> <0xffffffff>
critical:   at FooMobile.iOS.AppDelegate.RegisterServices () [0x0026e] in /Users/ncook/Foo/mobile/FooMobile.iOS/AppDelegate.cs:424
critical:   at FooMobile.iOS.AppDelegate.Initialize () [0x0001d] in /Users/ncook/Foo/mobile/FooMobile.iOS/AppDelegate.cs:119
critical:   at FooMobile.iOS.AppDelegate.InitInstance () [0x00008] in /Users/ncook/Foo/mobile/FooMobile.iOS/AppDelegate.cs:104
critical:   at FooMobile.iOS.AppDelegate..ctor () [0x00016] in /Users/ncook/Foo/mobile/FooMobile.iOS/AppDelegate.cs:87
critical:   at (wrapper runtime-invoke) object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr) [0x0001e] in <54e129cad3ee4c61a6b46c3680ddc69a#D2F73BB0-AA2C-AC36-5BB6-3A298210327D>:0
critical:   at <unknown> <0xffffffff>
critical:   at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <0x00007>
critical:   at UIKit.UIApplication.Main (string[],intptr,intptr) [0x00005] in /Users/builder/data/lanes/3985/ba11e481/source/xamarin-macios/src/UIKit/UIApplication.cs:79
critical:   at UIKit.UIApplication.Main (string[],string,string) [0x00038] in /Users/builder/data/lanes/3985/ba11e481/source/xamarin-macios/src/UIKit/UIApplication.cs:63
critical:   at FooMobile.iOS.Application.Main (string[]) [0x00008] in /Users/ncook/Foo/mobile/FooMobile.iOS/Main.cs:17
critical:   at (wrapper runtime-invoke) object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr) [0x0001e] in <54e129cad3ee4c61a6b46c3680ddc69a#D2F73BB0-AA2C-AC36-5BB6-3A298210327D>:0
critical: 
Native stacktrace:

0   Mono                                0x00000001049ed158 mono_handle_native_sigsegv + 260
1   Mono                                0x00000001049f88a0 mono_sigsegv_signal_handler + 336
2   libsystem_platform.dylib            0x000000018c595348 _sigtramp + 52
3   Foundation                          0x000000018dff8ba4 _NS_os_log_callback + 68
4   libsystem_trace.dylib               0x000000018c5bf954 <redacted> + 112
5   libsystem_trace.dylib               0x000000018c5bf564 <redacted> + 736
6   libsystem_trace.dylib               0x000000018c5bffb8 _os_log_encode + 1036
7   libsystem_trace.dylib               0x000000018c5c30a4 os_log_with_args + 544
8   libsystem_trace.dylib               0x000000018c5c349c os_log_shim_with_CFString + 172
9   CoreFoundation                      0x000000018d4e8de4 _CFLogvEx3 + 152
10  Foundation                          0x000000018dff9cb0 <redacted> + 132
11  Foundation                          0x000000018df20b3c NSLog + 32
12  FooMobileiOS                        0x00000001011993f0 wrapper_managed_to_native_FooMobile_Logger_NSLog_intptr_intptr + 208
13  FooMobileiOS                        0x000000010054bf84 FooMobile_Logger_OutputStringToConsole_string + 564
14  FooMobileiOS                        0x000000010054b5c4 FooMobile_Logger_Debug_string + 132
15  FooMobileiOS                        0x000000010052c464 FooMobile_DL_FooDatabase__cctor + 388
16  FooMobileiOS                        0x00000001015b3af4 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 244
17  Mono                                0x00000001049fbabc mono_jit_runtime_invoke + 1776
18  Mono                                0x0000000104a75b34 do_runtime_invoke + 112
19  Mono                                0x0000000104a72d40 mono_runtime_class_init_full + 936
20  Mono                                0x00000001049f7fb0 mono_jit_compile_method_with_opt + 500
21  Mono                                0x00000001049fdf9c common_call_trampoline + 944
22  Mono                                0x00000001049fdbb8 mono_magic_trampoline + 88
23  FooMobileiOS                        0x00000001015e4d9c generic_trampoline_jit + 252
24  FooMobileiOS                        0x00000001007ed708 FooMobile_Offline_CloudStorageUpdateQueue_get_IsEmpty + 120
25  FooMobileiOS                        0x00000001007f5b20 FooMobile_Offline_CloudRecordStorage__ctor + 128
26  FooMobileiOS                        0x00000001007fde48 FooMobile_Offline_CloudRecordStorage__cctor + 104
27  FooMobileiOS                        0x00000001015b3af4 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 244
28  Mono                                0x00000001049fbabc mono_jit_runtime_invoke + 1776
29  Mono                                0x0000000104a75b34 do_runtime_invoke + 112
30  Mono                                0x0000000104a72d40 mono_runtime_class_init_full + 936
31  Mono                                0x00000001049f7884 mono_resolve_patch_target + 1612
32  Mono                                0x00000001049c1cbc init_method + 1196
33  Mono                                0x00000001049bb588 load_method + 1660
34  Mono                                0x00000001049baca4 mono_aot_get_method_checked + 2084
35  Mono                                0x00000001049f7f88 mono_jit_compile_method_with_opt + 460
36  Mono                                0x00000001049fdf9c common_call_trampoline + 944
37  Mono                                0x00000001049fdbb8 mono_magic_trampoline + 88
38  FooMobileiOS                        0x00000001015e4d9c generic_trampoline_jit + 252
39  FooMobileiOS                        0x0000000100250870 FooMobile_iOS_AppDelegate_RegisterServices + 3840
40  FooMobileiOS                        0x000000010024cc90 FooMobile_iOS_AppDelegate_Initialize + 240
41  FooMobileiOS                        0x000000010024cb70 FooMobile_iOS_AppDelegate_InitInstance + 176
42  FooMobileiOS                        0x000000010024c098 FooMobile_iOS_AppDelegate__ctor + 216
43  FooMobileiOS                        0x00000001015b3af4 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 244
44  Mono                                0x00000001049fbabc mono_jit_runtime_invoke + 1776
45  Mono                                0x0000000104a75b34 do_runtime_invoke + 112
46  Mono                                0x0000000104a75a90 mono_runtime_invoke + 208
47  FooMobileiOS                        0x0000000101e5030c _Z30native_to_managed_trampoline_4P11objc_objectP13objc_selectorPP11_MonoMethodPKcS7_Pb + 324
48  FooMobileiOS                        0x0000000101e739a4 -[AppDelegate init] + 68
49  UIKit                               0x00000001936445a4 <redacted> + 1880
50  UIKit                               0x0000000193420500 UIApplicationMain + 156
51  FooMobileiOS                        0x00000001016f0894 wrapper_managed_to_native_UIKit_UIApplication_UIApplicationMain_int_string___intptr_intptr + 340
52  FooMobileiOS                        0x000000010169204c UIKit_UIApplication_Main_string___intptr_intptr + 44
53  FooMobileiOS                        0x000000010169200c UIKit_UIApplication_Main_string___string_string + 172
54  FooMobileiOS                        0x000000010024bf90 FooMobile_iOS_Application_Main_string__ + 160
55  FooMobileiOS                        0x00000001015b3af4 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 244
56  Mono                                0x00000001049fbabc mono_jit_runtime_invoke + 1776
57  Mono                                0x0000000104a75b34 do_runtime_invoke + 112
58  Mono                                0x0000000104a78ecc do_exec_main_checked + 152
59  Mono                                0x00000001049df778 mono_jit_exec + 260
60  FooMobileiOS                        0x000000010024b43c xamarin_main + 2136
61  FooMobileiOS                        0x0000000101e9281c main + 96
62  libdyld.dylib                       0x000000018c3c15b8 <redacted> + 4

=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

Has something changed? Was surprised to see this workaround still being recommended since it does not work for me.
Comment 8 Rolf Bjarne Kvinge 2017-03-22 17:42:37 UTC
Yeah, that workaround won't work on an arm64 device.

You'll need an arm64-specific code path: https://github.com/xamarin/xamarin-macios/blob/master/src/ObjCRuntime/Runtime.cs#L1322-L1324
Comment 9 Nate Cook 2017-03-22 20:10:46 UTC
Cool, the following seems to work. I don't have a non-ARM64 device to try it on. Let me know if you have any concerns:

const string FoundationLibrary = "/System/Library/Frameworks/Foundation.framework/Foundation";

[System.Runtime.InteropServices.DllImport(FoundationLibrary)]
extern static void NSLog(IntPtr format, IntPtr s);

[System.Runtime.InteropServices.DllImport(FoundationLibrary, EntryPoint = "NSLog")]
extern static void NSLog_ARM64(IntPtr format, IntPtr p2, IntPtr p3, IntPtr p4, IntPtr p5, IntPtr p6, IntPtr p7, IntPtr p8, IntPtr s);

static readonly bool Is64Bit = IntPtr.Size == 8;
static readonly bool IsDevice = ObjCRuntime.Runtime.Arch == ObjCRuntime.Arch.DEVICE;

static readonly Foundation.NSString nsFormat = new Foundation.NSString(@"%@");

static void OutputStringToConsole(string text)
{
    using (var nsText = new Foundation.NSString(text))
    {
        if (IsDevice && Is64Bit)
        {
            NSLog_ARM64(nsFormat.Handle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, nsText.Handle);
        }
        else
        {
            NSLog(nsFormat.Handle, nsText.Handle);
        }
    }
}
Comment 10 Rolf Bjarne Kvinge 2017-03-23 11:30:22 UTC
(In reply to Nate Cook from comment #9)
> Cool, the following seems to work. I don't have a non-ARM64 device to try it
> on. Let me know if you have any concerns:

This looks good 👍
Comment 11 amir.jahan 2017-04-10 17:58:48 UTC
I came up with a rather stupid solution for now. I don't know if it's gonna help anyone else, but it does help me. Whenever I need to write something to Console, I load a new textview, write it there and then dismiss it. You can call the method:


showTextView ("Some value to show");

		private void showTextView(String valStr)
		{
			UITextView toastTextView = new UITextView();
			CoreGraphics.CGRect textViewFrame = View.Frame;
			textViewFrame.Size = new CoreGraphics.CGSize(textViewFrame.Size.Width * .9f, textViewFrame.Size.Height * .6f);
			toastTextView.BackgroundColor = UIColor.LightGray;
			toastTextView.Text = valStr;
			toastTextView.Frame = textViewFrame;
			toastTextView.Center = View.Center;
			UIButton dismissButton = new UIButton();
			dismissButton.Frame = new CoreGraphics.CGRect(0, textViewFrame.Size.Height - 40, textViewFrame.Size.Width, 40);
			dismissButton.BackgroundColor = UIColor.DarkGray;
			dismissButton.SetTitle("Dismiss", UIControlState.Normal);
			dismissButton.TouchUpInside += (object sender, EventArgs e) =
			{
				toastTextView.RemoveFromSuperview();
			};

			toastTextView.AddSubview(dismissButton);
			View.AddSubview(toastTextView);
		}


I don't know if it'll help anyone, but I though I'd share it.
Comment 12 Paul Morris 2017-04-25 14:41:46 UTC
Trying the suggestion in comment 9 resolves the crash for me but it doesn't actually write the text to the device log. iPhone SE running 10.3.1
Comment 13 Paul Morris 2017-04-25 17:02:41 UTC
Ok so this is what worked for me in the end:

        public void Write(string message)
        {
            var fmt = NSString.CreateNative ("%s");

            if (IntPtr.Size == 8 && Runtime.Arch == Arch.DEVICE)
            {
                NSLog_arm64(
                    fmt, 
                    IntPtr.Zero, 
                    IntPtr.Zero, 
                    IntPtr.Zero, 
                    IntPtr.Zero, 
                    IntPtr.Zero, 
                    IntPtr.Zero, 
                    IntPtr.Zero, 
                    message);
            }
            else
            {
                NSLog(fmt, message);
            }

            NSString.ReleaseNative(fmt);
        }

        [DllImport(Constants.FoundationLibrary)]
        private extern static void NSLog(IntPtr format, [MarshalAs(UnmanagedType.LPStr)] string s);

        [DllImport(Constants.FoundationLibrary, EntryPoint = "NSLog")]
        private extern static void NSLog_arm64(
            IntPtr format, 
            IntPtr p2, 
            IntPtr p3, 
            IntPtr p4, 
            IntPtr p5, 
            IntPtr p6, 
            IntPtr p7, 
            IntPtr p8, 
            [MarshalAs(UnmanagedType.LPStr)] string s);
Comment 14 softlion 2017-05-03 10:17:48 UTC
When will this work ?
Still have this issue in VS2017 on device only (arm64, iOS 10.3.1).
+ accented characters are not appearing correctly.

Xamarin 4.4.0.34
Xamarin.iOS 10.8.0.174


* Text logged:
Debug Trace xx3 affichée normalement

* Using Console.WriteLine:
May  3 12:12:43 BenTelephone MyAppIos[5292] <Notice>: <\M-b\M^@\M-&>

* Using the workaround as explained here:
May  3 12:12:43 BenTelephone MyAppIos[5292] <Notice>: Debug Trace xx3 affich\M-b\M^H\M^Z\M-B\M-)e normalement


All work as expected on iOS simulator using Console.WriteLine.
Comment 15 Rolf Bjarne Kvinge 2017-05-05 05:54:46 UTC
@softlion, this is fixed in Xamarin.iOS 10.10, which is currently in the beta channel.
Comment 16 softlion 2017-05-05 06:04:56 UTC
Yes i saw that thks.
And this new beta does fix the other bugs i had with the previous beta.

Note You need to log in before you can comment on or make changes to this bug.