C# log4Net 日志文件未写入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13525261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
log4Net log file not writing
提问by Shibin V.M
I am trying to implement log4net in my asp.net web application. But unfortunately the file is not created. Below is my configuration.
我正在尝试在我的 asp.net Web 应用程序中实现 log4net。但不幸的是,该文件没有被创建。下面是我的配置。
1 . Added log4net .dll reference
1 . 添加了 log4net .dll 参考
2 . Web.config settings.
2 . Web.config 设置。
<configSections>
<section name="log4net"type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="E:/Log/error_%date{dd-MM-yyyy}.log"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<!--<maxSizeRollBackups value="5"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>-->
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception%newline%newline"/>
</layout>
</appender>
<root>
<appender-ref ref="RollingFileAppender"/>
</root>
</log4net>
3 . Added Assembly reference
3 . 添加了程序集参考
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
4 . Log writing in the code behind
4 . 写在后面的代码中
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
try
{
throw new System.IO.FileNotFoundException();
}
catch (Exception ex)
{
log.Error("Error error logging", ex);
}
These are the steps I had followed, but the log is not created...
这些是我遵循的步骤,但未创建日志...
Please give your suggestions.
请提出您的建议。
Thanks in advance
提前致谢
采纳答案by Milen Kindekov
Make sure you are calling the Configurefunction of the XmlConfigurator.
确保您正在调用XmlConfigurator的Configure函数。
See this solution: Configure Log4Net in web application
请参阅此解决方案:在 Web 应用程序中配置 Log4Net
回答by Rustem
Try to give write permission in E:/Log for asp.net user or for Everyone, then try to add requirePermission="false" attribute, like this:
尝试在 E:/Log 中为 asp.net 用户或所有人授予写权限,然后尝试添加 requirePermission="false" 属性,如下所示:
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" requirePermission="false" />
or you should specify logging level in root section, like this:
或者您应该在根部分指定日志级别,如下所示:
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
</root>

