Skip to content

Ensure we can handle more data types in ElapsedMillisecond/Elapsed from serilog #514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/Elastic.CommonSchema.Serilog/LogEventConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,13 @@ private static Event GetEvent(LogEvent e)
var hasElapsedMs = e.TryGetScalarPropertyValue(SpecialKeys.Elapsed, out var elapsedMsObj)
|| e.TryGetScalarPropertyValue(SpecialKeys.ElapsedMilliseconds, out elapsedMsObj);

var elapsedMs = hasElapsedMs ? (double?)Convert.ToDouble(elapsedMsObj!.Value) : null;
var elapsedMs = hasElapsedMs ?
elapsedMsObj!.Value is TimeSpan ts
? ts.TotalMilliseconds
: elapsedMsObj.Value is double d
? d
: elapsedMsObj.Value is long l ? l : ToDouble()
: null;

var evnt = new Event
{
Expand Down Expand Up @@ -300,6 +306,19 @@ private static Event GetEvent(LogEvent e)
}

return evnt;

double? ToDouble()
{
try
{
return Convert.ToDouble(elapsedMsObj.Value);
}
catch
{
// ignored
}
return null;
}
}

private static Agent? GetAgent(LogEvent e)
Expand Down
Loading