我们正在使用log4net并希望在外部配置文件中指定它的配置(正如我们对其他部分所做的那样).为此,我们将App.config中的log4net部分更改为:
...
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
...
<log4net configSource="Log.config" />
...
Run Code Online (Sandbox Code Playgroud)
在Log.Config文件(与App.config相同的目录)中,我们有:
<log4net>
<appender name="General" type="log4net.Appender.FileAppender">
<file value="myapp.log" />
<layout type="log4net.Layout.SimpleLayout" />
</appender>
<root>
<appender-ref ref="General" />
</root>
</log4net>
Run Code Online (Sandbox Code Playgroud)
但是,当我们运行应用程序时,不会创建任何日志文件(并且没有完成日志记录).没有错误消息输出到控制台.
如果我们将Log.config文件的内容移回App.config(替换上面的第一个代码行),它将按预期工作.知道为什么它不在外部文件中工作吗?
有没有办法配置log4net以在调试期间将日志打印到控制台和文件?
我试图通过在日志发生时立即观察日志来找到一种有效调试软件的方法.
写入文件对我来说是有问题的,因为我不想等到文件被刷新到磁盘然后打开它.
因此我更喜欢它写入控制台.
你有什么建议?
我添加了app.config附加附加的文件,但我无法显示结果控制台.
以下是我的app.config配置:
<?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"> …Run Code Online (Sandbox Code Playgroud) 我正在使用 Log4Net 在我的 Web 应用程序中记录异常。
在这里我找到了一个配置示例:http : //www.csharptocsharp.com/log4net-configuration-for-rockin-loggin
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net>
<!--
This writes the log information to the console window. It only logs events
that are at least at the INFO level (which would mean that DEBUG events are not
captured.
-->
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{ABSOLUTE} [%thread] %level %logger - %message%newlineExtra Info: %property{testProperty}%newline%exception"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO"/>
<levelMax value="FATAL"/>
</filter>
</appender>
<!--
This stores information in the log.txt …Run Code Online (Sandbox Code Playgroud)