I have followed the log4net tutorial very carefully but the logger failed to save as a txt file and failed to appear in the console. I am expecting the log file to be saved under C:\temp.
Here is my app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, Log4net"/>
</configSections>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="C:\temp\Logs.txt"/>
<param name="AppendToFile" value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="3MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger - %message%newline"/>
</layout>
</appender>
<root>
<level value="Debug"/>
<appender-ref ref="LogFileAppender"/>
</root>
</log4net>
</configuration>
I also included
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
in the AssemblyInfo.cs.
I placed my logger in the LoggerView.xaml.cs class. (I am using MVVM design pattern.)
[Export]
[PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.NonShared)]
public partial class LoggerView : Window
{
private LoggerViewModel _viewModel;
protected static readonly ILog log = LogManager.GetLogger(typeof(LoggerView));
[ImportingConstructor]
public LoggerView(LoggerViewModel viewModel)
{
_viewModel = viewModel;
this.DataContext = _viewModel;
InitializeComponent();
}
static void Main(string[] args)
{
log4net.Config.XmlConfigurator.Configure();
log.Debug("Debug Statement");
log.Info("Info");
log.Error("Error Statement");
log.Warn("Warning Statement");
log.Fatal("Fatal Statement");
}
}
Is there anything that I am missing or doing wrong?
Thank you.
Try specifying the config file in your AssemblyInfo.cs, mine is like this:
Add an XML file log4net.config to your project, use this to start with:
then put this in your app (mine was just a simple console app that only logged "Test".