Message Printing
.Print(culture) prints to a string.
// Create message description
IMessageDescription fileNotFoundFileName = new MessageDescription("mscorlib.IO.FileNotFound_FileName", 0xA345040F, "Could not find file '{0}'.").SetHResult(0x80070002).SetSeverity(MessageLevel.Error).SetException(typeof(FileNotFoundException));
// Create message
IMessage message = fileNotFoundFileName.New("MyFile.txt");
// Print as string
string printOut = message.Print(CultureInfo.InvariantCulture);
// Write print
WriteLine(printOut); // "Could not find file 'MyFile.txt'."
.EstimatePrintLength(culture) counts the number of characters and .PrintTo(span, culture, args) writes to span. Estimation is needed for zero heap printing.
// Create message description
IMessageDescription fileNotFoundFileName = new MessageDescription("mscorlib.IO.FileNotFound_FileName", 0xA345040F, "Could not find file '{0}'.").SetHResult(0x80070002).SetSeverity(MessageLevel.Error).SetException(typeof(FileNotFoundException));
// Create message
IMessage message = fileNotFoundFileName.New("MyFile.txt");
// Estimate length
int length = message.EstimatePrintLength(CultureInfo.InvariantCulture);
// Allocate span
Span<char> span = length < 512 ? stackalloc char[length] : new char[length];
// Print to span
length = message.PrintTo(span, CultureInfo.InvariantCulture);
// Print to stdout, zero heap.
Console.Out.Write(span.Slice(0, length));
.TryPrintTo(span, culture) attempts to print to a Span<char> without possibility of throwing InvalidOperationException on too short allocation.
// Create message description
IMessageDescription fileNotFoundFileName = new MessageDescription("mscorlib.IO.FileNotFound_FileName", 0xA345040F, "Could not find file '{0}'.").SetHResult(0x80070002).SetSeverity(MessageLevel.Error).SetException(typeof(FileNotFoundException));
// Create message
IMessage message = fileNotFoundFileName.New("MyFile.txt");
// Estimate length
int length = message.EstimatePrintLength(CultureInfo.InvariantCulture);
// Allocate span
Span<char> span = length < 512 ? stackalloc char[length] : new char[length];
// Print to span
if (message.TryPrintTo(span, out length, CultureInfo.InvariantCulture))
Console.Out.Write(span.Slice(0, length));
.AppendTo(stringbuilder, culture) appends to a string builder.
// Create message description
IMessageDescription fileNotFoundFileName = new MessageDescription("mscorlib.IO.FileNotFound_FileName", 0xA345040F, "Could not find file '{0}'.").SetHResult(0x80070002).SetSeverity(MessageLevel.Error).SetException(typeof(FileNotFoundException));
// Create message
IMessage message = fileNotFoundFileName.New("MyFile.txt");
// Create string builder
StringBuilder sb = new(1024);
// Append to string builder
message.AppendTo(sb, CultureInfo.InvariantCulture);
// Print
WriteLine(sb); // "Could not find file 'MyFile.txt'."
.WriteTo(textwriter, culture) writes to a text writer.
// Create message description
IMessageDescription fileNotFoundFileName = new MessageDescription("mscorlib.IO.FileNotFound_FileName", 0xA345040F, "Could not find file '{0}'.").SetHResult(0x80070002).SetSeverity(MessageLevel.Error).SetException(typeof(FileNotFoundException));
// Create message
IMessage message = fileNotFoundFileName.New("MyFile.txt");
// Assign text writer
TextWriter textWriter = Console.Out;
// Write to writer
message.WriteTo(textWriter, CultureInfo.InvariantCulture); // "Could not find file 'MyFile.txt'."
.LogTo(logger) outputs message to a logger.
// Create message
IMessage message = SystemMessages.ArgumentNull.Generic.New("argumentName");
// Log message
message.LogTo(logger);
Message implements IFormattable and ISpanFormattable. Format provider is forwarded to arguments.
// Create template
ITemplateText text = TemplateFormat.BraceNumeric.Text["Time={0}."];
// Create message description
IMessageDescription messageDescription = new MessageDescription().SetTemplate(text);
// Create message
IMessage message = messageDescription.New(DateTime.Now);
// Get cultures
CultureInfo fi = CultureInfo.GetCultureInfo("fi"), en = CultureInfo.GetCultureInfo("en");
// Use as IFormattable or ISpanFormattable
WriteLine(String.Format(fi, "Message: {0}", message)); // "Message: Time=10.1.2022 18.53.06."
WriteLine(String.Format(en, "Message: {0}", message)); // "Message: Time=1/10/2022 6:53:06 PM."
Note
Thank you C# 10 for Span, stackalloc, ISpanFormattable and MemoryPool which allow zero heap or near zero implementations.
Full Example
Full example
using System.Globalization;
using System.Text;
using Avalanche.Message;
using Avalanche.StatusCode;
using Avalanche.Template;
using static System.Console;
class printing
{
public static void Run()
{
{
// <01>
// Create message description
IMessageDescription fileNotFoundFileName = new MessageDescription("mscorlib.IO.FileNotFound_FileName", 0xA345040F, "Could not find file '{0}'.").SetHResult(0x80070002).SetSeverity(MessageLevel.Error).SetException(typeof(FileNotFoundException));
// Create message
IMessage message = fileNotFoundFileName.New("MyFile.txt");
// Print as string
string printOut = message.Print(CultureInfo.InvariantCulture);
// Write print
WriteLine(printOut); // "Could not find file 'MyFile.txt'."
// </01>
}
{
// <03>
// Create message description
IMessageDescription fileNotFoundFileName = new MessageDescription("mscorlib.IO.FileNotFound_FileName", 0xA345040F, "Could not find file '{0}'.").SetHResult(0x80070002).SetSeverity(MessageLevel.Error).SetException(typeof(FileNotFoundException));
// Create message
IMessage message = fileNotFoundFileName.New("MyFile.txt");
// Estimate length
int length = message.EstimatePrintLength(CultureInfo.InvariantCulture);
// Allocate span
Span<char> span = length < 512 ? stackalloc char[length] : new char[length];
// Print to span
length = message.PrintTo(span, CultureInfo.InvariantCulture);
// Print to stdout, zero heap.
Console.Out.Write(span.Slice(0, length));
// </03>
WriteLine();
}
{
// <04>
// Create message description
IMessageDescription fileNotFoundFileName = new MessageDescription("mscorlib.IO.FileNotFound_FileName", 0xA345040F, "Could not find file '{0}'.").SetHResult(0x80070002).SetSeverity(MessageLevel.Error).SetException(typeof(FileNotFoundException));
// Create message
IMessage message = fileNotFoundFileName.New("MyFile.txt");
// Estimate length
int length = message.EstimatePrintLength(CultureInfo.InvariantCulture);
// Allocate span
Span<char> span = length < 512 ? stackalloc char[length] : new char[length];
// Print to span
if (message.TryPrintTo(span, out length, CultureInfo.InvariantCulture))
Console.Out.Write(span.Slice(0, length));
// </04>
WriteLine();
}
{
// <11>
// Create message description
IMessageDescription fileNotFoundFileName = new MessageDescription("mscorlib.IO.FileNotFound_FileName", 0xA345040F, "Could not find file '{0}'.").SetHResult(0x80070002).SetSeverity(MessageLevel.Error).SetException(typeof(FileNotFoundException));
// Create message
IMessage message = fileNotFoundFileName.New("MyFile.txt");
// Create string builder
StringBuilder sb = new(1024);
// Append to string builder
message.AppendTo(sb, CultureInfo.InvariantCulture);
// Print
WriteLine(sb); // "Could not find file 'MyFile.txt'."
// </11>
}
{
// <13>
// Create message description
IMessageDescription fileNotFoundFileName = new MessageDescription("mscorlib.IO.FileNotFound_FileName", 0xA345040F, "Could not find file '{0}'.").SetHResult(0x80070002).SetSeverity(MessageLevel.Error).SetException(typeof(FileNotFoundException));
// Create message
IMessage message = fileNotFoundFileName.New("MyFile.txt");
// Assign text writer
TextWriter textWriter = Console.Out;
// Write to writer
message.WriteTo(textWriter, CultureInfo.InvariantCulture); // "Could not find file 'MyFile.txt'."
// </13>
WriteLine();
}
{
// <21>
// Create template
ITemplateText text = TemplateFormat.BraceNumeric.Text["Time={0}."];
// Create message description
IMessageDescription messageDescription = new MessageDescription().SetTemplate(text);
// Create message
IMessage message = messageDescription.New(DateTime.Now);
// Get cultures
CultureInfo fi = CultureInfo.GetCultureInfo("fi"), en = CultureInfo.GetCultureInfo("en");
// Use as IFormattable or ISpanFormattable
WriteLine(String.Format(fi, "Message: {0}", message)); // "Message: Time=10.1.2022 18.53.06."
WriteLine(String.Format(en, "Message: {0}", message)); // "Message: Time=1/10/2022 6:53:06 PM."
// </21>
}
}
}