[英]Log4Net is creating file but not writing to it
Recently i had an issue making Log4Net work ( described here ) but after that it was OK. 最近我有一个问题使Log4Net工作( 在这里描述 )但在那之后它没关系。
I have left this behind a while because i needed to develop some modules and i left the logging somewhat behind. 我已经离开了这一段时间,因为我需要开发一些模块,我让日志记录稍微落后了。 Now that i look, i have even tried changing the name of the log file and the location (set it statically), it is creating it but not writing anything to it in both cases.
现在,我看,我甚至尝试更改日志文件的名称和位置(静态设置),它创建它,但在两种情况下都没有写任何东西。
This is my log4Net config file: 这是我的log4Net配置文件:
<?xml version="1.0"?>
<configuration>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="console" />
<appender-ref ref="file" />
</root>
<appender name="console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message%newline" />
</layout>
</appender>
<appender name="file" type="log4net.Appender.RollingFileAppender">
<file value="ApplicationLogging.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
</appender>
</log4net>
</configuration>
This is my Global.asax 这是我的Global.asax
[assembly: XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
XmlConfigurator.Configure();
ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
logger.Info("Application started.");
How i declare it: 我是如何声明的:
readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
I than use it sometimes like this: 我有时会像这样使用它:
logger.Info("some logging here");
Or for Context logging i would use it like this: 或者对于Context记录,我会像这样使用它:
context.Database.Log = (dbLog => logger.Debug(dbLog));
The file is created, but no content is written to it. 该文件已创建,但未向其写入任何内容。 Can anyone suggest me where or what to look for?
任何人都可以建议我在哪里或寻找什么?
UPDATE: As suggested by stuartd i have added this in the web.config: 更新:正如stuartd所建议的,我在web.config中添加了这个:
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\log4net.txt" />
</listeners>
</trace>
</system.diagnostics>
Which writes the following content (pastebin) 其中写了以下内容(pastebin)
Note that i have removed the first section which i didn't saw before and i think it is redundant? 请注意,我删除了之前没有看过的第一部分,我认为它是多余的?
<!--<appender name="console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message%newline" />
</layout>
</appender>-->
Either way with or without it it doesn't work. 无论是否使用它都不起作用。
Here is the output WITH the first part NOT commented out (in its original state) 这是输出 ,第一部分未注释掉(处于原始状态)
I tried the following as well: https://logging.apache.org/log4net/release/config-examples.html https://csharp.today/log4net-tutorial-great-library-for-logging/ 我也尝试了以下内容: https : //logging.apache.org/log4net/release/config-examples.html https://csharp.today/log4net-tutorial-great-library-for-logging/
I am clueless on why it can create it but not write to it... I can not seem to find anything on the web neither, all issues are regarding the creation of the file, not writing to it. 我不知道为什么它可以创建它但不写入它...我似乎无法在网上找到任何东西,所有问题都与文件的创建有关,而不是写入文件。
+1 for including the debug/trace log in your question. +1用于在您的问题中包含调试/跟踪日志。
The message log4net: Configuring Repository [log4net-default-repository]
appears multiple times in this log, which looks like the configuration is being set up twice , once via the XmlConfiguratorAttribute
and a second time via the call to XmlConfigurator.Configure();
消息
log4net: Configuring Repository [log4net-default-repository]
在此日志中多次出现,看起来配置正在设置两次 ,一次是通过XmlConfiguratorAttribute
,另一次是通过调用XmlConfigurator.Configure();
. 。
Inspecting the source code of the XmlConfiguratorAttribute
reveals that it internally makes a similar call to XmlConfigurator.Configure(...);
检查
XmlConfiguratorAttribute
的源代码显示它在内部对XmlConfigurator.Configure(...);
进行类似的调用XmlConfigurator.Configure(...);
private void ConfigureFromFile(ILoggerRepository targetRepository, FileInfo configFile)
{
if (m_configureAndWatch)
{
XmlConfigurator.ConfigureAndWatch(targetRepository, configFile);
}
else
{
XmlConfigurator.Configure(targetRepository, configFile);
}
}
This is what happens. 这就是发生的事情。
The XmlConfiguratorAttribute
initially sets up the loggers and appenders as defined in the log4net.config
file, and creates the empty ApplicationLogging.log
file. XmlConfiguratorAttribute
最初设置log4net.config
文件中定义的记录器和追加器,并创建空的ApplicationLogging.log
文件。
Then the call to XmlConfigurator.Configure();
然后调用
XmlConfigurator.Configure();
overwrites these loggers and appenders with the configuration as in web.config
( being the default location ) which doesn't contain any as you are using a separate log4net.config
file. 使用
web.config
( 作为默认位置 )中的配置覆盖这些记录器和appender,因为您使用单独的log4net.config
文件,该配置不包含任何内容。
Because of this, you end up without any Appender
, and nothing gets logged. 因此,您最终没有任何
Appender
,也没有任何记录。
The solution is to apply XmlConfiguratorAttribute
or call XmlConfigurator.Configure()
with the path to the log4net.config
file ( XmlConfigurator.Configure(new FileInfo("log4net.config"))
), but don't do both . 解决方案是应用
XmlConfiguratorAttribute
或使用log4net.config
文件的路径调用XmlConfigurator.Configure()
( XmlConfigurator.Configure(new FileInfo("log4net.config"))
),但不要同时执行这两个操作 。
Please check if your application has write permissions on destination folder for log file, Those kind of things can sometimes lead to a real pain. 请检查您的应用程序是否对日志文件的目标文件夹具有写入权限,这些事情有时会导致真正的痛苦。
Besides everything else looks quite good. 除了其他一切看起来相当不错
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.