129 lines
3.6 KiB
C#
129 lines
3.6 KiB
C#
using System;
|
|
using JD.Remoting;
|
|
|
|
namespace JD.EngineLogger.Models
|
|
{
|
|
public class LogEntry
|
|
{
|
|
public enum LogLevel
|
|
{
|
|
Debug,
|
|
Info,
|
|
Warning,
|
|
Error,
|
|
Fatal,
|
|
Invalid
|
|
}
|
|
|
|
public string FilePath { get; private set; }
|
|
|
|
public uint FileLine { get; private set; }
|
|
|
|
public DateTime Timestamp { get; private set; }
|
|
|
|
public string Text { get; protected set; }
|
|
|
|
public string Category { get; private set; }
|
|
|
|
public LogLevel Level { get; private set; }
|
|
|
|
public static bool TryLoadFromBlob(IBlob blob, out LogEntry logEntry)
|
|
{
|
|
logEntry = new LogEntry();
|
|
if (blob == null)
|
|
{
|
|
logEntry.Text = $"<Unable to get log entry from a null {nameof(IBlob)} reference.>";
|
|
logEntry.Level = LogLevel.Error;
|
|
return false;
|
|
}
|
|
|
|
bool loadedProperly = true;
|
|
|
|
try
|
|
{
|
|
logEntry.Text = blob.ExtractString();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logEntry.Text = $"<Unable to get log text: {e}>";
|
|
loadedProperly = false;
|
|
}
|
|
|
|
try
|
|
{
|
|
var levelValue = (LogLevel) blob.ExtractByte();
|
|
logEntry.Level = Enum.IsDefined(typeof(LogLevel), levelValue)
|
|
? levelValue
|
|
: LogLevel.Invalid;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logEntry.Text += $"{Environment.NewLine}<unable to get log level: {e}>";
|
|
loadedProperly = false;
|
|
}
|
|
|
|
try
|
|
{
|
|
logEntry.Category = blob.ExtractString();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logEntry.Category = $"<Unable to get category: {e}>";
|
|
loadedProperly = false;
|
|
}
|
|
|
|
try
|
|
{
|
|
logEntry.FilePath = blob.ExtractString();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logEntry.FilePath = $"<Unable to get file path: {e}>";
|
|
loadedProperly = false;
|
|
}
|
|
|
|
try
|
|
{
|
|
logEntry.FileLine = blob.ExtractUInt();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logEntry.FileLine = uint.MaxValue;
|
|
logEntry.Text += $"{Environment.NewLine}<unable to get file line: {e}>";
|
|
loadedProperly = false;
|
|
}
|
|
|
|
try
|
|
{
|
|
var elaspedSeconds = blob.ExtractDouble();
|
|
logEntry.Timestamp = DateTime.Now;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logEntry.Timestamp = DateTime.Now;
|
|
logEntry.Text += $"{Environment.NewLine}<unable to get timestamp: {e}>";
|
|
loadedProperly = false;
|
|
}
|
|
|
|
if (!loadedProperly)
|
|
{
|
|
logEntry.Level = LogLevel.Invalid;
|
|
}
|
|
|
|
return loadedProperly;
|
|
}
|
|
|
|
|
|
public Plume.ErrorList.Model.LogEntry ToErrorListEntry()
|
|
{
|
|
Enum.TryParse(Level.ToString(), out Plume.ErrorList.Model.LogLevel logLevel);
|
|
return new Plume.ErrorList.Model.LogEntry(Text, logLevel, Category, DateTime.Now);
|
|
}
|
|
|
|
public Plume.Output.Model.LogEntry ToOutputEntry()
|
|
{
|
|
Enum.TryParse(Level.ToString(), out Plume.Output.Model.LogLevel logLevel);
|
|
return new Plume.Output.Model.LogEntry(Text, logLevel, Category, DateTime.Now);
|
|
}
|
|
}
|
|
}
|