Home > APIs > AppServer API > Examples > Connection and System Status > Read Log Messages
Read Log Messages
Below is sample code showing how to read messages from the K2's log file.
[C#]
// get a log object from the AppServer
IAppServerLog3 log = (IAppServerLog3) iappServer.CreateLog(appName);
// open the K2's log file
log.Open("C:\\logs\\mlog.mmf");
// set the format to get strings out rather than binary data
log.ReadFormat = "string";
int actualCount = 0; // var used to tell number objects returned
// get the first 10 messages from the top of the log
object[] firstTen = (object[]) log.ReadNextMsg(10, ref actualCount);
foreach (object o in firstTen)
{
Console.WriteLine(o);
}
Console.WriteLine("");
// get the last three messages from the bottom of the log
object[] lastThree = (object[]) log.ReadLastMsg(3, ref actualCount);
foreach (object o in lastThree)
{
Console.WriteLine(o);
}
Console.WriteLine("");
// get the three messages before that
object[] prevThree = (object[]) log.ReadPrevMsg(3, ref actualCount);
foreach (object o in prevThree)
{
Console.WriteLine(o);
}
// delete all messages in log
// log.Clear();
// close the log file
log.Close();
