Logging
Logging in Shiny is built into the very core of the framework. We use Microsoft.Extensions.Logging as our abstraction point to allow for all sorts of logging configuration and providers.
With Shiny and especially background services, it is important to know that providers like debug and console won’t work. The reason is simple, when the background delegates/jobs execute, the debugger is not attached. Thus, we recommend using loggers that write to a file or a database.
Some suggested logging provider nugets:
Provider | NuGet |
---|---|
Shiny.Logging.Sqlite | |
Shiny.Logging.AppCenter |
Logging Within Your App
Within any component that has been registered with your dependency injection container, you can now do the following
using Microsoft.Logging.Extensions;
namespace MyNamespace;
public class MyThing {
public MyThing(ILogger<MyThing> logger)
{
logger.LogInformation("Hello World");
}
}
App AppCenter
The AppCenter library can be used without any other Shiny module or wiring. It only requires the standing Microsoft.Logging.Extensions wiring
using Shiny;
namespace ShinyApp;
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp
.CreateBuilder()
.UseMauiApp<App>()
.UseShiny() // <-- add this line (this is important)
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
#if !MACCATALYST
builder.Logging.AddAppCenter("YourAppCenterKey");
#endif
return builder.Build();
}
}
SQLite
SQLite logging is great for local testing of your background delegates as it doesn’t require much additional setup and a log viewer can be created right inside your app
using Shiny;
namespace ShinyApp;
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp
.CreateBuilder()
.UseMauiApp<App>()
.UseShiny() // <-- add this line (this is important)
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
return builder.Build();
}
}