Log4Net 不工作
我正在 Windows XP 中以管理员用户身份使用 C# 开发 .NET Framework 3.5,并使用 log4net 进行 .NET Framework 2.0 二进制引用。
我尝试过 log4net 缺少什么 - 无日志文件已创建,但它对我不起作用。
我将以下内容添加到 AssemblyInfo.cs 中:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
并创建了一个文件“Log4Net.config”,如下所示,但是,当我运行控制台应用程序时,在项目中找不到任何日志文件。我想念什么?
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net>
<!--
log4net documentation http://logging.apache.org/log4net/
Logging levels in order of increasing priority:
* ALL
* DEBUG
* INFO
* WARN
* ERROR
* FATAL
* OFF
-->
<appender name="DebugLog" type="log4net.Appender.FileAppender">
<file value="MyLog_debug.log" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<maximumFileSize value="5MB" />
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="DEBUG" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="InfoLog" type="log4net.Appender.RollingFileAppender">
<file value="MyLog_info.log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMdd" />
<maxSizeRollBackups value="7" />
<maximumFileSize value="5MB" />
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="WARN" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="ErrorLog" type="log4net.Appender.RollingFileAppender">
<file value="MyLog_error.log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMdd" />
<maxSizeRollBackups value="30" />
<maximumFileSize value="5MB" />
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date %-5level %logger - %message%newline" />
</layout>
</appender>
<!-- Set the default logging level and add the active appenders -->
<root>
<level value="ALL" />
<appender-ref ref="DebugLog" />
<appender-ref ref="InfoLog" />
<appender-ref ref="ErrorLog" />
</root>
</log4net>
</configuration>
I am working on .NET Framework 3.5 using C# in Windows XP as administrator user, using log4net for .NET Framework 2.0 binary reference.
I had tried What am I missing with log4net - No log file created but it does`t work for me.
I add the following into AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
And created a file "Log4Net.config" as below, however, when I run my console application, cannot find any log file in my project. What do I miss?
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net>
<!--
log4net documentation http://logging.apache.org/log4net/
Logging levels in order of increasing priority:
* ALL
* DEBUG
* INFO
* WARN
* ERROR
* FATAL
* OFF
-->
<appender name="DebugLog" type="log4net.Appender.FileAppender">
<file value="MyLog_debug.log" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<maximumFileSize value="5MB" />
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="DEBUG" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="InfoLog" type="log4net.Appender.RollingFileAppender">
<file value="MyLog_info.log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMdd" />
<maxSizeRollBackups value="7" />
<maximumFileSize value="5MB" />
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="WARN" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="ErrorLog" type="log4net.Appender.RollingFileAppender">
<file value="MyLog_error.log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMdd" />
<maxSizeRollBackups value="30" />
<maximumFileSize value="5MB" />
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date %-5level %logger - %message%newline" />
</layout>
</appender>
<!-- Set the default logging level and add the active appenders -->
<root>
<level value="ALL" />
<appender-ref ref="DebugLog" />
<appender-ref ref="InfoLog" />
<appender-ref ref="ErrorLog" />
</root>
</log4net>
</configuration>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保配置文件被复制到输出目录。
如果您使用的是 Visual Studio,请转到“Log4Net.config”属性,并确保“复制到输出目录”设置为“始终复制”或“如果较新则复制”。否则,只需确保将配置文件复制到输出目录即可。
另外, log4net.Appender.FileAppender 没有“maximumFileSize”属性,因此您需要从配置文件中删除它。
Make sure the config file is being copied to the output directory.
If you're using Visual Studio, go to the 'Log4Net.config' properties, and make sure that the 'Copy to Output Directory' is set to 'Copy always' or 'Copy if newer'. Otherwise, just make sure you copy the config file to the output directory.
Also, log4net.Appender.FileAppender does not have a 'maximumFileSize' property, so you'll want to remove that from your configuration file.
在您的
myproject.csproj
中添加以下包含内容。In your
myproject.csproj
add following inside which include content.