Once we have collected our memory statistics, we will then use this information to publish a memory update message to the system. On the other end, the health monitor microservice could be listening and interested in high watermarks on CPU or memory. Here's what this function looks like:
public void PublishMemoryUpdateMessage(int gen1, int gen2, float timeSpent, string MemoryBefore, string MemoryAfter)
{
// publish a message
MemoryUpdateMessage msg = new MemoryUpdateMessage
{
Text = "Memory MicroService Ran",
Date = SystemClock.Instance.GetCurrentInstant().ToDateTimeUtc(),
Gen1CollectionCount = gen1,
Gen2CollectionCount = gen2,
TimeSpentPercent = timeSpent,
MemoryBeforeCollection = MemoryBefore,
MemoryAfterCollection = MemoryAfter
};
Bus.Publish(msg, "MemoryStatus");
}
Base Logger
Our base microservice class also exposes the ILogger interface and the MSBaseLogger concrete class:
public interface ILogger
{
void LogInformation(string message);
void LogWarning(string message);
void LogError(string message);
void LogException(string message, Exception ex);
void LogDebug(string message);
void LogTrace(string message);
}
Here's what the concrete class of our MSBaseLogger looks like:
public class MSBaseLogger : ILogger
{
private void WriteLineInColor(string message, ConsoleColor foregroundColor)
{
Console.ForegroundColor = foregroundColor;
Console.WriteLine(message);
Console.ResetColor();
}
public void LogInformation(string message)
{
RILogManager.Default?.SendInformation(message);
WriteLineInColor(message, ConsoleColor.White);
}
public void LogWarning(string message)
{
RILogManager.Default?.SendWarning(message);
WriteLineInColor(message, ConsoleColor.Yellow);
}
public void LogError(string message)
{
RILogManager.Default?.SendError(message);
WriteLineInColor(message, ConsoleColor.Red);
}
public void LogException(string message, Exception ex)
{
RILogManager.Default?.SendException(message, ex);
WriteLineInColor(message, ConsoleColor.Red);
}
public void LogDebug(string message)
{
RILogManager.Default?.SendDebug(message);
WriteLineInColor(message, ConsoleColor.Blue);
}
public void LogTrace(string message)
{
RILogManager.Default?.SendTrace(message);
WriteLineInColor(message, ConsoleColor.Cyan);
Trace.WriteLine(message);
}
}