Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove log4net system properties from output

Tags:

c#

log4net

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

LogicalThreadContext.Properties["MyProperty"] = 1;

In log4net config 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 log I got

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

I don't want to have system properties log4net:Identity, log4net:UserName and log4net:HostName in log. How to do this? I can write config like this

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

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

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

doesn't work.

like image 860
Vasyl Zv Avatar asked Nov 03 '15 14:11

Vasyl Zv


People also ask

How do I disable log4net?

How do I completely disable all logging at runtime? Setting the Threshold on the Hierarchy to Level OFF will disable all logging from that Hierarchy. This can be done in the log4net configuration file by setting the "threshold" attribute on the log4net configuration element to "OFF".

Where is log4net config?

You can configure the log4net. config file to create log files. The file is located in the webroot\App_data directory of the installation.

Is log4net same as log4j?

log4net is a port of the excellent Apache log4j™ framework to the Microsoft® . NET runtime. We have kept the framework similar in spirit to the original log4j while taking advantage of new features in the . NET runtime.

Is log4net thread safe?

Is log4net thread-safe? Yes, log4net is thread-safe. So, no need for manual locking.


1 Answers

I found that it's posible to remove only log4net:HostName property with code GlobalContext.Properties.Remove(LoggingEvent.HostNameProperty). log4net:Identity and log4net:UserName cannot be removed because of CreateCompositeProperties method in log4net.Core.LoggingEvent class https://github.com/apache/log4net/blob/trunk/src/Core/LoggingEvent.cs. It adds these properties without any conditions and so it's imposible to remove them for the last log4net version.

like image 54
Vasyl Zv Avatar answered Oct 25 '22 12:10

Vasyl Zv