Microsoft.Extensions.Logging
ILocalizationErrorHandler can be added to service collection to log localization file parse errors.
IServiceCollection serviceCollection = new ServiceCollection()
.AddAvalancheLocalizationService()
.AddAvalancheLocalizationFileSystemApplicationRoot()
.AddSingleton(typeof(ILocalizationErrorHandler), new LocalizationErrorHandler(e => WriteLine(e)));
ILogger is adapted to ILocalizationErrorHandler which logs localization file parse errors.
// Add service descriptors
IServiceCollection serviceCollection = new ServiceCollection()
.AddAvalancheLocalizationService()
.AddAvalancheLocalizationFileSystemApplicationRoot()
.AddLogging(loggingBuilder => loggingBuilder.SetMinimumLevel(LogLevel.Trace).AddConsole());
// Build service
using ServiceProvider service = serviceCollection.BuildServiceProvider();
// Get Localization
ILocalization localization = service.GetService<ILocalization>()!;
// Get text
ILocalizedText localizedText = localization.LocalizedTextCached[("", "Example.ErrorExample")];
// Print text
WriteLine(localizedText.Print(new object[] { 2 }));
IStringLocalizer, IViewLocalizer, IHtmlLocalizer, ITextLocalizer and IFileLocalizer log an entry when key is not found.
dbug: Avalanche.Localization.Internal.DI.StringLocalizer[-1599422402] Localization file was not found: Key=NonExistent, Culture=fi
Full Example
Full example
using Avalanche.Localization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using static System.Console;
class microsoft_extensions_logging
{
public static void Run()
{
{
// Add service descriptors
// <01>
IServiceCollection serviceCollection = new ServiceCollection()
.AddAvalancheLocalizationService()
.AddAvalancheLocalizationFileSystemApplicationRoot()
.AddSingleton(typeof(ILocalizationErrorHandler), new LocalizationErrorHandler(e => WriteLine(e)));
// </01>
// Build service
using ServiceProvider service = serviceCollection.BuildServiceProvider();
// Get Localization
ILocalization localization = service.GetService<ILocalization>()!;
// Get text
ILocalizedText localizedText = localization.LocalizedTextCached[("", "Example.ErrorExample")];
// Print text
WriteLine(localizedText.Print(new object[] { 2 }));
}
{
// <02>
// Add service descriptors
IServiceCollection serviceCollection = new ServiceCollection()
.AddAvalancheLocalizationService()
.AddAvalancheLocalizationFileSystemApplicationRoot()
.AddLogging(loggingBuilder => loggingBuilder.SetMinimumLevel(LogLevel.Trace).AddConsole());
// Build service
using ServiceProvider service = serviceCollection.BuildServiceProvider();
// Get Localization
ILocalization localization = service.GetService<ILocalization>()!;
// Get text
ILocalizedText localizedText = localization.LocalizedTextCached[("", "Example.ErrorExample")];
// Print text
WriteLine(localizedText.Print(new object[] { 2 }));
// </02>
}
}
}