Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions src/OpenTelemetry/Internal/OpenTelemetrySdkEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ public void LoggerProviderException(string methodName, Exception ex)
}
}

[NonEvent]
public void LoggerProcessStateSkipped<TState>()
{
if (this.IsEnabled(EventLevel.Warning, EventKeywords.All))
{
this.LoggerProcessStateSkipped(typeof(TState).FullName!);
}
}

[Event(4, Message = "Unknown error in SpanProcessor event '{0}': '{1}'.", Level = EventLevel.Error)]
public void SpanProcessorException(string evnt, string ex)
{
Expand Down Expand Up @@ -325,6 +334,12 @@ public void LoggerProviderException(string methodName, string ex)
this.WriteEvent(50, methodName, ex);
}

[Event(51, Message = "Skip processing log state of type '{0}'.", Level = EventLevel.Warning)]
public void LoggerProcessStateSkipped(string type)
{
this.WriteEvent(51, type);
}

#if DEBUG
public class OpenTelemetryEventListener : EventListener
{
Expand Down
60 changes: 37 additions & 23 deletions src/OpenTelemetry/Logs/ILogger/OpenTelemetryLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,38 +194,52 @@ internal static void SetLogRecordSeverityFields(ref LogRecordData logRecordData,
iLoggerData.State = state;
return null;
}
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
else if (RuntimeFeature.IsDynamicCodeSupported)
{
return ParseCustomState(logRecord, state);
}
#endif
else
{
try
{
PropertyDescriptorCollection itemProperties = TypeDescriptor.GetProperties(state);
OpenTelemetrySdkEventSource.Log.LoggerProcessStateSkipped<TState>();
return Array.Empty<KeyValuePair<string, object?>>();
}
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = "ParseCustomState will only be called when RuntimeFeature.IsDynamicCodeSupported is true.")]
private static IReadOnlyList<KeyValuePair<string, object?>> ParseCustomState<TState>(
LogRecord logRecord,
in TState state)
{
try
{
PropertyDescriptorCollection itemProperties = TypeDescriptor.GetProperties(state!);

var attributeStorage = logRecord.AttributeStorage ??= new List<KeyValuePair<string, object?>>(itemProperties.Count);
var attributeStorage = logRecord.AttributeStorage ??= new List<KeyValuePair<string, object?>>(itemProperties.Count);

foreach (PropertyDescriptor? itemProperty in itemProperties)
foreach (PropertyDescriptor? itemProperty in itemProperties)
{
if (itemProperty == null)
{
if (itemProperty == null)
{
continue;
}

object? value = itemProperty.GetValue(state);
if (value == null)
{
continue;
}

attributeStorage.Add(new KeyValuePair<string, object?>(itemProperty.Name, value));
continue;
}

return attributeStorage;
}
catch (Exception parseException)
{
OpenTelemetrySdkEventSource.Log.LoggerParseStateException<TState>(parseException);
object? value = itemProperty.GetValue(state);
if (value == null)
{
continue;
}

return Array.Empty<KeyValuePair<string, object?>>();
attributeStorage.Add(new KeyValuePair<string, object?>(itemProperty.Name, value));
}

return attributeStorage;
}
catch (Exception parseException)
{
OpenTelemetrySdkEventSource.Log.LoggerParseStateException<TState>(parseException);
return Array.Empty<KeyValuePair<string, object?>>();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void EnsureAotCompatibility()
Assert.True(process.ExitCode == 0, "Publishing the AotCompatibility app failed. See test output for more details.");

var warnings = expectedOutput.ToString().Split('\n', '\r').Where(line => line.Contains("warning IL"));
Assert.Equal(40, warnings.Count());
Assert.Equal(39, warnings.Count());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ public void AddOtlpLogExporterParseStateValueCanBeTurnedOff(bool parseState)
{
Assert.Null(logRecord.State);
Assert.NotNull(logRecord.Attributes);
Assert.Contains(logRecord.Attributes, kvp => kvp.Key == "propertyA" && (string)kvp.Value == "valueA");
}
else
{
Expand Down Expand Up @@ -140,7 +139,6 @@ public void AddOtlpLogExporterParseStateValueCanBeTurnedOffHosting(bool parseSta
{
Assert.Null(logRecord.State);
Assert.NotNull(logRecord.Attributes);
Assert.Contains(logRecord.Attributes, kvp => kvp.Key == "propertyA" && (string)kvp.Value == "valueA");
}
else
{
Expand Down