C# Journald Reader

After searching the web for a solution and not finding a library, I’m instead trying to write a systemd journal reader using csharp and not having much luck.

There’s an example written in c++ at the bottom of the following link:

https://www.freedesktop.org/software/systemd/man/latest/sd_journal_next.html

This is the code I have so far, which, when I run it, I get

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$ ./LogPublisher
success: 0
success: -10
success: -10
Success: -10 Message:
</code>
<code>$ ./LogPublisher success: 0 success: -10 success: -10 Success: -10 Message: </code>
$ ./LogPublisher 
success: 0
success: -10
success: -10
Success: -10 Message: 

As far as I can tell, -10 refers to No child processes as taken from https://en.wikipedia.org/wiki/Errno.h

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>using System.Runtime.InteropServices;
using System.Text;
class Program
{
enum Flags
{
SD_JOURNAL_LOCAL_ONLY = 1 << 0, // 0000 0001 = 1
SD_JOURNAL_RUNTIME_ONLY = 1 << 1, // 0000 0010 = 2
SD_JOURNAL_SYSTEM = 1 << 2, // 0000 0100 = 4
SD_JOURNAL_CURRENT_USER = 1 << 3, // 0000 1000 = 8
SD_JOURNAL_OS_ROOT = 1 << 4, // 0001 0000 = 16
SD_JOURNAL_ALL_NAMESPACES = 1 << 5, // (32) Show all namespaces, not just the default or specified one */
SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE = 1 << 6, // (64) Show default namespace in addition to specified one */
SD_JOURNAL_TAKE_DIRECTORY_FD = 1 << 7, // (128) sd_journal_open_directory_fd() will take ownership of the provided file descriptor. */
SD_JOURNAL_ASSUME_IMMUTABLE = 1 << 8, // (256) Assume the opened journal files are immutable. Journal entries added later may be ignored. */
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Journal
{
public int toplevel_fd;
public string path;
public string prefix;
}
internal static class Systemd
{
[DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
internal static extern int sd_journal_open(ref Journal sd_journal, int flags);
[DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
internal static extern int sd_journal_seek_head(ref Journal sd_journal);
[DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
internal static extern int sd_journal_next(ref Journal sd_journal);
[DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
internal static extern int sd_journal_get_data(ref Journal sd_journal, string field, [Out] StringBuilder data, [Out] ulong length);
[DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
internal static extern int sd_journal_seek_head(IntPtr sd_journal);
[DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
internal static extern void sd_journal_close(ref Journal sd_journal);
}
static void Main()
{
int success;
Journal j = new();
success = Systemd.sd_journal_open(ref j, (int)Flags.SD_JOURNAL_LOCAL_ONLY);
Console.WriteLine($"Success: {success}");
success = Systemd.sd_journal_seek_head(ref j);
Console.WriteLine($"Success: {success}");
// success = Systemd.sd_journal_next(ref j);
// Console.WriteLine($"Success: {success}");
ulong length = 0;
StringBuilder data = new(1024 * 768 * 4);
success = Systemd.sd_journal_get_data(ref j, "MESSAGE", data, length);
Console.WriteLine($"Success: {success} Message: {data}");
Systemd.sd_journal_close(ref j);
}
}
</code>
<code>using System.Runtime.InteropServices; using System.Text; class Program { enum Flags { SD_JOURNAL_LOCAL_ONLY = 1 << 0, // 0000 0001 = 1 SD_JOURNAL_RUNTIME_ONLY = 1 << 1, // 0000 0010 = 2 SD_JOURNAL_SYSTEM = 1 << 2, // 0000 0100 = 4 SD_JOURNAL_CURRENT_USER = 1 << 3, // 0000 1000 = 8 SD_JOURNAL_OS_ROOT = 1 << 4, // 0001 0000 = 16 SD_JOURNAL_ALL_NAMESPACES = 1 << 5, // (32) Show all namespaces, not just the default or specified one */ SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE = 1 << 6, // (64) Show default namespace in addition to specified one */ SD_JOURNAL_TAKE_DIRECTORY_FD = 1 << 7, // (128) sd_journal_open_directory_fd() will take ownership of the provided file descriptor. */ SD_JOURNAL_ASSUME_IMMUTABLE = 1 << 8, // (256) Assume the opened journal files are immutable. Journal entries added later may be ignored. */ } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public struct Journal { public int toplevel_fd; public string path; public string prefix; } internal static class Systemd { [DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)] internal static extern int sd_journal_open(ref Journal sd_journal, int flags); [DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)] internal static extern int sd_journal_seek_head(ref Journal sd_journal); [DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)] internal static extern int sd_journal_next(ref Journal sd_journal); [DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)] internal static extern int sd_journal_get_data(ref Journal sd_journal, string field, [Out] StringBuilder data, [Out] ulong length); [DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)] internal static extern int sd_journal_seek_head(IntPtr sd_journal); [DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)] internal static extern void sd_journal_close(ref Journal sd_journal); } static void Main() { int success; Journal j = new(); success = Systemd.sd_journal_open(ref j, (int)Flags.SD_JOURNAL_LOCAL_ONLY); Console.WriteLine($"Success: {success}"); success = Systemd.sd_journal_seek_head(ref j); Console.WriteLine($"Success: {success}"); // success = Systemd.sd_journal_next(ref j); // Console.WriteLine($"Success: {success}"); ulong length = 0; StringBuilder data = new(1024 * 768 * 4); success = Systemd.sd_journal_get_data(ref j, "MESSAGE", data, length); Console.WriteLine($"Success: {success} Message: {data}"); Systemd.sd_journal_close(ref j); } } </code>
using System.Runtime.InteropServices;
using System.Text;

class Program
{
    enum Flags
    {
        SD_JOURNAL_LOCAL_ONLY = 1 << 0,                 // 0000 0001 = 1
        SD_JOURNAL_RUNTIME_ONLY = 1 << 1,               // 0000 0010 = 2
        SD_JOURNAL_SYSTEM = 1 << 2,                     // 0000 0100 = 4
        SD_JOURNAL_CURRENT_USER = 1 << 3,               // 0000 1000 = 8
        SD_JOURNAL_OS_ROOT = 1 << 4,                    // 0001 0000 = 16
        SD_JOURNAL_ALL_NAMESPACES = 1 << 5,             // (32) Show all namespaces, not just the default or specified one */
        SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE = 1 << 6,  // (64) Show default namespace in addition to specified one */
        SD_JOURNAL_TAKE_DIRECTORY_FD = 1 << 7,          // (128) sd_journal_open_directory_fd() will take ownership of the provided file descriptor. */
        SD_JOURNAL_ASSUME_IMMUTABLE = 1 << 8,           // (256) Assume the opened journal files are immutable. Journal entries added later may be ignored. */
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct Journal
    {
        public int toplevel_fd;
        public string path;
        public string prefix;
    }

    internal static class Systemd
    {
        [DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
        internal static extern int sd_journal_open(ref Journal sd_journal, int flags);

        [DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
        internal static extern int sd_journal_seek_head(ref Journal sd_journal);

        [DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
        internal static extern int sd_journal_next(ref Journal sd_journal);

        [DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
        internal static extern int sd_journal_get_data(ref Journal sd_journal, string field, [Out] StringBuilder data, [Out] ulong length);

        [DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
        internal static extern int sd_journal_seek_head(IntPtr sd_journal);

        [DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
        internal static extern void sd_journal_close(ref Journal sd_journal);
    }

    static void Main()
    {
        int success;
        Journal j = new();

        success = Systemd.sd_journal_open(ref j, (int)Flags.SD_JOURNAL_LOCAL_ONLY);
        Console.WriteLine($"Success: {success}");

        success = Systemd.sd_journal_seek_head(ref j);
        Console.WriteLine($"Success: {success}");

        // success = Systemd.sd_journal_next(ref j);
        // Console.WriteLine($"Success: {success}");

        ulong length = 0;
        StringBuilder data = new(1024 * 768 * 4);

        success = Systemd.sd_journal_get_data(ref j, "MESSAGE", data, length);
        Console.WriteLine($"Success: {success} Message: {data}");

        Systemd.sd_journal_close(ref j);
    }
}

I’ve been trying to reference the source code for journal to replicate but just cant work it out.

https://github.com/systemd/systemd/blob/main/src/systemd/sd-journal.h

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật