Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure log4net to print to console in debug mode

Is there a way to configure log4net to print logs both to console and to files during debug?

I am trying to find a way to debug my software efficiently by observing the logs immediately when they happen.

Writing to files is problematic for me for debugging because I don't want to wait until the file is flushed to the disk and then to open it.

Therefore I prefer it to write into console.

What do you suggest?

I added the app.config file with the additional appended, but I am not being able to show result console.

Following is my app.config configuration:

<?xml version="1.0"?> <configuration>   <configSections>     <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />   </configSections>     <system.serviceModel>         <bindings>             <wsHttpBinding>                 <binding name="WSHttpBinding_IProviderService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">                     <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>                     <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false"/>                     <security mode="Message">                         <transport clientCredentialType="Windows" proxyCredentialType="None" realm=""/>                         <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default"/>                     </security>                 </binding>             </wsHttpBinding>         </bindings>         <client>             <endpoint address="http://localhost:8081/AP2" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IProviderService" contract="IProviderService" name="WSHttpBinding_IProviderService">                 <identity>                     <dns value="localhost"/>                 </identity>             </endpoint>         </client>     </system.serviceModel>    <log4net debug="true">     <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">       <file value="C:\\Temp\\1.txt" />       <appendToFile value="true" />       <rollingStyle value="Size" />       <maxSizeRollBackups value="10" />       <maximumFileSize value="10MB" />       <staticLogFileName value="true" />       <layout type="log4net.Layout.PatternLayout">         <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />       </layout>     </appender>     <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">       <param name="Threshold" value="INFO" />       <layout type="log4net.Layout.PatternLayout">         <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />       </layout>     </appender>     <root>       <level value="DEBUG" />       <appender-ref ref="RollingLogFileAppender" />     </root>   </log4net>   <startup>     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>   </startup> </configuration> 
like image 920
user829174 Avatar asked Feb 21 '12 08:02

user829174


People also ask

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.


2 Answers

You need to have both appenders declared in your <root> section.

Configure your log4net like this:

<log4net>     <appender name="Console" type="log4net.Appender.ConsoleAppender">         <layout type="log4net.Layout.PatternLayout">             <conversionPattern value="%date %-5level: %message%newline" />         </layout>     </appender>      <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">         <file value="Log.txt" />         <appendToFile value="false" />         <rollingStyle value="Size" />         <maxSizeRollBackups value="10" />         <maximumFileSize value="50MB" />         <staticLogFileName value="true" />         <layout type="log4net.Layout.PatternLayout">             <conversionPattern value="%date %-5level[%logger]: %message%newline" />         </layout>     </appender>      <root>         <level value="INFO" />         <appender-ref ref="Console" />         <appender-ref ref="RollingFileAppender" />             </root> </log4net> 
like image 150
Maciej Avatar answered Sep 17 '22 12:09

Maciej


It looks like you've forgotten to hook your appender up to the root logging:

<root>   <level value="DEBUG" />   <appender-ref ref="RollingLogFileAppender" />   <appender-ref ref="ConsoleAppender" /> </root> 
like image 21
Rob Levine Avatar answered Sep 20 '22 12:09

Rob Levine