How-To: Enable and Use Logging
Uno.Extensions.Logging allows you to leverage logging capabilities tailored to your target platform to easily record events for XAML layout, Uno-internal messages, and custom events with severity and verbosity levels of your choice.
Step-by-step
Important
This guide assumes you used the template wizard or dotnet new unoapp to create your solution. If not, it is recommended that you follow the Creating an application with Uno.Extensions documentation to create an application from the template.
1. Installation
Add
Loggingto the<UnoFeatures>property in the Class Library (.csproj) file. If you already haveExtensionsin<UnoFeatures>, thenLoggingis already installed, as its dependencies are included with theExtensionsfeature.<UnoFeatures> Material; + Logging; Toolkit; MVUX; </UnoFeatures>
2. Opt into logging
Uno.Extensions offers a simple way to wire up platform-specific log providers such as
Uno.Extensions.Logging.OSLogLoggerProviderfor iOS andUno.Extensions.Logging.WebAssembly.WebAssemblyConsoleLoggerProviderfor WASM as both debug and console logging.Call the
UseLogging()method to register the resultant implementation ofILoggerwith the DI container:protected override void OnLaunched(LaunchActivatedEventArgs e) { var appBuilder = this.CreateBuilder(args) .Configure(host => { host.UseLogging(); }); ...
3. Use the injected service to log application events
Add a constructor parameter of
ILoggertype to a view model you registered with the service collection:public class MainViewModel { private readonly ILogger logger; public MainViewModel(ILogger logger) { this.logger = logger; } ... }You can now record application events using the injected
ILoggerservice implementation:logger.LogInformation("This is an information log.");