Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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

Code Block
languagec#
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.

Code Block
languagec#
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].

Code Block
languagec#
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

Code Block
languagec#
try
{
    try {
        ...
    }
    catch(Exception ex)
    {
        serviceException = true;
        initialMessage= string.Format("[serviceException] with message [{0}] and with stacktrace [{1}]", ex.Message, ex.StackTrace);
    }
    ...
    initialMessage = string.Format("[Process] ended at [{0}] and with sucess.", DateTime.Now.ToLocalTime().ToString());
}
catch (Exception ex)
{
    initialMessage = string.Format("[serviceException] with message [{0}] and with stacktrace [{1}]", ex.Message, ex.StackTrace);
    services.GetRequiredService<Journal>().AppendText(ref initialMessage, string.Format("Exception happened as generic error at {0} : {1}.", 
                                            DateTime.Now.ToLocalTime().ToString(), ex.Message));
}
finally
{
    if (!serviceException)
    {
        services.GetRequiredService<Journal>().AppendText(ref initialMessage, string.Format("Exception happened as generic error at {0} : {1}.",
                                            DateTime.Now.ToLocalTime().ToString(), initialMessage));
        var logMessage = Util.CreateLogMessageInPowerPlatform(initialMessage);
    }
    else {
        //Log in the server - event log with the initialMessage variables
    }
}