Skip to end of metadata
Go to start of metadata

You are viewing an old version of this content. View the current version.

Compare with Current View Version History

« Previous Version 3 Next »

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

Catch the error and finalize the log message

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
    }
}
  • No labels