Reputation: 2485
I am trying to write a log file using Log4net in C# but the log is getting printed on console. Here is my code
class Program
{
public static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
static void Main(string[] args)
{
log4net.Config.BasicConfigurator.Configure();
Log.Debug("TEST");
Console.Read();
}
}
Here is the XML (log4net.config)
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender type="log4net.Appender.RollingFileAppender" name="RollingFileAppender">
<file value="mylog.txt"/>
<appendToFile value="true"/>
<datePattern value="yyyyMMdd"/>
<rollingStyle value="Once"/>
<layout type="log4net.Layout.SimpleLayout">
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="RollingFileAppender"/>
</root>
</log4net>
inside Assembly.cs i have
[assembly: XmlConfigurator(Watch=true)]
But still the log file isn't create inside debug folder. Does anyone have any idea why this is getting printed on console instead of inside the Log file.
Upvotes: 0
Views: 687
Reputation: 7355
Do you copy the config file to the output directory?
If you are using Visual Studio, set the Build Action to Copy to output directory="Copy Always" or "Copy if newer" and BuildAction="Content"
Upvotes: 2
Reputation: 16393
If you are using the app.config for log4net configuration, I think you should use
log4net.Config.XmlConfigurator.Configure();
Also, you might need to resolve the log after calling configure
public static log4net.ILog Log { get; private set; }
static void Main(string[] args)
{
log4net.Config.XmlConfigurator.Configure();
Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
}
Upvotes: 0