Delete log4net system properties on output

I want to use LogicalThreadContext to pass some context information in my WCF service. I need to pass different properties. In C #, I have code

LogicalThreadContext.Properties["MyProperty"] = 1; 

In log4net configuration I have

 <log4net> <appender name="RollingLogFileAppenderSize" type="log4net.Appender.RollingFileAppender"> <file value="Logs\Log.log" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <datePattern value="yyyyMMdd" /> <maxSizeRollBackups value="3" /> <maximumFileSize value="5MB" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%d [%2t] [%property] %level %m%n" /> </layout> </appender> <root> <level value="INFO" /> <appender-ref ref="RollingLogFileAppenderSize" /> </root> </log4net> 

And in the magazine I got

 2015-11-03 16:24:36,313 [10] [{MyProperty=1, log4net:Identity=, log4net:UserName=User, log4net:HostName=User}] INFO - Info 

I do not want to have the system properties log4net: Identity, log4net: UserName and log4net: HostName in the log. How to do it? I can write config as follows

 conversionPattern value="%d [%2t] [%property{MyProperty}] %level %m%n" 

But I have several properties in the code, and I want to see only the properties that I added. Code

 LogicalThreadContext.Properties.Remove("log4net:UserName"); 

does not work.

+5
source share
2 answers

I found that only log4net can be removed: the HostName property with the GlobalContext.Properties.Remove(LoggingEvent.HostNameProperty) code GlobalContext.Properties.Remove(LoggingEvent.HostNameProperty) . log4net: Identity and log4net: UserName cannot be deleted due to the CreateCompositeProperties method in the log4net.Core.LoggingEvent class https://github.com/apache/log4net/blob/trunk/src/Core/LoggingEvent.cs . It adds these properties without any conditions and therefore it is not possible to remove them for the latest version of log4net.

+3
source

I had the same problem.

 GlobalContext.Properties.Clear(); 

Worked for me.

0
source

Source: https://habr.com/ru/post/1235132/


All Articles