markmcdowell / NLog.Targets.ElasticSearch

NLog target for Elasticsearch
MIT License
176 stars 89 forks source link

fix: Fix System.Type serialization #165

Closed denis-ivanov closed 2 years ago

denis-ivanov commented 2 years ago

Checklist

This PR fixed the following error:

2022-08-03 11:45:58.9930 Error ElasticSearch: Error while sending log messages Exception: Elasticsearch.Net.UnexpectedElasticsearchClientException: Failed to serialize anonymous type: System.RuntimeType.
The type defines the following properties:
'IsCollectible' of type System.Boolean
'DeclaringMethod' of type System.Reflection.MethodBase
'FullName' of type System.String
'AssemblyQualifiedName' of type System.String
'Namespace' of type System.String
'GUID' of type System.Guid
'GenericParameterAttributes' of type System.Reflection.GenericParameterAttributes
...
snakefoot commented 2 years ago

Can you not add typeof(Type) to the existing list here:

            ObjectTypeConverters = new List<ObjectTypeConvert>()
            {
                new ObjectTypeConvert(typeof(System.Reflection.Assembly)),     // Skip serializing all types in application
                new ObjectTypeConvert(typeof(System.Reflection.Module)),       // Skip serializing all types in application
                new ObjectTypeConvert(typeof(System.Reflection.MemberInfo)),   // Skip serializing all types in application
                new ObjectTypeConvert(typeof(System.IO.Stream)),               // Skip serializing Stream properties, since they throw
                new ObjectTypeConvert(typeof(System.Net.IPAddress)),           // Skip serializing IPAdress properties, since they throw when IPv6 address
            };
denis-ivanov commented 2 years ago

@snakefoot , done

liderisp commented 2 years ago

Why was this closed? Was really hoping to get this fix ASAP, since passing exception object to LogError triggers this issue..

denis-ivanov commented 2 years ago

It does not solve issue. You can create your own PR

snakefoot commented 2 years ago

@denis-ivanov Not sure why the pull-request was closed, but while waiting for a release then you can do this:

<target xsi:type="ElasticSearch" uri="http://localhost:9200/">
    <typeconverter objectType="System.Type" />
</target>

Which will force ToString for System.Type

snakefoot commented 2 years ago

Alternative you can configure maxRecursionLimit="1", that should protect you against the very fragile Elastic-Json-Serializer.