I have a console application and have a class library which wraps the Log4Net methods. Now when run the application in debug mode it writes log but when it is built in release mode it doesn’t write log file. What would be the solution for this? The sample code and config file is given below
My development environment is
- Visual Studio 2013 and .NET Framework 4.5
Console Application
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
log4net.GlobalContext.Properties["LogFileName"] = "TestLogin.txt";
Logger log = new Logger(typeof(Program));
log.Info("Logging is enabled!!");
}
}
}
App.config in Console Application
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value ="%property{LogFileName}"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level - %message%newline%exception" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</configuration>
Class Library
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace Logging
{
public class Logger
{
private readonly ILog log = null;
public Logger(Type type)
{
log = LogManager.GetLogger(type);
}
public void Info(object message)
{
log.Info(message);
}
}
}
I have followed the post and it didn’t help me to figure out why Log4Net doesn’t write in log file in release mode?
log4net doesn't log when running a .Net 4.0 Windows application built in Release mode
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…