I've just taken over a C# project that uses the log4net xmllayout for logging.
The problem is that each event in the log has 4 data values: machinename, hostname, username and app which are always the same but are repeated for each eve开发者_开发百科nt leading to unnecessarily large log files.
How do I prevent these from being logged?
Config file:
<?xml version="1.0"?>
<configuration>
<!--log4net setting-->
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="%env{LocalAppData}\\myApp\\myApp.log.xml" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="1" />
<maximumFileSize value="3MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.XmlLayoutSchemaLog4j">
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_myAppWCF" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="">
<extendedProtectionPolicy policyEnforcement="Never" />
</transport>
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:12372/conserv" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_myAppWCF" contract="myAppNOWServiceClient.myAppWCF"
name="BasicHttpBinding_myAppWCF" />
</client>
</system.serviceModel>
<startup>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>
When you use XMLLayout, you get what they want to give you. You can't specify which items to log like you can with the other layouts. However, there are alternatives if you want to change this functionality. First, you can change to a file appender and try to make a layout manually. That gets messy at best. The other option is to create your own appender that logs to the XML file only what you want and in the schema you want. This is much more desirable if you are doing a long-term change unless you really don't want to add to log4net.
If you want to go the second route, here is an article that will show you exactly how to do so:
http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/making-an-xmllayout-for-log4net
Here is a SO question that has an answer that explains how to do this as well:
Log4net xml output
精彩评论