Skip to content

Parsing Logs results in an error: Invalid cast from 'Int64' to 'DateTime'. #3

@bjarketrux

Description

@bjarketrux

Hi,
I'm getting an error trying to inspect Meta Logs from a gererated report (using the .NET client).

I'm accessing the logs as such:

var report = await reportingService.RenderAsync(request);
var logs = report.Meta.Logs.ToList();

Doing so results in the following error (Windows 64bit, Asp.NET Core 3.1 Console application):

Invalid cast from 'Int64' to 'DateTime'.
   at System.Int64.System.IConvertible.ToDateTime(IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at Newtonsoft.Json.Linq.Extensions.Convert[T,U](T token)
   at Newtonsoft.Json.Linq.Extensions.Value[T,U](IEnumerable`1 value)
   at Newtonsoft.Json.Linq.Extensions.Value[U](IEnumerable`1 value)
   at jsreport.Shared.SerializerHelper.<>c.<ParseReportMeta>b__0_0(JToken s)

I have traced the error to this Repository in ParseReportMeta:

Timestamp = s["timestamp"].Value<DateTime>()

In context:

public static ReportMeta ParseReportMeta(IDictionary<string, string> meta)
{
    IEnumerable<LogEntry> logs = null;

    if (meta.MetaValue("debugLogs") != null)
    {
        var joLogs = JArray.Parse(meta.MetaValue("debugLogs"));
        logs = joLogs.Select(s => new LogEntry()
        {
            Message = s["message"].Value<string>(),
            Level = (LogEntryLevel)Enum.Parse(typeof(LogEntryLevel), s["level"].Value<string>(), true),
            Timestamp = s["timestamp"].Value<DateTime>()
        });
    }

    ...
}

I extracted the parsing logic into my application and changed the line to the following which seemingly works:

Timestamp = DateTimeOffset.FromUnixTimeMilliseconds(s["timestamp"].Value<long>()).UtcDateTime

Is it a bug or perhaps something with my setup?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions