问题
My configuration file is :
<log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<appender name="InfoFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="D:/" />
<appendToFile value="true" />
<param name="DatePattern" value="yyyyMMdd".txt"" />
<rollingStyle value="Date" />
<maxSizeRollBackups value="100" />
<datePattern value="YYYY-MM-DD'.log'"/>
<maximumFileSize value="1024KB" />
<staticLogFileName value="false" />
<encoding value="utf-8"/>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="ErrorFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="D:/" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="100" />
<maximumFileSize value="10240KB" />
<datePattern value="YYYY-MM-DD'.log'"/>
<staticLogFileName value="true" />
<encoding value="utf-8"/>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="WARN" />
<param name="LevelMax" value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="InfoFileAppender" />
<appender-ref ref="ErrorFileAppender" />
</root>
</log4net>
in app.config.
But it has no log file in disk D:!!!!
回答1:
Just verified your config - works fine. You probably didn't initialize log4net properly. You should add into AssemblyInfo.cs file:
[assembly: XmlConfigurator(Watch = true)]
Or you can use XmlConfigurator.ConfigureAndWatch method or simply
XmlConfigurator.Configure();
Unlike NLog, log4net cannot initialize itself.
NOTES: You should change your date pattern to yyyy-MM-dd'.log'
- year and day should be lowercase letters. Also you can write several levels with single appender. There is no need to create two appenders which write to same file.
回答2:
Try as below
<file value="D:/ABC.txt" />
assemblyinfo.cs
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "~\\Abc\\xyz\\log4net.config", Watch = true)]
来源:https://stackoverflow.com/questions/20512548/how-to-check-log4net-log-path