How to use iOS OSLog with Xamarin?

安稳与你 提交于 2019-12-25 03:17:59

问题


How can I use the iOS OSLog in Xamarin.iOS?

I did succeed in using NSLog as follows, but I see no way of setting the subsystem (to the bundle identifier) with NSLog so that I can use that to filter the logs in Console.app.

public class Logger
{
    #if DEBUG
    [DllImport(ObjCRuntime.Constants.FoundationLibrary)]
    private extern static void NSLog(IntPtr message);
    #endif

    public void WriteLine(string line)
    {
        #if DEBUG
        using (var nss = new NSString(line))
        {
            NSLog(nss.Handle);
        }
        #endif
    }
}

回答1:


OSLog is a ObjC struct (of two const chars) and a kernel method, os_log_create, is provided to allocate one.

Note: Refer to the os/log.h for details.

Define:

[DllImport("__Internal", EntryPoint = "os_log_create")]
public static extern IntPtr os_log_create(string subsystem, string category);

Usage:

var oslog = os_log_create("some.bundle.id", "StackOverflowCategory");

FYI: your NSLog should include a printf format string as a NSString

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


来源:https://stackoverflow.com/questions/52429007/how-to-use-ios-oslog-with-xamarin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!