0

I have two properties of Type IDictionary<string, Object>

  • ConfigProperties and CustomProperties

Both have the same values set to them. But one is set through a constructor and the other is from a method signature with help of a static method.

in likewise manner:

Static Method returning IDictionary<string, object> and is later passed on LogMessageEvent.

public void LogMessageEvent(IDictionary<string, object> dictionary, string eventMessage, IReadOnlyCollection<Exception> exceptions, Guid messageId, Guid sessionId, LogMessageType logMessageType, ErrorLevel errorLevel)
        {    
            LogEntity m = new LogEntity(messageId, sessionId, EntityType.Message, logMessageType, errorLevel, dictionary);

                m.Description = eventMessage;
                m.CustomProperties.Add("Configuration", dictionary);
...

Lastly the Constructor way.

public LogEntity(Guid id, Guid relatedId, EntityType entityType, LogMessageType logMessageType, ErrorLevel errorLevel, IDictionary<string,object> configProperties) : base(id, relatedId, entityType)
        {
            ConfigProperties = configProperties,
    ...
        }

Later whenever i print these on the EventViewer I get two different results.

Alternative A

System.Diagnostics.EventLog.WriteEntry("SendAsync", "ConfigProperties: \n " + string.Join(Environment.NewLine, logMessage.ConfigProperties), EventLogEntryType.Error);

Outputs

ConfigProperties: 
 [Url, hej]
[SpecificUrlParameters, {
  "HoursSinceUpdated": "-72"
}]
[Parameters, {
  "Authorization": "LoginTokenProperty"
}]
[HmacArgs, {}]
[StaticPostMessage, ]
[Encryption, tls12]
[LookForCertificate, false] ...

Alternative B CustomProperties - IDictionary<string, Object>

System.Diagnostics.EventLog.WriteEntry("SendAsync", "CustomProps: item:  " + string.Join(Environment.NewLine, logMessage.CustomProperties["Configuration"]), EventLogEntryType.Error);

Outputs

CustomProps: item:  System.Collections.Generic.Dictionary`2[System.String,System.Object]
  • please provide a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Your question is pretty unclear, in particular as you're missing any data within those dictionaries. – MakePeaceGreatAgain Apr 04 '22 at 13:55
  • 1
    Without more info I bet your ConfigProperties have a ToString extensions that cast to JSON and CustomProps didn't – J.Salas Apr 04 '22 at 14:05
  • @J.Salas This means that the one that didn't cast to JSON was Printed? – Ahune ajé o ahe Apr 04 '22 at 14:42
  • String.Join obviously tries to cast to string, rereading your code: CustomProps isn't sent as is to the string.Join, your code send the Object **inside** CustomProperties["Configuration"] dictionary element to the string.Join so perhaps the ToString Override must be inside that Object class – J.Salas Apr 04 '22 at 14:49
  • 1
    If you just need a dictionary that prints out its contents instead of its type name, you can use [`J2N.Collections.Dictionary`](https://github.com/NightOwl888/J2N/blob/main/src/J2N/Collections/Generic/Dictionary.cs#L47-L51) instead of `System.Collections.Dictionary`. [J2N on NuGet](https://www.nuget.org/packages/J2N/). Full Disclosure: I am the primary maintainer of J2N. – NightOwl888 Apr 05 '22 at 07:44

0 Answers0