gpt4 book ai didi

c# - Log4net配置错误

转载 作者:太空宇宙 更新时间:2023-11-03 21:56:13 25 4
gpt4 key购买 nike

我正在使用 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 file. It only captures log events
that contain the key word test or error.
-->
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="L:\Name\trunk\Name.Web\log.txt"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="5"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="test"/>
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="error"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
</layout>
</appender>
<root>
<level value="FATAL"/>
<appender-ref ref="RollingFileAppender"/>
</root>
<logger name="Log4NetTest.OtherClass">
<level value="DEBUG"/>
<appender-ref ref="ConsoleAppender"/>
</logger>
</log4net>


</configuration>

在我的课上我有:

protected static readonly ILog log = LogManager.GetLogger(typeof(TemplateController));

在我的方法中我有:

log4net.Config.XmlConfigurator.Configure();

//————————–
log.Error("sadi the great");
log.Info("sadi the great");

但是我有错误:

enter image description here

最佳答案

Rolling File appender 的工作配置是这样的:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="logger.log" />
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %logger [%thread] [%ndc] (%file:%line) %level- %message%newline"/>
</layout>
</appender>

<root>
<level value="DEBUG" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
</configuration>

并且您应该将目标框架更改为“.net framework 4”(默认情况下它与我的情况不同)检查这个会有用

关于c# - Log4net配置错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12141309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com