这个 log4.net 配置有什么问题?
What is wrong with this log4.net configuration?
它确实看起来应该有效。它不会抛出任何错误,遍历每一行代码,但不会生成日志文件。这里有什么问题?它应该在 appender 名称的目录中生成一个日志文件,但从未生成任何日志文件。
log4net.xml 文件:
<configuration>
<log4net debug="true">
<appender name ="task_appender" type="log4net.Appender.RollingAppender">
<file value="C:\Users\ryan\Documents\Visual Studio 2017\Workout Project\GPWorkouts\blablabla.txt"></file>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBacks value="2"/>
<maximumFileSize value="5000KB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline"></conversionPattern>
</layout>
</appender>
<root>
<level value="DEBUG"/>
</root>
<logger name="Task">
<level value="DEBUG"/>
<appender-ref ref="task_appender"/>
</logger>
</log4net>
</configuration>
申请文件:
public partial class MainWindow : Window
{
static protected ILog log = LogManager.GetLogger("Task");
static void log4net_demo()
{
FileInfo fi = new FileInfo("log4net.xml");
log4net.Config.XmlConfigurator.Configure(fi);
log4net.GlobalContext.Properties["host"] = Environment.MachineName;
}
public MainWindow()
{
InitializeComponent();
log4net_demo();
log.Info("This is the information log level");
log.Debug("This is the debugging log level");
log.Error("This is the error log level");
log.Fatal("This is the fatal log level");
}
您的 log4net 配置中似乎有一些拼写错误,正确的附加程序类型名称是 log4net.Appender.RollingFileAppender
在您的 xml 中,它被指定为 log4net.Appender.RollingAppender
这是更正后的 xml
<configuration>
<log4net debug="true">
<appender name ="task_appender" type="log4net.Appender.RollingFileAppender">
<file value="C:\Users\ryan\Documents\Visual Studio 2017\Workout Project\GPWorkouts\blablabla.txt"></file>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBacks value="2"/>
<maximumFileSize value="5000KB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline"></conversionPattern>
</layout>
</appender>
<root>
<level value="DEBUG"/>
</root>
<logger name="Task">
<level value="DEBUG"/>
<appender-ref ref="task_appender"/>
</logger>
</log4net>
</configuration>
它确实看起来应该有效。它不会抛出任何错误,遍历每一行代码,但不会生成日志文件。这里有什么问题?它应该在 appender 名称的目录中生成一个日志文件,但从未生成任何日志文件。
log4net.xml 文件:
<configuration>
<log4net debug="true">
<appender name ="task_appender" type="log4net.Appender.RollingAppender">
<file value="C:\Users\ryan\Documents\Visual Studio 2017\Workout Project\GPWorkouts\blablabla.txt"></file>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBacks value="2"/>
<maximumFileSize value="5000KB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline"></conversionPattern>
</layout>
</appender>
<root>
<level value="DEBUG"/>
</root>
<logger name="Task">
<level value="DEBUG"/>
<appender-ref ref="task_appender"/>
</logger>
</log4net>
</configuration>
申请文件:
public partial class MainWindow : Window
{
static protected ILog log = LogManager.GetLogger("Task");
static void log4net_demo()
{
FileInfo fi = new FileInfo("log4net.xml");
log4net.Config.XmlConfigurator.Configure(fi);
log4net.GlobalContext.Properties["host"] = Environment.MachineName;
}
public MainWindow()
{
InitializeComponent();
log4net_demo();
log.Info("This is the information log level");
log.Debug("This is the debugging log level");
log.Error("This is the error log level");
log.Fatal("This is the fatal log level");
}
您的 log4net 配置中似乎有一些拼写错误,正确的附加程序类型名称是 log4net.Appender.RollingFileAppender
在您的 xml 中,它被指定为 log4net.Appender.RollingAppender
这是更正后的 xml
<configuration>
<log4net debug="true">
<appender name ="task_appender" type="log4net.Appender.RollingFileAppender">
<file value="C:\Users\ryan\Documents\Visual Studio 2017\Workout Project\GPWorkouts\blablabla.txt"></file>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBacks value="2"/>
<maximumFileSize value="5000KB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline"></conversionPattern>
</layout>
</appender>
<root>
<level value="DEBUG"/>
</root>
<logger name="Task">
<level value="DEBUG"/>
<appender-ref ref="task_appender"/>
</logger>
</log4net>
</configuration>