Skip to content

Update example to flush OnStop #34

Open
@snakefoot

Description

@snakefoot

Update the example code to force flush on OnStop:

using Microsoft.Maui.LifecycleEvents;

namespace PlatformLifecycleDemo
{
    public static class MauiProgram
    {
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                .ConfigureLifecycleEvents(events =>
                {
#if IOS || MACCATALYST
                    events.AddiOS(ios => ios
                        .OnActivated((app) => LogEvent(nameof(iOSLifecycle.OnActivated)))
                        .OnResignActivation((app) => LogEvent(nameof(iOSLifecycle.OnResignActivation)))
                        .DidEnterBackground((app) => LogEvent(nameof(iOSLifecycle.DidEnterBackground)))
                        .WillTerminate((app) => LogEvent(nameof(iOSLifecycle.WillTerminate))));
#endif
                    static bool LogEvent(string eventName, string type = null)
                    {
                        NLog.LogManager.Info($"Lifecycle event: {eventName}{(type == null ? string.Empty : $" ({type})")}");
                        NLog.LogManager.Flush();
                        return true;
                    }
                });

            return builder.Build();
        }
    }
}

Mostly important if depending on async-wrapper or network-targets (eMail / Syslog / Graylog / Database etc.)

If just using pure console-target and file-target without any wrappers, then everything is synchronous and no bonus flush needed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions