问题
I'm trying to utilize the Loggly appender utility for log4net.
I've found that their code is enumerating through the ThreadContext
properties and appending them to the payload getting sent over the wire to the loggly service. Good idea! However, the same feature is not being applied to the GlobalContext
properties. Figuring this was a miss on their part I tried my hand at enumerating through the GlobalContext
properties and adding these to the payload as well.
However, this has proven to be a problem. There doesn't appear to be any way to access the keys and associated values as the ThreadContext
properties are accessed.
How can the GlobalContext
properties be enumerated?
回答1:
The only way I see would be to retrieve the properties class for the global context (GlobalContext.Properties
which returns a GlobalContextProperties
class) and get the ReadOnlyPropertiesDictionary
returned by the internal method GetReadOnlyProperties()
through reflection. Once you have the ReadOnlyPropertiesDictionary
you can iterate on keys and values
From what I see the ThreadContext
has more or less the same mechanism so you could take example on the ThreadContext
enumeration to port it to the GlobalContext
.
来源:https://stackoverflow.com/questions/29448768/log4net-enumerating-globalcontext-properties