如何在 Xamarin 中使用 iOS OSLog? [英] How to use iOS OSLog with Xamarin?

查看:42
本文介绍了如何在 Xamarin 中使用 iOS OSLog?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Xamarin.iOS 中使用 iOS OSLog?

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

我确实成功地使用了 NSLog,如下所示,但我看不到使用 NSLog 设置子系统(到包标识符)的方法,以便我可以使用它来过滤 Console.app 中的日志.

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
    }
}

推荐答案

OSLog 是一个 ObjC 结构(两个 const 字符)和一个内核方法,os_log_create,是提供分配一个.

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

注意:详情请参考os/log.h.

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

用法:

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

仅供参考:您的 NSLog 应包含 printf 格式字符串作为 NSString

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);

这篇关于如何在 Xamarin 中使用 iOS OSLog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆