本文介绍了如何配置log4net的打印在调试模式安慰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有调试过程中配置log4net的打印日志既控制台和文件的方式?



我试图找到一种方法来有效地调试我的软件立即观察日志时,他们发生了。



写入文件是有问题的,我的调试,因为我不想等到文件被刷新到磁盘,然后打开它。



因此,我更喜欢它写入到控制台中。



你有什么建议?



我添加了的app.config 通过额外的附加文件,但我不能够显示结果控制台。



以下是我的app.config配置:



<?XML版本=1.0>?;
<结构>
< configSections>
<节名称=log4net的TYPE =log4net.Config.Log4NetConfigurationSectionHandler,log4net的/>
< / configSections>
< system.serviceModel>
<&绑定GT;
<&的wsHttpBinding GT;
<绑定名称=WSHttpBinding_IProviderServicecloseTimeout =00:01:00openTimeout =00:01:00receiveTimeout =00:10:00的SendTimeout =00:01:00bypassProxyOnLocal = 假transactionFlow =假hostNameComparisonMode =StrongWildcardmaxBufferPoolSize =524288maxReceivedMessageSize =65536messageEncoding =文本textEncoding =UTF-8useDefaultWebProxy =真allowCookies =假>
< readerQuotas MAXDEPTH =32maxStringContentLength =8192maxArrayLength =16384maxBytesPerRead =4096maxNameTableCharCount =16384/>
<有序的ReliableSession =真正的inactivityTimeout =00:10:00启用=FALSE/>
<安全模式=消息>
<运输clientCredentialType =视窗proxyCredentialType =无的境界=/>
<消息clientCredentialType =视窗negotiateServiceCredential =真algorithmSuite =默认/>
< /安全>
< /&结合GT;
< /&的wsHttpBinding GT;
< /绑定>
<客户端>
<端点地址=HTTP://本地主机:8081 / AP2绑定=的wsHttpBindingbindingConfiguration =WSHttpBinding_IProviderService合同=IProviderServiceNAME =WSHttpBinding_IProviderService>
<同一性GT;
< D​​NS值=本地主机/>
< /身份>
< /端点>
< /客户>
< /system.serviceModel>
< log4net的调试=真正的>
<追加程序名称=RollingLogFileAppenderTYPE =log4net.Appender.RollingFileAppender>
<文件值=C:\\Temp\\1.txt/>
< appendToFile值=真/>
< rollingStyle值=大小/>
< maxSizeRollBackups值=10/>
< maximumFileSize值=10MB/>
< staticLogFileName值=真/>
<布局类型=log4net.Layout.PatternLayout>
< conversionPattern值=% - 5P%D%5rms%-22.22c {1}%-18.18M - %M%N/>
< /布局>
< /附加器>
<追加程序名称=是ConsoleAppenderTYPE =log4net.Appender.ConsoleAppender>
< PARAM NAME =门槛值=INFO/>
<布局类型=log4net.Layout.PatternLayout>
< PARAM NAME =ConversionPattern值=%D [%T]%-5p%C [%X] - %M%N/>
< /布局>
< /附加器>
<根和GT;
<电平值=DEBUG/>
<附加目的地-REF REF =RollingLogFileAppender/>
< /根>
< / log4net的>
<&启动GT;
< supportedRuntime版本=V4.0SKU =.net框架,版本= V4.0/>
< /启动>
< /结构>


解决方案

您需要在您的根节中声明这两个附加目的地



配置您的log4net的是这样的:



<&log4net的GT;
<追加程序名称=控制台TYPE =log4net.Appender.ConsoleAppender>
<布局类型=log4net.Layout.PatternLayout>
< conversionPattern值=%DATE%-5level:%讯息%换行/>
< /布局>
< /附加器>

<追加程序名称=RollingFileAppender进行TYPE =log4net.Appender.RollingFileAppender>
<文件值=Log.txt文件/>
< appendToFile值=FALSE/>
< rollingStyle值=大小/>
< maxSizeRollBackups值=10/>
< maximumFileSize值=50MB/>
< staticLogFileName值=真/>
<布局类型=log4net.Layout.PatternLayout>
< conversionPattern值=%DATE%-5level [%记录仪]:%讯息%换行/>
< /布局>
< /附加器>

<根和GT;
<电平值=INFO/>
<附加目的地-REF REF =控制台/>
<附加目的地-REF REF =RollingFileAppender进行/>
< /根>
< / log4net的>


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>
解决方案

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>

这篇关于如何配置log4net的打印在调试模式安慰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 11:41