/
Logging Pattern

Logging Pattern

Success and failure generated by the process will be logged in the dataverse if possible. If the connection to the dataverse is not established, the log will be sent to the server.

Creation of the object LoggerMessage

public class LoggerMessage { private readonly ILogger<LoggerMessage> _logger; public LoggerMessage(ILogger<LoggerMessage> logger) { _logger = logger; } public void LogMessage(string message) { _logger.LogInformation(message); } public void AppendText(ref string intial, string append) { intial = intial + "\n" + append; } }

Creation of the service and its dependency injection

Inject all the service in IServiceProvider regarding the log message and the log management.

public static IServiceProvider GenerateServiceProvider(string[] args, IContextServicePowerPlatform contextServicePowerPlatform, IContextServicePowerBI contextServicePowerBI, IContextServiceDataMappingPowerPlatform contextServiceDataMappingPowerPlatform) { IHost host = Util.Util.CreateHostBuilder(args, contextServicePowerPlatform, contextServicePowerBI, contextServiceDataMappingPowerPlatform).Build(); var scope = host.Services.CreateScope(); return scope.ServiceProvider; }

Generate the IHostBuilder in the method [CreateHostBuilder].

public static IHostBuilder CreateHostBuilder(string[] strings, IContextServicePowerPlatform contextServicePowerPlatform, IContextServicePowerBI contextServicePowerBI, IContextServiceDataMappingPowerPlatform contextServiceDataMappingPowerPlatform) { return Host.CreateDefaultBuilder() .ConfigureServices((_, services) => { services.AddSingleton<LoggerMessage>(); .... }); }

Catch the error and finalize the log message